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 plain  copy
  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 plain  copy
  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/

猜你喜欢

转载自blog.csdn.net/u013362412/article/details/80279560