首页 > 解决方案 > 未在 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

标签: mqttiot

解决方案


您已明确告诉 MQTTjs 库使用本地 MQTT,而不是通过mqtt://在 URI 的开头使用 MQTT over Websockets。

如果你想通过 websockets 使用 MQTT,URI 应该以ws://

其次,您使用的是端口 1883,这通常用于本机 MQTT,而不是 MQTT over websockets。cloudmqtt文档建议您应该使用以 a 开头的端口号3来访问 websockets 侦听器。


推荐阅读