如何创建服务在Android中,这使得坚持XMPP连接使用XMPP服务器? [英] How To Create Service In Android Which Makes Persist Xmpp Connection With XMPP Server?

查看:554
本文介绍了如何创建服务在Android中,这使得坚持XMPP连接使用XMPP服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在网上搜索了很多之后,我们才能确保与XMPP服务器的持续XMPP连接来一个结论,我们必须创建一个服务,

我们已经创建了一个它使用aSmack库与XMPP服务器连接,这是工作的罚款与移动和无线网络。

每当你做出一些设计方法总是事!!!,aSmack有这听的连接,如果连接中断aSmack尝试在一定的时间间隔与XMPP服务器重新连接此重新连接机制已经在那里库隐式实现的。

我们的用例场景::标签                        互联网连接可能是因为WiFi或数据网络,在这里如果连接走的是一个人的理想状态下关闭屏幕CPU进入睡眠状态,目前任何数据被发送到服务器的连接上不会有任何响应,因为服务器没有更多的聆听客户端,在客户端XMPP连接已经在连接模式下连接侦听器未检测到从服务器的任何连接中断,所以在这里流得到肤色的。

在搜索上互联网,我们发现可能的解决方案来解决这个问题是ping主机修复后,(我们使用的是1分钟为固定的周期),Ping不通检测后,我们已经实现了重联机制一样咂嘴(理念取自aSmack重联机制本身),通过使用定时器的任务。

问题::唯一的问题我们是讨厌的排放,如果用户与互联网和连接区间仍连接会增加它会消耗古怪。

1)。什么是上述问题的可能的解决方案?

2)。如果我们不得不采取另一种方法?

解决方案
  

如何创建服务在Android中,这使得坚持XMPP连接   随着XMPP服务器?

两件事情

  1. 重新建立连接,通过侦听CONNECTIVITY_CHANGED意图,并确定当前使用的数据连接去了(,取而代之的是另一种)。
  2. 确保连接已建立通过ping服务器

有关

备注
  1. 监听CONNECTIVY_CHANGED是不够的,你需要pviously比较$ P $与现在活跃的一个活动连接。而如果它是不一样的,重新建立XMPP连接。
  2. 啪4.1自带的<一个href="https://github.com/igniterealtime/Smack/blob/master/smack-android-extensions/src/main/java/org/jivesoftware/smackx/ping/android/ServerPingWithAlarmManager.java"相对=nofollow> ServerPingWithAlarmManager ,这将检查是否有平根据PingManager <强的设置要求>每30 mintues 。这个值是硬$ C $光盘一个不能更改。

使用1分钟平安间隔的方式来了!正如你所经历的,它会耗尽你的电池非常快。合理的ping间隔是什么> 15分钟,我建议30分钟。啪4.1也将确保一个平仅发送,如果没有收到节withing ping时间间隔存在。

也可以使用XEP-0198:流管理在可能的情况

我建议看着遵循以下指导原则和实现稳定的,永久连接不会耗尽用户的电池 1

的各种开源应用程序

1:只要遵循这些建议不能保证电池将耗尽。还有更多的因素来考虑。

After searching a lot on Internet we have came to one conclusion in order to ensure an persist xmpp connection with xmpp server we have to create an service,

We have created one which uses aSmack library to connect with xmpp server and it is working fine with mobile and wifi network.

Every time you make something design approach always matter!!!, aSmack have this reconnection mechanism already implicitly implemented in there library which listen to connection and if connection drops aSmack try to reconnect with xmpp server at some interval of time.

Our use case scenario::
INTERNET connectivity can be because of wifi or data network,here if connection go is ideal state of someone turn off screen cpu goes to sleep now any data is sent to server on this connection there will be no response because server is no more listening to client ,on client side XMPP connection is already in connected mode connection listener is not detecting any disconnection from server,so here flow gets complected.

After searching on INTERNET we found that possible solution to solve this is to ping server after a fix (we are using 1 min as fix period),after ping fail detected ,we have implemented reconnection mechanism same as smack(idea taken from aSmack reconnection mechanism itself)by making use of timer task.

Problem:: only problem we have is bratty draining ,if user is still connected with INTERNET and reconnection interval increases it will drain batty.

1). What is the possible solution of above problem?

2). Should we have to take another approach?

解决方案

How To Create Service In Android Which Makes Persist Xmpp Connection With XMPP Server?

Two things

  1. Reestablish the connection, by listening for CONNECTIVITY_CHANGED intents and determine if the currently used data connection went down (and was replaced by another).
  2. Ensure that the connection is established by pinging the server

Remarks about

  1. Listening for CONNECTIVY_CHANGED is not enough, you need to compare the previously active connection with the now active one. And if it's not the same, re-establish the XMPP connection.
  2. Smack 4.1 comes with ServerPingWithAlarmManager, which will check if a ping is required according to the settings of PingManager every 30 mintues. This value is hardcoded an can not be changed.

Using 1 minute as Ping interval is way to much! As you have experienced, it will drain you battery very fast. A reasonable ping interval is something > 15 minutes and I recommend 30 minutes. Smack 4.1 will also ensure that a ping is only send if there was no received stanza withing the Ping interval.

Also use XEP-0198: Stream Management when possible.

I recommend looking at the various open source apps that follow these guidelines and achieve a stable, permanent connection without draining the users battery1.

1: Just following these advises can not guarantee that the battery will drained. There are more factors to take into consideration.

这篇关于如何创建服务在Android中,这使得坚持XMPP连接使用XMPP服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