简体   繁体   English

如何在 windows 中的 docker 上测试 mqtt?

[英]How to test mqtt on docker in windows?

I have pulled the eclipse-mosquitto image on the docker.我在 docker 上提取了 eclipse-mosquitto 图像。 How can I test the mqtt client by subscribing and publishing some message through docker?如何通过 docker 订阅和发布一些消息来测试 mqtt 客户端?

Following are the details of my system:以下是我的系统的详细信息:

Operating System: Windows 10 Home操作系统:Windows 10

Docker version 19.03.1 Docker 版本 19.03.1

Can someone please guide me with the steps on how to test the mqtt on docker in windows 10?有人可以指导我如何在 windows 10 中测试 docker 上的 mqtt 的步骤吗?

Thank you谢谢

You can test using MQTT client docker container.您可以使用 MQTT 客户端 docker 容器进行测试。

  • server服务器

Start eclipse-mosquitto container启动eclipse-mosquitto容器

docker run --name mq -it -p 1883:1883 -p 9001:9001  eclipse-mosquitto

  • client:客户:

Then open another terminal window and run subscriber command using docker MQTT client然后打开另一个终端 window 并使用 docker MQTT 客户端运行订阅者命令

docker run --rm -it   --link mq   ruimarinho/mosquitto mosquitto_sub -h mq -t '#'

Now open another terminal window and publish a message, you will able to see message in window 2.现在打开另一个终端 window 并发布一条消息,您将能够在 window 2 中看到消息。

docker run --rm -it   --link mq   ruimarinho/mosquitto mosquitto_pub -h mq -t home-assistant/switch/1/on -m "Docker pub-subtest message"

在此处输入图像描述

You need to install an MQTT client that can do both Publish and Subscribe.您需要安装一个可以发布和订阅的 MQTT 客户端。 I tend to use the Node.JS MQTT module that includes a client that will do both.我倾向于使用 Node.JS MQTT 模块,它包含一个可以同时执行这两种操作的客户端。

You will need to install Node.JS first.您需要先安装 Node.JS。 Then do a:然后做一个:

> npm install mqtt

This will install the MQTT Node.JS module under the./node_modules directory in your current directory.这将在您当前目录的 ./node_modules 目录下安装 MQTT Node.JS 模块。

After this, you need to run a Subscriber and a Publisher.在此之后,您需要运行订阅者和发布者。 The Subscriber you run like so:您像这样运行的订阅者:

> node ./node_modules/mqtt/mqtt.js sub -t "my/local/#" -h "mqtt-broker-host.home.local" -vv

Where mqtt-broker-host.home.local is either the FQDN or the IP address of your MQTT Broker container.其中mqtt-broker-host.home.local是 MQTT Broker 容器的 FQDN 或 IP 地址。

In another terminal session, you can run MQTT publishers:在另一个终端 session 中,可以运行 MQTT 发布者:

> node ./node_modules/mqtt/mqtt.js pub -t "my/local/test-topic" -h "mqtt-broker-host.home.local' -m '{"msg": "This is a test" }' 

In your 'sub' terminal you should now see the "This is a test" message if everything is working correctly.如果一切正常,您现在应该在您的“子”终端中看到“这是一个测试”消息。 If not, look in the 'pub' terminal session for any error messages.如果没有,请在“pub”终端 session 中查看任何错误消息。 If there is none, then you should look at your MQTT Broker log file to see what it thinks is going one.如果没有,那么您应该查看您的 MQTT 代理日志文件,看看它认为会发生什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM