MQTT協議的簡單介紹和服務器的安裝

最近公司做的項目中有用到消息推送,經過多方面的篩選之後確定了使用MQTT協議,相對於XMPP,MQTT更加輕量級,並且佔用用戶很少的帶寬。

MQTT是IBM推出的一種針對移動終端設備的基於TCP/IP的發佈/預訂協議,可以連接大量的遠程傳感器和控制設備。

MQTT的官網見:http://mqtt.org/。其中http://mqtt.org/software裏面提供了官方推薦的各種服務器和客戶端使用的各種語言版本的API。

下面以服務器Apollo 1.6爲例,之前嘗試過使用ActiveMQ,效果很不理想,只能實現服務器和客戶端一對一的通信,從官網上瞭解到Apollo屬於activemq的一個子工程。先不管這些了,言歸正傳,以下在windows環境下。

1、在這裏下載Apollo服務器,下載後解壓,然後運行apache-apollo-1.6\bin\apollo.cmd,輸入create mybroker(名字任意取,這裏是根據官網介紹的來取的)創建服務器實例,服務器實例包含了所有的配置,運行時數據等,並且和一個服務器進程關聯。

2、create mybroker之後會在bin目錄下生成mybroker文件夾,裏面包含有很多信息,其中etc\apollo.xml文件下是配置服務器信息的文件,etc\users.properties文件包含連接MQTT服務器時用到的用戶名和密碼,後面會介紹,可以修改原始的admin=password,可以接着換行添加新的用戶名密碼。

3、打開cmd,運行…apache-apollo-1.6\bin\mybroker\bin\apollo-broker.cmd run 開啓服務器,可以在瀏覽器中輸入http://127.0.0.1:61680/查看是否安裝成功,該界面展示了topic,連接數等很多信息。

經過上面的簡單步驟,服務器基本上就已經完成,下一篇將介紹Android客戶端的編寫和注意事項。

客戶端使用的API,開始我使用的是mqtt-client,使用過後發現問題百出,不能很好的滿足要求,後來使用了官方推薦的Eclipse Paho,下面開始客戶端代碼的編寫,爲了方便測試這裏有android和j2se兩個工程:

1、新建android工程MQTTClient

2、MainActivity代碼如下:

