为什么 MQTT 不与 NodeJS 连接? [英] Why is MQTT not connecting with NodeJS?

查看:104
本文介绍了为什么 MQTT 不与 NodeJS 连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试使用 NODEJS 连接到 MQTT 服务器时遇到一个奇怪的问题:

I have a weird issue trying to connect to an MQTT server with NODEJS:

如果我连接到 MQTT 服务器但我没有连接,它就会挂起.

If I connect to the MQTT server and I do not get a connect it just hangs.

如果我使用命令行执行此操作,我会看到数据,因此网络、服务器等都很好.

If I do it with the command line I see data so network, server etc is all good.

如果我使用了错误的端口,那么命令行会给我一个有效的拒绝消息,但 NODE 只是挂起.

If I use a port thats wrong then command line gives me a valid reject message but NODE just hangs.

命令行是:

mosquitto_sub -h 10.10.10.30 -p 1883 -t sim 

我的代码完全是基础的:

My code is completely basic:

var mqtt = require('mqtt');

var MQTT_TOPIC          = "sim";
var MQTT_ADDR           = "10.10.10.30";
var MQTT_PORT           = 1883;
var client = mqtt.connect({host: MQTT_ADDR, port : MQTT_PORT, debug: true});

client.on('connect', function() {
    console.log('Connected');
    client.subscribe(MQTT_TOPIC, function() {
        client.on('message', function(topic, message, packet) {
            console.log(topic + ": '" + message);
        });
    });
});

推荐答案

我遇到了同样的问题,并找到了解决方案.我不是 node.js 专家,所以这只是一个反复试验的案例.也许其他人可以详细说明真正的问题.

I had the same problem, and figured out a solution. I'm no node.js expert, so it was just a case of trial and error. Maybe somebody else can detail the true problem.

这个连接字符串对我有用:var client = mqtt.connect(MQTT_ADDR,{clientId: 'bgtestnodejs', protocolId: 'MQIsdp', protocolVersion: 3, connectTimeout:1000, debug:true});

This connection string works for me: var client = mqtt.connect(MQTT_ADDR,{clientId: 'bgtestnodejs', protocolId: 'MQIsdp', protocolVersion: 3, connectTimeout:1000, debug:true});

完整示例如下:

var mqtt = require('mqtt');

var MQTT_TOPIC          = "hello";
var MQTT_ADDR           = "mqtt://192.168.0.105";
var MQTT_PORT           = 1883;

/* This is not working as expected */
//var client = mqtt.connect({host: MQTT_ADDR, port:MQTT_PORT},{clientId: 'bgtestnodejs'});

/* This works... */
var client  = mqtt.connect(MQTT_ADDR,{clientId: 'bgtestnodejs', protocolId: 'MQIsdp', protocolVersion: 3, connectTimeout:1000, debug:true});

client.on('connect', function () {
    client.subscribe(MQTT_TOPIC);
    client.publish(MQTT_TOPIC, 'Hello mqtt');
});

client.on('message', function (topic, message) {
    // message is Buffer
    console.log(message.toString());
    client.end();
});

client.on('error', function(){
    console.log("ERROR")
    client.end()
})

Mosquitto 似乎要求 protocolId 和 protocolVersion 设置如上.此外,请注意主机和端口不包含在选项中,而是作为第一个参数给出.

Mosquitto seems to require that the protocolId and protocolVersion is set as above. Additionally, notice that the host and port is not included in the options, but instead given as the first argument.

如果我正确阅读了文档,则不应将主机和端口参数作为选项的一部分给出,而应作为服务器"选项在选项之前给出.请参阅此链接.我无法从该链接获取语法,但上面的代码行似乎很有效.

If I read the documentation correct, the host and port arguments should NOT be given as part of the options, but as a "servers" option, before the options. See this link. I can't get the syntax from that link to work, but the code lines above, seems to the trick.

这篇关于为什么 MQTT 不与 NodeJS 连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