docker中安装mqtt服务端 - 炫Q香蕉派

docker中安装mqtt服务端

MQTT(Message Queuing Telemetry Transport,消息队列遥测传输)是IBM开发的一个即时通讯协议,有可能成为物联网的重要组成部分。
mqtt.jpg

首先创建实例
docker run -ti -h mqtt --name mqtt_1884 -p 21884:1884 ubuntu:14.04
然后更新
apt-get update
apt-get install libc-ares-dev uuid-dev wget make libssl-dev g++
wget http://mosquitto.org/files/source/mosquitto-1.4.14.tar.gz
tar zxfv mosquitto-1.4.14.tar.gz
cd mosquitto-1.4.14
make
make install
然后添加用户
adduser mosquitto
cp /etc/mosquitto/mosquitto.conf.example /etc/mosquitto/mosquitto.conf
vi /etc/mosquitto/mosquitto.conf
去掉端口的注释,改为1884
mosquitto -v -c /etc/mosquitto/mosquitto.conf

测试,接收端:

import paho.mqtt.client as mqtt

def on_connect(client, userdata, flags, rc):
  client.subscribe("MikuQ.com/#")

def on_message(client, userdata, msg):
  print msg.payload.decode("utf-8")

client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("192.168.1.1", 21884, 50)
client.loop_forever()

测试,发送端:

import paho.mqtt.client as mqtt

client = mqtt.Client()
client.connect("192.168.1.1", 21884, 50)
client.publish("MikuQ.com/android/command", "MQTT")

参考了酋猫的例子。

标签: none

添加新评论