Smack+OpenFire搭建IM通訊,包含心跳和自動重連(Android實現)

Smack是一個開源,易於使用的XMPP(jabber)客戶端類庫。優勢:簡單的,功能強大,給用戶發送信息只需三行代碼即可完成。缺點:API並不是爲大量併發用戶設計,每一個客戶要1個線程,佔用資源大。
OpenFire是開源的、基於可拓展通信和表示協議(XMPP)、採用Java編程語言開發的實時協做服務器。 Openfire安裝和使用都很是簡單,並利用Web進行管理。單臺服務器可支持上萬併發用戶。
一、首先到網址 http://www.igniterealtime.org 下載OpenFire服務器和Smack jar包
git

二、安裝OpenFire登錄到控制檯
github

   這裏設置多長時間關閉閒置鏈接,能夠判斷用戶是否在線的最長反應時間編程

三、建立兩個測試帳號,先用Spark登錄一個帳號
服務器

四、手機端登錄,使用Service保持鏈接,並與spark端發送消息,實現雙向通訊(代碼和程序在後面)
網絡

五、關鍵代碼併發

配置鏈接OpenFire服務器,鏈接成功後設置響應Linstener和Receiver,這裏因業務需求設置ping間隔爲10s編程語言

 1     public void connect() {
 2         Log.i(TAG, "connect()");
 3         XMPPTCPConnectionConfiguration.Builder configBuilder = XMPPTCPConnectionConfiguration.builder();
 4         configBuilder.setHost(SmackConst.XMPP_HOST);
 5         configBuilder.setServiceName(SmackConst.SERVICE_NAME);
 6         configBuilder.setUsernameAndPassword(mUsername, mPassword);
 7         configBuilder.setSecurityMode(SecurityMode.disabled);
 8         mConnection = new XMPPTCPConnection(configBuilder.build());
 9         //Set ConnectionListener here to catch initial connect();
10         mConnection.addConnectionListener(this);
11         try {
12             mConnection.connect();
13             mConnection.login();
14             if(mConnection.isAuthenticated()){//登陸成功
15                 MyPingManager.setDefaultPingInterval(10);//Ping every 10 seconds
16                 MyPingManager myPingManager = MyPingManager.getInstanceFor(mConnection);
17                 //Set PingListener here to catch connect status
18                 myPingManager.registerPingFailedListener(SmackConnection.this);
19                 setupSendMessageReceiver();
20                 //Set ChatListener here to catch receive message and send NEW_MESSAGE broadcast
21                 ChatManager.getInstanceFor(mConnection).addChatListener(this);
22                 //Set ChatListener here to catch roster change and rebuildRoster
23                 //Roster.getInstanceFor(mConnection).addRosterListener(this);
24                 sendLoginBroadcast(true);
25             }else{
26                 mConnection.disconnect();
27                 Log.i(TAG, "Authentication failure");
28                 sendLoginBroadcast(false);
29             }
30         } catch (Exception e) {
31             e.printStackTrace();
32             sendLoginBroadcast(false);
33             Intent intent = new Intent(mService, mService.getClass());
34             mService.stopService(intent);
35         }
36 
37     }

 自動重連TimerTask,Ping失敗後啓動,重連成功後關閉ide

 1     private Timer reConnectTimer;
 2     private int delay = 10000;
 3     //pingFailed時啓動重連線程
 4     class ReConnectTimer extends TimerTask {  
 5         @Override  
 6         public void run() {
 7             // 無網絡鏈接時,直接返回
 8             if (getNetworkState(mService) == NETWORN_NONE) {
 9                 Log.i(TAG, "無網絡鏈接,"+delay/1000+"s後從新鏈接");
10                 reConnectTimer.schedule(new ReConnectTimer(), delay);
11                 //reConnectTimer.cancel();
12                 return;
13             }
14             // 鏈接服務器 
15             try {
16                 mConnection.connect();
17                 if(!mConnection.isAuthenticated()){
18                     mConnection.login();
19                     reConnectTimer.cancel();
20                 }
21                 Log.i(TAG, "重連成功");
22                 Intent intent = new Intent(SmackConst.ACTION_RECONNECT_SUCCESS);
23                 mService.sendBroadcast(intent);
24             } catch (Exception e) {
25                 Log.i(TAG, "重連失敗,"+delay/1000+"s後從新鏈接");
26                 e.printStackTrace();
27                 reConnectTimer.schedule(new ReConnectTimer(), delay);
28             } 
29             
30         }  
31     }

資源地址:https://github.com/liuhaijin/Smack-Openfire學習

菜鳥一枚,共同窗習~~測試

相關文章
相關標籤/搜索