mqtt - 未在 cloudmqtt 上使用 MQTTjs 连接用户
问题描述
我的文件中有这段代码(属于反应)
const client = mqtt.connect({
host: 'mqtt://m16.cloudmqtt.com',
port: 1883,
username: 'b*******k',
password: 'gU******S',
});
client.on('connect', () => {
console.log('hello');
client.subscribe('v');
client.publish('v', 'chal pa');
});
client.on('message', (topic, message) => {
if (topic === 'v') {
console.log('here my topic is v');
// var connected = (message.toString() === 'true');
}
console.log('recived message from mqtt');
console.log(message);
});
client.on('error', er => {
console.log(er);
});
我期待连接到 mqtt 代理并收到一些消息。
但是什么也没发生。当我在 cloudmqtt.com 检查日志文件时
我被困在这里,任何人都可以帮忙。链接到任何有帮助的博客/视频将不胜感激。
我正在使用 mqttjs
解决方案
您已明确告诉 MQTTjs 库使用本地 MQTT,而不是通过mqtt://
在 URI 的开头使用 MQTT over Websockets。
如果你想通过 websockets 使用 MQTT,URI 应该以ws://
其次,您使用的是端口 1883,这通常用于本机 MQTT,而不是 MQTT over websockets。cloudmqtt文档建议您应该使用以 a 开头的端口号3
来访问 websockets 侦听器。
推荐阅读
- c# - 为什么 NuGet 目标框架不匹配没有警告?
- javascript - AOS(滚动动画)不能全屏工作
- javascript - 如何将吸气剂添加到所有对象
- angular - 在角度 8 中查找两个对象之间的差异
- apache - 301页面到页面重定向指令不起作用
- python - python - 当我从熊猫的excel中读取十六进制字符串时,如何抑制python中的科学记数法?
- visual-studio - 将 pod 暴露在当地环境中以便于开发?
- vb.net - 如何编辑这个循环?
- c++ - 模板模板可变参数包
- javascript - 如何在vscode扩展开发中获取triggerSuggest选择的值