[java] view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. package ldw.mqttclient;  
  2.   
  3. import java.util.concurrent.Executors;  
  4. import java.util.concurrent.ScheduledExecutorService;  
  5. import java.util.concurrent.TimeUnit;  
  6.   
  7. import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;  
  8. import org.eclipse.paho.client.mqttv3.MqttCallback;  
  9. import org.eclipse.paho.client.mqttv3.MqttClient;  
  10. import org.eclipse.paho.client.mqttv3.MqttConnectOptions;  
  11. import org.eclipse.paho.client.mqttv3.MqttException;  
  12. import org.eclipse.paho.client.mqttv3.MqttMessage;  
  13. import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;  
  14.   
  15. import android.app.Activity;  
  16. import android.os.Bundle;  
  17. import android.os.Handler;  
  18. import android.os.Message;  
  19. import android.view.KeyEvent;  
  20. import android.widget.TextView;  
  21. import android.widget.Toast;  
  22.   
  23. public class MainActivity extends Activity {  
  24.   
  25.     private TextView resultTv;  
  26.   
  27.     private String host = "tcp://127.0.0.1:1883";  
  28.     private String userName = "admin";  
  29.     private String passWord = "password";  
  30.   
  31.     private Handler handler;  
  32.   
  33.     private MqttClient client;  
  34.   
  35.     private String myTopic = "test/topic";  
  36.   
  37.     private MqttConnectOptions options;  
  38.   
  39.     private ScheduledExecutorService scheduler;  
  40.   
  41.     @Override  
  42.     protected void onCreate(Bundle savedInstanceState) {  
  43.         super.onCreate(savedInstanceState);  
  44.         setContentView(R.layout.main);  
  45.   
  46.         resultTv = (TextView) findViewById(R.id.result);  
  47.   
  48.         init();  
  49.   
  50.         handler = new Handler() {  
  51.             @Override  
  52.             public void handleMessage(Message msg) {  
  53.                 super.handleMessage(msg);  
  54.                 if(msg.what == 1) {  
  55.                     Toast.makeText(MainActivity.this, (String) msg.obj,  
  56.                             Toast.LENGTH_SHORT).show();  
  57.                     System.out.println("-----------------------------");  
  58.                 } else if(msg.what == 2) {  
  59.                     Toast.makeText(MainActivity.this"連接成功", Toast.LENGTH_SHORT).show();  
  60.                     try {  
  61.                         client.subscribe(myTopic, 1);  
  62.                     } catch (Exception e) {  
  63.                         e.printStackTrace();  
  64.                     }  
  65.                 } else if(msg.what == 3) {  
  66.                     Toast.makeText(MainActivity.this"連接失敗,系統正在重連", Toast.LENGTH_SHORT).show();  
  67.                 }  
  68.             }  
  69.         };  
  70.   
  71.         startReconnect();  
  72.   
  73.     }  
  74.   
  75.     private void startReconnect() {  
  76.         scheduler = Executors.newSingleThreadScheduledExecutor();  
  77.         scheduler.scheduleAtFixedRate(new Runnable() {  
  78.   
  79.             @Override  
  80.             public void run() {  
  81.                 if(!client.isConnected()) {  
  82.                     connect();  
  83.                 }  
  84.             }  
  85.         }, 0 * 100010 * 1000, TimeUnit.MILLISECONDS);  
  86.     }  
  87.   
  88.     private void init() {  
  89.         try {  
  90.                        //host爲主機名,test爲clientid即連接MQTT的客戶端ID,一般以客戶端唯一標識符表示,MemoryPersistence設置clientid的保存形式,默認爲以內存保存  
  91.             client = new MqttClient(host, "test",  
  92.                     new MemoryPersistence());  
  93.                        //MQTT的連接設置  
  94.             options = new MqttConnectOptions();  
  95.                        //設置是否清空session,這裏如果設置爲false表示服務器會保留客戶端的連接記錄,這裏設置爲true表示每次連接到服務器都以新的身份連接  
  96.             options.setCleanSession(true);  
  97.                        //設置連接的用戶名  
  98.             options.setUserName(userName);  
  99.                        //設置連接的密碼  
  100.             options.setPassword(passWord.toCharArray());  
  101.             // 設置超時時間 單位爲秒  
  102.             options.setConnectionTimeout(10);  
  103.             // 設置會話心跳時間 單位爲秒 服務器會每隔1.5*20秒的時間向客戶端發送個消息判斷客戶端是否在線,但這個方法並沒有重連的機制  
  104.             options.setKeepAliveInterval(20);  
  105.                         //設置回調  
  106.             client.setCallback(new MqttCallback() {  
  107.   
  108.                 @Override  
  109.                 public void connectionLost(Throwable cause) {  
  110.                                         //連接丟失後,一般在這裏面進行重連  
  111.                     System.out.println("connectionLost----------");  
  112.                 }  
  113.   
  114.                 @Override  
  115.                 public void deliveryComplete(IMqttDeliveryToken token) {  
  116.                                         //publish後會執行到這裏  
  117.                     System.out.println("deliveryComplete---------"  
  118.                             + token.isComplete());  
  119.                 }  
  120.   
  121.                 @Override  
  122.                 public void messageArrived(String topicName, MqttMessage message)  
  123.                         throws Exception {  
  124.                                         //subscribe後得到的消息會執行到這裏面  
  125.                     System.out.println("messageArrived----------");  
  126.                     Message msg = new Message();  
  127.                     msg.what = 1;  
  128.                     msg.obj = topicName+"---"+message.toString();  
  129.                     handler.sendMessage(msg);  
  130.                 }  
  131.             });  
  132. //          connect();  
  133.         } catch (Exception e) {  
  134.             e.printStackTrace();  
  135.         }  
  136.     }  
  137.   
  138.     private void connect() {  
  139.         new Thread(new Runnable() {  
  140.   
  141.             @Override  
  142.             public void run() {  
  143.                 try {  
  144.                     client.connect(options);  
  145.                     Message msg = new Message();  
  146.                     msg.what = 2;  
  147.                     handler.sendMessage(msg);  
  148.                 } catch (Exception e) {  
  149.                     e.printStackTrace();  
  150.                     Message msg = new Message();  
  151.                     msg.what = 3;  
  152.                     handler.sendMessage(msg);  
  153.                 }  
  154.             }  
  155.         }).start();  
  156.     }  
  157.   
  158.     @Override  
  159.     public boolean onKeyDown(int keyCode, KeyEvent event) {  
  160.         if(client != null && keyCode == KeyEvent.KEYCODE_BACK) {  
  161.             try {  
  162.                 client.disconnect();  
  163.             } catch (Exception e) {  
  164.                 e.printStackTrace();  
  165.             }  
  166.         }  
  167.         return super.onKeyDown(keyCode, event);  
  168.     }  
  169.   
  170.     @Override  
  171.     protected void onDestroy() {  
  172.         super.onDestroy();  
  173.         try {  
  174.             scheduler.shutdown();  
  175.             client.disconnect();  
  176.         } catch (MqttException e) {  
  177.             e.printStackTrace();  
  178.         }  
  179.     }  
  180. }  

由於項目需要,我用到了心跳重連。根據這裏的解釋設置apollo.xml,主要有設置主機連接的地址。另外,options還有個setWill方法,如果項目中需要知道客戶端是否掉線可以調用該方法。

