smack 源碼分析一(android上實現長連接)【1】


前段時間應一個項目需求: 要求給終端短信, 聯繫人信息做一個雲存儲雲備份及雲端遠程控制終端並且雲端能夠推送消息到終端的需求. 這需要在終端與雲端建立一個長連接以便雲端消息能及時推送到終端. 所以項目中用到了smack框架.  smack功能強大, 遠不止本文所寫的這點內容. 現在我只將對smack的理解以及項目中對smack的使用心得總結並記錄下來, 一則給大家分享 , 二則也算是一個技術經驗的累積. 但基於我混亂的表達能力和可能的理解上的偏差可能會有些錯誤. 歡迎各位大蝦大牛拍磚. 

項目中用到smack的長連接這塊關鍵有以下這幾個類: Connection , XMPPConnection , PakcetWriter , PacketReader. 如下圖: 

查看XMPPConnection源碼:

[java] view plaincopy
  1. protected void connectUsingConfiguration(ConnectionConfiguration config) throws XMPPException {  
  2.         String host = config.getHost();  
  3.         int port = config.getPort();  
  4.         try {  
  5.   
  6.             if (this.socket == null || this.socket.isClosed()) {  
  7.   
  8.                 LogUtil.i(TAG, "this.socket == null || this.socket.isClosed()");  
  9.                 if (config.getSocketFactory() == null) {  
  10.                     this.socket = new Socket(host, port);  
  11.                 } else {  
  12.                     this.socket = config.getSocketFactory().createSocket(host, port);  
  13.                 }  
  14.             }  




可以看到, connectUsingConfiguration(ConnectionConfiguration config) 是長連接入口,
傳遞config制定服務器ipAddress和port , 然後根據config創建一個socket對象. 緊接着調用initConnection()方法, 而在initConnection()中: 

[java] view plaincopy
  1. private void initConnection() throws XMPPException {  
  2.   
  3.             usingCompression = false;  
  4.   
  5.         // Set the reader and writer instance variables  
  6.         initReaderAndWriter();  
  7.   
  8.         try {   
  9. //            if (isFirstInitialization) {  
  10.                 packetWriter = new PacketWriter(this);  
  11.                 packetReader = new PacketReader(this);  
  12.   
  13.                 // If debugging is enabled, we should start the thread that will  
  14.                 // listen for  
  15.                 // all packets and then log them.  
  16.                 if (config.isDebuggerEnabled()) {  
  17.                     addPacketListener(debugger.getReaderListener(), null);  
  18.                     if (debugger.getWriterListener() != null) {  
  19.                         addPacketSendingListener(debugger.getWriterListener(), null);  
  20.                     }  
  21.                 }  
  22. //            } else {  
  23. //                packetWriter.init();  
  24. //                packetReader.init();  
  25. //            }  
  26.   
  27.             // Start the packet writer. This will open a XMPP stream to the  
  28.             // server  
  29.             packetWriter.startup();  
  30.             // Start the packet reader. The startup() method will block until we  
  31.             // get an opening stream packet back from server.  
  32.             packetReader.startup();  
  33.   
  34.             // Make note of the fact that we're now connected.  
  35.             connected = true;  

以上執行順序是

1. initReaderAndWriter()方法通過上面創建的socket實例化connection

的Writer和Reader.


2. 實例化PacketWriter和PacketReader對象.

PacketWriter就是向服務器發送數據發送心跳包一直保持與服務器的連接連. PacketReader則是不斷的讀取並且解析服務器推送的消息.


3. 分別調用packetWriterpacketReader的startup()方法.

至此整個連接過程就建立完成了.



   長連接大致流程基本就這樣, 也許還是一頭霧水.  

1.終端如何發生心跳包與服務器一直保持聯繫的呢.

2.服務器推送消息到終端, 終端是如何根據消息進行分發處理的呢. 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章