ConnectionListener 连接状态监听器,监听Connet的各种状态的情况。
....接口代码片段
void connected(XMPPConnection var1);
void authenticated(XMPPConnection var1);
void connectionClosed();
void connectionClosedOnError(Exception var1);
void reconnectingIn(int var1);
void reconnectionSuccessful();
void reconnectionFailed(Exception var1);
...
connected连接成功
authenticated 认证成功。
connectionClosed 连接已关闭
connectionClosedOnError 连接发生错误,主要是断网或者别别人抢占了连接等情况会产生
reconnectingIn 重连中
reconnectionSuccessful 重连成功
reconnectionFailed 重连失败
根据此方法简单实现一个实例,上代码
@Override
public void authenticated(XMPPConnection arg0) {
VPLog.i(TAG, "authenticated");
isLogin = true;
welcome();
}
@Override
public void connected(XMPPConnection arg0) {
VPLog.i(TAG, "connected");
}
@Override
public void connectionClosed() {
VPLog.i(TAG, "connectionClosed");
isLogin = false;
}
@Override
public void connectionClosedOnError(Exception arg0) {
VPLog.i(TAG, "connectionClosedOnError");
//这里就是网络不正常或者被挤掉断线激发的事件
if(arg0.getMessage()!=null && arg0.getMessage().contains("conflict")){ //被挤掉线
VPLog.e(TAG,"非正常关闭异常:"+arg0.getMessage());
//VPLog.e(TAG,"非正常关闭异常:"+arg0);
arg0.printStackTrace();
isQuit = true;
mhHandler.removeMessages(SEND_LOGIN_REQUEST);
//logout();
if (mConnection!=null) {
try {
mConnection.disconnect();
mExecutorService.shutdownNow();//立即停止
} catch (Exception e) {
e.printStackTrace();
}
}
Intent intent = new Intent(VpApplication.getInstance(),ConfictLoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
stopSelf();
}
//isLogin = false;
}
@Override
public void reconnectingIn(int arg0) {
VPLog.i(TAG, "reconnectingIn");
if (!isLogin &&!isQuit) {//没有登录
login();//去登陆
}
}
@Override
public void reconnectionFailed(Exception arg0) {
VPLog.i(TAG, "reconnectionFailed :" +arg0);
}
@Override
public void reconnectionSuccessful() {
VPLog.i(TAG, "reconnectionSuccessful");
isLogin = true;
mExecutorService.execute(new Runnable() {
@Override
public void run() {
offlineManager();
}
});
}
以上对各个状态进行了一些简单的业务逻辑的处理。
关于被挤掉线的异常 arg0.getMessage().contains(“conflict”)
问: 1.什么情况才能出现
要想做到类似微信单点登录的方法,xmpp只需要配置RESOURCE = “mobile”;
resource表示了从哪里登入,只需要将resource设置相同,则服务器就会干掉以前的连接。
public void connectionClosedOnError(Exception arg0) {
VPLog.i(TAG, "connectionClosedOnError");
//这里就是网络不正常或者被挤掉断线激发的事件
if(arg0.getMessage()!=null && arg0.getMessage().contains("conflict")){ //被挤掉线
VPLog.e(TAG,"非正常关闭异常:"+arg0.getMessage());
//VPLog.e(TAG,"非正常关闭异常:"+arg0);
arg0.printStackTrace();
isQuit = true;
mhHandler.removeMessages(SEND_LOGIN_REQUEST);
//logout();
if (mConnection!=null) {
try {
mConnection.disconnect();
mExecutorService.shutdownNow();//立即停止
} catch (Exception e) {
e.printStackTrace();
}
}
Intent intent = new Intent(VpApplication.getInstance(),ConfictLoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
stopSelf();
}
//isLogin = false;
}
本文介绍了使用ConnectionListener接口来监听XMPP连接的各种状态,并通过示例代码详细展示了如何处理连接成功、认证成功、连接关闭及重连等事件。特别针对被挤下线的情况进行了深入分析,提供了一种实现单点登录的方法。
2287

被折叠的 条评论
为什么被折叠?



