简体   繁体   English

无法使用mqtt.js通过tcp将表单javascript连接到代理

[英]Cannot connect form javascript to a broker through tcp using mqtt.js

I would like to conenct an MQTT broker with Javascript in order to subscript to a topic and publish messages. 我想用Javascript连接一个MQTT代理,以便下标主题并发布消息。 The connection needs to be done through tcp on port 1883. I am using MQTT.js library. 连接需要通过端口1883上的tcp完成。我正在使用MQTT.js库。 The front end is in angularjs. 前端是棱角分明的。 The example followed is the one in MQTT.js page, though the connection cannot be achieved. 接下来的示例是MQTT.js页面中的示例,但无法实现连接。 Could anyone please help? 有人可以帮忙吗?

Connection through index.html: 通过index.html连接:

<script src="../node_modules/mqtt/browserMqtt.js"></script>

Code for connection: 连接代码:

var client = mqtt.connect('url.com:1883',{clientId :'client1', clean: true});
client.on('connect', function () {
  console.log("onsubscribe");
  client.subscribe('votingSignals', function (err) {
    if (!err) {
      console.log("onsubscribe");
      client.publish('votingSignals', 'start')
    }
 })
})

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

The error displayed is: 显示的错误是:

WebSocket connection to 'ws://url.com:1883/' failed: Error during WebSocket handshake: net::ERR_CONNECTION_RESET 与'ws://url.com:1883 /'的WebSocket连接失败:WebSocket握手期间出错:net :: ERR_CONNECTION_RESET

From a web browser you can ONLY use MQTT over Websockets, not native MQTT (over TCP). 从Web浏览器中,您只能在Websockets上使用MQTT,而不是使用本机MQTT(通过TCP)。

This is because the browser will not let you open a normal socket. 这是因为浏览器不会让你打开正常的套接字。

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

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