如何使用 Paho MQTT JavaScript 客户端连接 IBM Watson IOT?



我正在尝试使用 Paho MQTT Javascript 客户端连接 IBM Watson IoT 平台,如以下示例代码中所述。


var client = new Messaging.Client("myOqgId.messaging.internetofthings.ibmcloud.com", 8883, "myclientid_" + parseInt(Math.random() * 100, 10));
//Gets  called if the websocket/mqtt connection gets disconnected for any reason
client.onConnectionLost = function (responseObject) {
//Depending on your scenario you could implement a reconnect logic here
alert("connection lost: " + responseObject.errorMessage);
};
//Gets called whenever you receive a message for your subscriptions
client.onMessageArrived = function (message) {
//Do something with the push message you received
$('#messages').append('<span>Topic: ' + message.destinationName + '  | ' + message.payloadString + '</span><br/>');
};
//Connect Options
var options = {
userName: API-Key here,
password: Auth token here,
timeout: 3,
//Gets Called if the connection has sucessfully been established
onSuccess: function () {
alert("Connected");
},
//Gets Called if the connection could not be established
onFailure: function (message) {
alert("Connection failed: " + message.errorMessage);
}
};
//Creates a new Messaging.Message Object and sends it to the HiveMQ MQTT Broker
var publish = function (payload, topic, qos) {
//Send your message (also possible to serialize it as JSON or protobuf or just use a string, no limitations)
var message = new Messaging.Message(payload);
message.destinationName = topic;
message.qos = qos;
client.send(message);
}

但无法连接。 我收到此错误:WebSocket 与"ws://myOrgIdXYZ.messaging.internetofthings.ibmcloud.com:8883/mqtt"的连接失败:WebSocket 握手期间出错:net::ERR_CONNECTION_RESET

请任何尝试使用 Paho Mqtt 客户端连接 IBM Watson IoT 的人。

谢谢大家的回复。根据所有响应,我对代码进行了更改。

<script type="text/javascript">
var clientId  = 'a:myOrgId:'+Math.random().toString(16).substr(2, 8);
var client = new Messaging.Client("myOqgId.messaging.internetofthings.ibmcloud.com", 1883, clientId);
//Gets  called if the websocket/mqtt connection gets disconnected for any reason
client.onConnectionLost = function (responseObject) {
//Depending on your scenario you could implement a reconnect logic here
alert("connection lost: " + responseObject.errorMessage);
};
//Gets called whenever you receive a message for your subscriptions
client.onMessageArrived = function (message) {
//Do something with the push message you received
$('#messages').append('<span>Topic: ' + message.destinationName + '  | ' + message.payloadString + '</span><br/>');
};
//Connect Options
var options = {
userName: API-Key here,
password: Auth token here,
timeout: 3,
//Gets Called if the connection has sucessfully been established
onSuccess: function () {
alert("Connected");    	 
},
//Gets Called if the connection could not be established
onFailure: function (message) {
alert("Connection failed: " + message.errorMessage);
}
};
//Creates a new Messaging.Message Object and sends it to the HiveMQ MQTT Broker
var publish = function (payload, topic, qos) {
//Send your message (also possible to serialize it as JSON or protobuf or just use a string, no limitations)
var message = new Messaging.Message(payload);
message.destinationName = topic;
message.qos = qos;
client.send(message);
}
client.connect(options);
</script>

您可以看到我在 ClientId 中进行了更改。如果您未使用 Watson IoT 库,IBM Watson IoT将仅接受以下格式的客户端 ID。

var clientId = 'a:OrgId:'+RandomString;

如果您使用的是 IBM Watson IoT 库,则客户端 ID 可以是任何内容。甚至我在节点中实现了.js

最新更新