XMPP连接保持使用的服务,让我的XMPP连接活跃在这里任何时候都得到断开的Android
IM是代码:XMPP连接保持使用的服务,让我的XMPP连接活跃在这里任何时候都得到断开的Android
public class XMPPService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
new ConnectionStatus().execute();
return START_STICKY;
}
@Override
public void onCreate() {
Log.i("service", "created");
}
public class ConnectionStatus extends AsyncTask{
@Override
protected Object doInBackground(Object[] params) {
XMPPClient.getConnection().addConnectionListener(
new AbstractConnectionListener() {
public void connectionClosed() {
Log.i("connection", "closed");
}
public void connectionClosedOnError(Exception e) {
Log.i("connection", "closed on error");
}
public void reconnectionFailed(Exception e) {
Log.i("reconnection", "failed");
}
public void reconnectionSuccessful() {
if (XMPPClient.getConnection().isAuthenticated()) {
Log.i("isauthenticauted : ", String.valueOf(XMPPClient.getConnection().isAuthenticated()));
Log.i("reconnection", "succesful");
} else {
try {
XMPPClient.getConnection().login(new TinyDB(getApplicationContext()).getString("username"), new TinyDB(getApplicationContext()).getString("password"));
} catch (XMPPException e) {
e.printStackTrace();
} catch (SmackException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Log.i("reconnection", "succesful");
}
}
public void reconnectingIn(int seconds) {
Log.i("reconnectingIn", String.valueOf(seconds));
}
}
);
return null;
}
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
}
但连接不断定期和我连接断开得到这样的:
org.jivesoftware.smack.SmackException: Parser got END_DOCUMENT event. This could happen e.g. if the server closed the connection without sending a closing stream element
at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader.parsePackets(XMPPTCPConnection.java:1148)
at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader.access$200(XMPPTCPConnection.java:937)
at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader$1.run(XMPPTCPConnection.java:952)
at java.lang.Thread.run(Thread.java:818)
之后,它开始重新连接到服务器上,然后我得到这个:
09-07 17:56:03.916 17754-20996/com.sports.unity D/SMACK﹕ SENT (0): `09-07 17:56:04.217 17754-20997/com.sports.unity D/SMACK﹕ RECV (0): <?xml version='1.0'?><stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' id='2025993121' from='mm.io' version='1.0' xml:lang='en'><stream:features><c xmlns='http://jabber.org/protocol/caps' hash='sha-1' node='http://www.process-one.net/en/ejabberd/' ver='Kyn00yB1iXiJLUJ0gVvn7tZREMg='/><register xmlns='http://jabber`
如何保持一个稳定的conenction到XMPP服务器
当你的客户端的空闲大于空闲时间,这基本上发生(“空闲连接策略”)在您server.For指定这一点,你需要在您的smack/asmack库中实现XMPP Ping类,其中您必须发送XMPP Pong作为对服务器ping的答复。 而且您还可以实施XMPP重新连接类。
是否意味着我们必须定期保持ping服务器的连接状态? –
您可以增加闲置连接策略下的“闲置连接时间”,并在该时间段内用pong回应,或者将客户端连接设置为“不要断开闲置的客户端”,这将使您的客户端保持活动状态,直到您手动断开连接。保持客户端存活也有缺点,例如由于存在错误(即在线和离线)而造成的信息丢失。 – Deepesh
@Deepesh - 我也在研究一个聊天应用程序,并面临同样的问题。你在这里提到的关于“不要断开闲置客户端”的东西。我该如何做到这一点? –
在服务器端更改属性设置客户端间隔从不断开 –
它已完成但它仍然保持下降和dosent完全连接。 –
@anupamx你有解决方案吗? –