XMPP IM 长连接 连接状态维护 和断线重连和单点登录 讲解(4)

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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值