3、新建j2se工程MQTTServer

4、Server代碼如下:

[java] view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. import java.awt.Container;  
  2. import java.awt.event.ActionEvent;  
  3. import java.awt.event.ActionListener;  
  4.   
  5. import javax.swing.JButton;  
  6. import javax.swing.JFrame;  
  7. import javax.swing.JPanel;  
  8.   
  9. import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;  
  10. import org.eclipse.paho.client.mqttv3.MqttCallback;  
  11. import org.eclipse.paho.client.mqttv3.MqttClient;  
  12. import org.eclipse.paho.client.mqttv3.MqttConnectOptions;  
  13. import org.eclipse.paho.client.mqttv3.MqttDeliveryToken;  
  14. import org.eclipse.paho.client.mqttv3.MqttMessage;  
  15. import org.eclipse.paho.client.mqttv3.MqttTopic;  
  16. import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;  
  17.   
  18. public class Server extends JFrame {  
  19.     private static final long serialVersionUID = 1L;  
  20.     private JPanel panel;  
  21.     private JButton button;  
  22.   
  23.     private MqttClient client;  
  24.     private String host = "tcp://127.0.0.1:1883";  
  25. //  private String host = "tcp://localhost:1883";  
  26.     private String userName = "test";  
  27.     private String passWord = "test";  
  28.     private MqttTopic topic;  
  29.     private MqttMessage message;  
  30.   
  31.     private String myTopic = "test/topic";  
  32.   
  33.     public Server() {  
  34.   
  35.         try {  
  36.             client = new MqttClient(host, "Server",  
  37.                     new MemoryPersistence());  
  38.             connect();  
  39.         } catch (Exception e) {  
  40.             e.printStackTrace();  
  41.         }  
  42.   
  43.         Container container = this.getContentPane();  
  44.         panel = new JPanel();  
  45.         button = new JButton("發佈話題");  
  46.         button.addActionListener(new ActionListener() {  
  47.   
  48.             @Override  
  49.             public void actionPerformed(ActionEvent ae) {  
  50.                 try {  
  51.                     MqttDeliveryToken token = topic.publish(message);  
  52.                     token.waitForCompletion();  
  53.                     System.out.println(token.isComplete()+"========");  
  54.                 } catch (Exception e) {  
  55.                     e.printStackTrace();  
  56.                 }  
  57.             }  
  58.         });  
  59.         panel.add(button);  
  60.         container.add(panel, "North");  
  61.   
  62.     }  
  63.   
  64.     private void connect() {  
  65.   
  66.         MqttConnectOptions options = new MqttConnectOptions();  
  67.         options.setCleanSession(false);  
  68.         options.setUserName(userName);  
  69.         options.setPassword(passWord.toCharArray());  
  70.         // 設置超時時間  
  71.         options.setConnectionTimeout(10);  
  72.         // 設置會話心跳時間  
  73.         options.setKeepAliveInterval(20);  
  74.         try {  
  75.             client.setCallback(new MqttCallback() {  
  76.   
  77.                 @Override  
  78.                 public void connectionLost(Throwable cause) {  
  79.                     System.out.println("connectionLost-----------");  
  80.                 }  
  81.   
  82.                 @Override  
  83.                 public void deliveryComplete(IMqttDeliveryToken token) {  
  84.                     System.out.println("deliveryComplete---------"+token.isComplete());  
  85.                 }  
  86.   
  87.                 @Override  
  88.                 public void messageArrived(String topic, MqttMessage arg1)  
  89.                         throws Exception {  
  90.                     System.out.println("messageArrived----------");  
  91.   
  92.                 }  
  93.             });  
  94.   
  95.             topic = client.getTopic(myTopic);  
  96.   
  97.             message = new MqttMessage();  
  98.             message.setQos(1);  
  99.             message.setRetained(true);  
  100.             System.out.println(message.isRetained()+"------ratained狀態");  
  101.             message.setPayload("eeeeeaaaaaawwwwww---".getBytes());  
  102.   
  103.             client.connect(options);  
  104.         } catch (Exception e) {  
  105.             e.printStackTrace();  
  106.         }  
  107.   
  108.     }  
  109.   
  110.     public static void main(String[] args) {  
  111.         Server s = new Server();  
  112.         s.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  113.         s.setSize(600370);  
  114.         s.setLocationRelativeTo(null);  
  115.         s.setVisible(true);  
  116.     }  
  117. }  

上面代碼跟客戶端的代碼差不多,這裏就不做解釋了。

沒什麼好說的,MQTT就是這麼簡單,但開始在使用的時候要注意一些參數的設置來適應項目的需求。

jar包下載地址:
https://repo.eclipse.org/content/repositories/paho/org/eclipse/paho/mqtt-client/0.4.0/

轉自:http://www.longdw.com/mqtt-server-client-android/

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