![[衰]](http://lf-web-assets.juejin.cn/obj/juejin-web/xitu_juejin_web/img/jj_emoji_34.cf5b4d5.png)
请问有人用MQTT吗。。。第一次用这是啥意思。下面是代码
const options = {
// port: 61616,
connectTimeout: 10000,
clientId: 'mqttjs_' + Math.random().toString(16).substr(2, 8),
};
const client = mqtt.connect('mqtt://地址', options);
const Msg = () => {
client.on('connect', (e) => {
console.log('连接成功 ');
client.subscribe('company_heart', { qos: 0 }, (error) => {
if (error) {
console.log('订阅失败:', error);
} else {
console.log('成功订阅 company_heart 主题');
}
});
});
client.on('message', (topic, message) => {
console.log('收到来自 ', topic, '的消息', message.toString());
});
client.on('reconnect', (error) => {
console.log('正在重连 ', error);
client.end(); // 关闭客户端连接
});
client.on('error', (error) => {
console.log('连接失败 ', error);
client.end(); // 关闭客户端连接
});
};
然后控制台会报错:WebSocket connection to 'ws://172.18.20.46:61616/' failed:
本地测试,不用带账号密码,这是那的问题,配置错了吗?
const options = {
// port: 61616,
connectTimeout: 10000,
clientId: 'mqttjs_' + Math.random().toString(16).substr(2, 8),
};
const client = mqtt.connect('mqtt://地址', options);
const Msg = () => {
client.on('connect', (e) => {
console.log('连接成功 ');
client.subscribe('company_heart', { qos: 0 }, (error) => {
if (error) {
console.log('订阅失败:', error);
} else {
console.log('成功订阅 company_heart 主题');
}
});
});
client.on('message', (topic, message) => {
console.log('收到来自 ', topic, '的消息', message.toString());
});
client.on('reconnect', (error) => {
console.log('正在重连 ', error);
client.end(); // 关闭客户端连接
});
client.on('error', (error) => {
console.log('连接失败 ', error);
client.end(); // 关闭客户端连接
});
};
然后控制台会报错:WebSocket connection to 'ws://172.18.20.46:61616/' failed:
本地测试,不用带账号密码,这是那的问题,配置错了吗?
![[流泪]](http://lf-web-assets.juejin.cn/obj/juejin-web/xitu_juejin_web/img/jj_emoji_6.dde0d83.png)
展开
17
点赞