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


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

2、安裝OpenFire登陸到控制台

   這里設置多長時間關閉閑置連接,可以判斷用戶是否在線的最長反應時間

3、創建兩個測試賬號,先用Spark登陸一個賬號

4、手機端登陸,使用Service保持連接,並與spark端發送消息,實現雙向通信(代碼和程序在后面)

5、關鍵代碼

配置連接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失敗后啟動,重連成功后關閉

 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

菜鳥一枚,共同學習~~

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM