简体   繁体   English

MQTT JavaScript客户端未连接(协议错误?)

[英]MQTT JavaScript client not connecting (wrong protocol?)

I am trying really hard to make my MQTT client work inside my application. 我非常努力地使我的MQTT客户端在我的应用程序中运行。 The broker is provided by CloudMQTT . 该代理由CloudMQTT提供。

When trying to run the following code in NodeJS, the client connects properly to MQTT; 尝试在NodeJS中运行以下代码时,客户端正确连接到MQTT;

var mqtt = require('mqtt');

var client = mqtt.connect(
        "mqtt://m20.cloudmqtt.com",
        {
            port: 11212,
            username: "XXXXXXXX",
            password: "XXXXXXXX"
        }
);

client.on('connect', function () {
    console.log('connected');
});

However, when I run the same code (without the require of course) in the frontend using the Bower package , the client does not connect. 但是,当我使用Bower软件包在前端运行相同的代码(当然不需要)时,客户端无法连接。 I have also tried other MQTT browserfied JS packages. 我还尝试了其他MQTT browserfied JS软件包。 I prefer MQTT.js and not Paho, because I would like to use multiple subscribes with one connected client. 我更喜欢MQTT.js,而不是Paho,因为我想对一个连接的客户端使用多个订阅。

If console.log(client); 如果console.log(client); , NodeJS uses protocol: 'mqtt' and protocolId: 'MQTT' . protocol: 'mqtt'使用protocol: 'mqtt'protocolId: 'MQTT' The frontend uses protocol: 'ws' and protocolId: 'MQTT' . 前端使用protocol: 'ws'protocolId: 'MQTT' Could this be the problem? 这可能是问题吗? Adding these options to mqtt.connect has no effect. 将这些选项添加到mqtt.connect无效。

From with in the browser the only option is going to be to connect via MQTT over Websockets. 在浏览器中,唯一的选择将是通过Websockets通过MQTT连接。 CloudMQTT use different port numbers for native MQTT and MQTT over websockets so you will need to also change the port number for the browser based code to the Websocket port listed in your CloudMQTT dashboard. CloudMQTT通过websocket对本机MQTT和MQTT使用不同的端口号,因此您还需要将基于浏览器的代码的端口号更改为CloudMQTT仪表板中列出的Websocket端口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM