在我的app.js中:
var mqtt = require('mqtt')
var client = mqtt.connect('mqtt://localhost:1883')
topic = 'testTopic'
client.on('connect', ()=> {
client.subscribe(topic)
})
client.on('message', function (topic, message) {
console.log(message.toString())
})我连接了hivemq-cli和hivemq server,然后在Test1订阅中创建了一个新的主题testTopic,并让另一个订阅testTopic

在hivemq-cli中一切正常,在我的终端中:
testTopic3@localhost> sub -t testTopic -s
Hello
Hello
Hello
Hello
Hello
Hello
Hi
Hi
Hi但当我使用npm start时,我的web应用程序显示:火狐无法建立到服务器ws://localhost:1883/的连接,并且没有返回任何信息。
我遇到这个麻烦已经有一天了,所以我非常需要一些帮助。非常感谢!
发布于 2020-05-05 20:41:07
您正在web应用程序中使用websockets (ws://),它可能是与MQTT端口1883 (mqtt://)不同的端口。
发布于 2020-05-07 15:22:00
您必须在HiveMQ配置(conf/config.xml)中配置侦听器,并在连接客户端时使用正确的端口和路径。默认配置不包含WebSocket侦听程序。
示例配置:
<hivemq>
<listeners>
<!-- default configuration -->
<tcp-listener>
<port>1883</port>
<bind-address>0.0.0.0</bind-address>
</tcp-listener>
<!-- WebSocket configuration -->
<websocket-listener>
<port>8000</port>
<bind-address>0.0.0.0</bind-address>
<path>/mqtt</path>
<subprotocols>
<subprotocol>mqttv3.1</subprotocol>
<subprotocol>mqtt</subprotocol>
</subprotocols>
<allow-extensions>true</allow-extensions>
</websocket-listener>
</listeners>
<hivemq>在本例中,您需要将客户端连接到ws://localhost:8000/mqtt
有关配置的更多信息,请参阅文档:https://www.hivemq.com/docs/hivemq/latest/user-guide/listeners.html#websockets
https://stackoverflow.com/questions/61612587
复制相似问题