1. 安装emqx?下载 EMQ X、HStreamDB、Neuron、NanoMQ | EMQ
2. 进入bin目录运行.\emqx start 或者 .\emqx console,这样emqx 服务就启动了默认
ip'127.0.0.1', 1883端口
3. 编写订阅功能
import paho.mqtt.client as mqtt
def on_connect(client, userdata, flags, rc):
print("Connected with result code: " + str(rc))
def on_message(client, userdata, msg):
print(msg.topic + " " + str(msg.payload))
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect('127.0.0.1', 1883, 600) # 600为keepalive的时间间隔
client.subscribe('fifa', qos=0)
client.loop_forever() # 保持连接
4. 编写发布功能
import paho.mqtt.client as mqtt
def on_connect(client, userdata, flags, rc):
print("Connected with result code: " + str(rc))
def on_message(client, userdata, msg):
print(msg.topic + " " + str(msg.payload))
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect('127.0.0.1', 1883, 600) # 600为keepalive的时间间隔
client.publish('fifa', payload=str({"a":4}), qos=0)
运行右键运行发布者文件
?
?
出现0链接成功
5.右键发送消息这文件则,所有订阅者都会收到消息?
?
?
?
|