gpt4 book ai didi

javascript - 无法使用 Eclipse PAHO MQTT Library for JavaScript 连接到 IBM Watson IoT Platform

转载 作者:行者123 更新时间:2023-11-29 19:02:34 24 4
gpt4 key购买 nike

出于安全原因,IBM 最近似乎停止接受与 MQTT borker 的非 TLS 连接。

我喜欢一个小应用程序,它使用 JavaScript 从智能手机读取加速计传感器数据,并通过 MQTT 将其发送到消息代理。

This application被打破。因此,我试图在此 fork 中修复它.

如前所述,非 TLS 连接会被拒绝,这就是我启用 TLS 的原因:

        window.client.connect({
onSuccess: onConnectSuccess,
onFailure: onConnectFailure,
userName: "use-token-auth",
password: window.password,
useSSL: true
});

它仍然没有连接。在 IBM Watson IoT Platform 上,我在日志中看到了这个错误,没有其他错误:

Closed connection from 213.55.176.207. The operation is not authorized.

现在我创建了一个小测试 index.html文件。如果我作为设备连接,它仍然无法工作,但如果我作为应用程序连接,它就可以工作,如图所示 here .但我已经在平台中创建了设备。如果我使用的是旧的 Watson IoT Platform 实例,它可以工作,但不能使用较新的实例。

我做错了什么?

最佳答案

在你的应用程序代码中你有这个:

window.client = new Paho.MQTT.Client("bmzc5i.messaging.internetofthings.ibmcloud.com", 8883, 'a:myOrgId:'+Math.random().toString(16).substr (2, 8));

很好,当应用程序连接时,id 的格式是 a:orgId:uniqueIdentifierForTheApplication

uniqueIdentifierForTheApplication 可以是任何字符串(在允许的字符集中),并且不需要预先注册,因此在这里生成随机数在 99% 的情况下都有效。

window.client = new Paho.MQTT.Client("bmzc5i.messaging.internetofthings.ibmcloud.com", 8883, 'd:bmzc5i:'+Math.random().toString(16).substr (2, 8));

当设备连接时,id 的格式为 d:orgId:typeId:deviceId。要连接设备,必须先注册它,因此 d:orgId:randomNumber 的 ID 将不起作用。

如果您使用 ID myDevice 注册设备,设备类型为 myType,并将其授权 token 设置为 myToken,那么您将形成如下连接:

window.client = new Paho.MQTT.Client("bmzc5i.messaging.internetofthings.ibmcloud.com", 8883, 'd:bmzc5i:myType:myDevice');
window.client.connect({
onSuccess: onConnect成功,
onFailure:onConnectFailure,
用户名:“使用 token 验证”,
密码:“我的 token ”,
使用SSL:真

此外,您应该注意作为设备和作为应用程序连接时主题空间的差异。

当您作为应用程序连接时,您拥有整个组织范围。因此,要代表设备发布/订阅事件,您可以使用以下主题:

发布到 iot-2/type/myType/id/myDevice/evt/statusEvent/fmt/json

当您作为设备连接时,您只有设备范围(这是一种安全机制,用于限制受损设备可能造成的损害)。因此对于设备来说,同样的事情可以通过发布到:

iot-2/evt/statusEvent/fmt/json

平台使用进行发布的设备的身份来确定属于哪个设备,而对于应用程序,应用程序决定将事件分配给哪个设备。

关于javascript - 无法使用 Eclipse PAHO MQTT Library for JavaScript 连接到 IBM Watson IoT Platform,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45775107/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com