将 MQTTNet 服务器与 MQTT.js 客户端一起使用 [英] Use MQTTNet Server with MQTT.js client

查看:214
本文介绍了将 MQTTNet 服务器与 MQTT.js 客户端一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经启动了一个 MQTT 服务器,就像 这个 示例一样.此代码托管在 ASP.Net Core 2.0 应用程序中,但我尝试过控制台应用程序但没有成功.

I have started an MQTT server just like this example. This code is hosted in an ASP.Net Core 2.0 application but I have tried console application with no luck.

我还使用与上面相同的演示设置了一个客户端,它连接完美.此外,Android 客户端连接正常.但是我已经放置了一个 MQTT.js 客户端网页,但它无法与 chrome 连接,显示 net::ERR_CONNECTION_REFUSED 错误.

I have also setup a client using the same demo as above and it connects perfectly. Also an Android client connects fine. But I have placed a MQTT.js client webpage but it will not connect with chrome showing net::ERR_CONNECTION_REFUSED error.

我认为问题在于服务器不支持网络套接字.因为如果我使用 WS 类型启动我的客户端,它将无法连接.

I believe that the problem is with server not supporting web sockets. Because if I start my client with WS type it will not connect.

var options = new MqttClientOptions
{
    Server = "localhost",
    //ConnectionType = MqttConnectionType.Tcp // Connects
    ConnectionType = MqttConnectionType.Ws    // Does not connect
};

现在 MQTT.js 支持 TCP此链接 正在讲述.但我似乎无法工作.

Now MQTT.js supports TCP as far this link is telling. But I don't seem to be able to work it.

这是我页面 javascript 中的代码:

This is the code in my page javascript:

var client = mqtt.connect('tcp://localhost') //Also did mqtt://localhost

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

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

我想知道如何让 javascript MQTT 客户端使用 TCP?(也许还有其他 js 插件?)或者我如何在 MQTTNet 中启用 WebSockets.感谢您的帮助.

I want to know how can I make javascript MQTT client to use TCP? (Any other js plugin maybe?) Or alternatively how can I enable WebSockets in MQTTNet. Thanks for your help.

推荐答案

MQTT.js 确实支持原生 MQTT 和通过 Websockets 的 MQTT,但是如果您将其嵌入到 Web 应用程序中,它只能使用通过 Websockets 的 MQTT,因为浏览器沙箱将不允许它使用任意 TCP 连接.

MQTT.js does support both native MQTT and MQTT over Websockets, but if you are embedding it in to a web app it can only use MQTT over Websockets because the browsers sandbox will not allow it to use arbitrary TCP connections.

至于在代理中启用 Websockets,我在代码中看不到任何明显的内容,因此您可能不得不针对 github 项目提出和问题以询问详细信息.

As for enabling Websockets in the broker I can't see anything obvious in the code so you'll probably have to raise and issue against the github project to ask for details.

这篇关于将 MQTTNet 服务器与 MQTT.js 客户端一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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