【发布时间】:2018-03-21 23:45:40
【问题描述】:
我一直在尝试使用 Eclipse Paho MQTT 客户端的 javacscript 版本来访问 Google IOTCore MQTT Bridge,如下所示:
https://cloud.google.com/iot/docs/how-tos/mqtt-bridge
但是,无论我做什么,任何使用已知良好凭据(与其他客户端合作)进行连接的尝试都会导致此连接错误:
errorCode: 7, errorMessage: "AMQJS0007E Socket error:undefined."
没有太多可做的,所以我想知道是否有人曾通过 Eclipse Paho 的 Javascript 成功连接到 MQTT Bridge,这是 Google 在其文档中建议的客户端实现。
我已经完成了他们的故障排除步骤,而且事情似乎正在好转,所以也没有任何帮助。
https://cloud.google.com/iot/docs/troubleshooting
我注意到在他们的文档中,他们有 Java/Python 等的示例代码,但没有 Javascript,所以我想知道它是否根本不受支持,他们的文档只是没有提及。
我已经简化了我的代码,只使用 Paho 文档中的“Hello World”示例,据我所知,我已经正确完成了一些事情(包括使用我的设备路径作为 ClientID,JWT 令牌作为密码,指定“未使用”的用户名字段并明确要求 MQTT v3.1.1)。
与此同时,我将回退到通过他们的 HTTP 桥接器进行轮询,但这具有明显的延迟和网络流量缺陷。
// Create a client instance
client = new Paho.MQTT.Client("mqtt.googleapis.com", Number(8883), "projects/[my-project-id]/locations/us-central1/registries/[my registry name]/devices/[my device id]");
// set callback handlers
client.onConnectionLost = onConnectionLost;
client.onMessageArrived = onMessageArrived;
// connect the client
client.connect({
mqttVersion: 4, // maps to MQTT V3.1.1, required by IOTCore
onSuccess:onConnect,
onFailure: onFailure,
userName: 'unused', // suggested by Google for this field
password: '[My Confirmed Working JWT Token]' // working JWT token
function onFailure(resp) {
console.log(resp);
}
// called when the client connects
function onConnect() {
// Once a connection has been made, make a subscription and send a message.
console.log("onConnect");
client.subscribe("World");
message = new Paho.MQTT.Message("Hello");
message.destinationName = "World";
client.send(message);
}
// called when the client loses its connection
function onConnectionLost(responseObject) {
if (responseObject.errorCode !== 0) {
console.log("onConnectionLost:"+responseObject.errorMessage);
}
}
// called when a message arrives
function onMessageArrived(message) {
console.log("onMessageArrived:"+message.payloadString);
}
【问题讨论】:
标签: google-cloud-platform mqtt paho google-cloud-iot