Linux下搭建emqtt服务器

1.emqx安装(linux)

参考:https://developer.emqx.io/docs/emq/v3/cn/install.html

wget http://www.emqtt.com/downloads/3104/ubuntu16_04-deb
mv ubuntu16_04-deb emqx-ubuntu16.04-v3.1-rc.1_amd64.deb
sudo dpkg -i emqx-ubuntu16.04-v3.1-rc.1_amd64.deb

配置文件

EMQ X 配置文件: /etc/emqx/emqx.conf,插件配置文件: /etc/emqx/plugins/*.conf。

日志文件

日志文件目录: /var/log/emqx

数据文件

数据文件目录:/var/lib/emqx/

2.启动停止

service emqx start|stop|restart

3.运行监听程序

import paho.mqtt.client as mqtt


def on_connect(client, userdata, flags, rc):
    print("Connected with result code " + str(rc))
    client.subscribe("chat")


def on_message(client, userdata, msg):
    # 编写逻辑
    print("message received:")
    print(msg.topic + " " + ":" + str(msg.payload))
    print("-------------")


client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("0.0.0.0", 1883, 60)
client.loop_forever()

4.发送测试程序

import paho.mqtt.client as mqtt
import time

HOST = "129.211.12.227"
PORT = 1883


def test():
    client = mqtt.Client()
    client.connect(HOST, PORT, 60)
    i = 0
    while True:
        client.publish("chat", "hello "+str(i), 2)
        i+=1
        time.sleep(1)
    client.loop_forever()


if __name__ == '__main__':
    test()

猜你喜欢

转载自blog.csdn.net/GroundWalker/article/details/89239661