mqtt即Message Queue Telemetry Transport,是基于服务器端、客户端的一个消息传输协议,那么自然分为服务器端和客户端,github主页:
https://github.com/mqtt/mqtt.github.io/wiki/software?id=software,这里有很多服务器端、客户端的实现。
1. 服务器端mosquitto
mosquitto源代码可以从这里下载: http://mosquitto.org/download/
代码下载下来之后直接make,出现下面错误:
./mosquitto_internal.h:40:20: fatal error: ares.h: No such file or directory
网上查找是因为缺少lib库的原因,使用命令sudo apt-get install libc-ares2 libc-ares-dev来安装,再次make。
还是有错误:
read_handle_server.c:31:25: fatal error: uuid/uuid.h: No such file or directory
应该还是缺少库的原因,使用命令sudo apt-get install uuid-dev来安装,make。
OK,编译完成,然后运行命令sudo make install来安装我们编译好的程序。
安装完成之后,首先是启动mqtt服务:
mosquitto -c /etc/mosquitto/mosquitto.conf -d
注:由于安装完mosquitto之后在/etc/mosquitto目录下并不存在mosquitto.conf文件,所以我直接将该目录下的mosquitto.conf.example拷贝一份并重命名为mosquitto.conf,当然也可以直接指定文件为mosquitto.conf.example。
然后可以使用mosquitto_sub、mosquitto_pub去订阅、发布消息,首先启动一个终端来订阅消息,例如:
mosquitto_sub -t mqtt
注意,订阅、发布消息都需要指定主题,这里的主题指定为mqtt。
然后再启动一个终端来发布消息,例如:
mosquitto_pub -t mqtt -m "hello world"
那么在消息订阅的终端就会打印出"hello world"这个消息。
2. 客户端libemqtt
从这里clone代码: https://github.com/menudoproblema/libemqtt
然后直接进入到libemqtt目录去make,有一个error信息:
src/libemqtt.c:81:10: error: conflicting types for ‘mqtt_parse_msg_id’
include/libemqtt.h:117:9: note: previous declaration of ‘mqtt_parse_msg_id’ was here
make: *** [libemqtt.o] Error 1
发现是声明的类型不匹配,将include/libemqtt.h修改如下:
-uint8_t mqtt_parse_msg_id(const uint8_t* buf);
+uint16_t mqtt_parse_msg_id(const uint8_t* buf);
然后再make,在client目录中生成了两个文件pub和sub,分别对应publish和subscribe。
注意这里只是客户端代码,需要与服务器端配合来使用,比如上面的mosquitto。
那么启动上面的mosquitto服务,然后运行mosquitto_sub -t hello/emqtt命令来订阅主题为"hello/emqtt"的消息,启动另一个窗口来运行client目录下的pub程序,订阅消息这边的终端输出如下:
Example: QoS 0
Example: QoS 1
Example: QoS 2
说明通信是成功的,注意主题一定要一致,否则会接收不到消息。
客户端发布消息测试是成功的,那么我们再来试试订阅消息,直接运行client目录下的sub程序,这个程序有一个while死循环,会一直读取消息,启动另一个窗口,使用mosquitto_pub命令来发布消息,命令如下:
mosquitto_pub -t public/test/topic -m "hello world"
同样主题一定要一致,运行sub程序的终端也如愿输出了以下信息:
Packet Header: 0x30...
public/test/topic hello world
1. 服务器端mosquitto
mosquitto源代码可以从这里下载: http://mosquitto.org/download/
代码下载下来之后直接make,出现下面错误:
./mosquitto_internal.h:40:20: fatal error: ares.h: No such file or directory
网上查找是因为缺少lib库的原因,使用命令sudo apt-get install libc-ares2 libc-ares-dev来安装,再次make。
还是有错误:
read_handle_server.c:31:25: fatal error: uuid/uuid.h: No such file or directory
应该还是缺少库的原因,使用命令sudo apt-get install uuid-dev来安装,make。
OK,编译完成,然后运行命令sudo make install来安装我们编译好的程序。
安装完成之后,首先是启动mqtt服务:
mosquitto -c /etc/mosquitto/mosquitto.conf -d
注:由于安装完mosquitto之后在/etc/mosquitto目录下并不存在mosquitto.conf文件,所以我直接将该目录下的mosquitto.conf.example拷贝一份并重命名为mosquitto.conf,当然也可以直接指定文件为mosquitto.conf.example。
然后可以使用mosquitto_sub、mosquitto_pub去订阅、发布消息,首先启动一个终端来订阅消息,例如:
mosquitto_sub -t mqtt
注意,订阅、发布消息都需要指定主题,这里的主题指定为mqtt。
然后再启动一个终端来发布消息,例如:
mosquitto_pub -t mqtt -m "hello world"
那么在消息订阅的终端就会打印出"hello world"这个消息。
2. 客户端libemqtt
从这里clone代码: https://github.com/menudoproblema/libemqtt
然后直接进入到libemqtt目录去make,有一个error信息:
src/libemqtt.c:81:10: error: conflicting types for ‘mqtt_parse_msg_id’
include/libemqtt.h:117:9: note: previous declaration of ‘mqtt_parse_msg_id’ was here
make: *** [libemqtt.o] Error 1
发现是声明的类型不匹配,将include/libemqtt.h修改如下:
-uint8_t mqtt_parse_msg_id(const uint8_t* buf);
+uint16_t mqtt_parse_msg_id(const uint8_t* buf);
然后再make,在client目录中生成了两个文件pub和sub,分别对应publish和subscribe。
注意这里只是客户端代码,需要与服务器端配合来使用,比如上面的mosquitto。
那么启动上面的mosquitto服务,然后运行mosquitto_sub -t hello/emqtt命令来订阅主题为"hello/emqtt"的消息,启动另一个窗口来运行client目录下的pub程序,订阅消息这边的终端输出如下:
Example: QoS 0
Example: QoS 1
Example: QoS 2
说明通信是成功的,注意主题一定要一致,否则会接收不到消息。
客户端发布消息测试是成功的,那么我们再来试试订阅消息,直接运行client目录下的sub程序,这个程序有一个while死循环,会一直读取消息,启动另一个窗口,使用mosquitto_pub命令来发布消息,命令如下:
mosquitto_pub -t public/test/topic -m "hello world"
同样主题一定要一致,运行sub程序的终端也如愿输出了以下信息:
Packet Header: 0x30...
public/test/topic hello world