我一直在尝试使用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."
没什么可去的,所以我想知道是否有人通过Javascript与Eclipse Paho成功连接到MQTT Bridge,这是Google在他们的文档中建议的客户端实现。
我已经完成了他们的故障排除步骤,事情似乎在起作用,所以也没有任何帮助。
https://cloud.google.com/iot/docs/troubleshooting
我注意到在他们的文档中他们有Java / Python等的示例代码,但不是Javascript,所以我想知道它是否根本不受支持而且他们的文档没有提到这样。
我已将我的代码简化为仅使用“Hello World' Paho文档中的示例,据我所知,我已经正确完成了事情(包括使用我的设备路径作为ClientID,将JWT令牌作为密码,指定一个未使用的' userName字段并明确要求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);
}
答案 0 :(得分:0)
I'm a Googler (but I don't work in Cloud IoT).
Your code looks good to me and it should work. I will try it for myself this evening or tomorrow and report back to you.
I've spent the past day working on a Golang version of the samples published on Google's documentation. Like you, I was disappointed to not see all Google's regular languages covered by samples.
Are you running the code from a browser or is it running on Node.JS?
Do you have a package.json (if Node) that you would share too please?
Update
Here's a Node.JS (JavaScript but non-browser) that connects to Cloud IoT, subscribes to /devices/${DEVICE}/config
and publishes to /devices/${DEVICE}/events
.
https://gist.github.com/DazWilkin/65ad8890d5f58eae9612632d594af2de
index.js
of the location of Google's CA and your keyconfig.json
node index.js
You should be able to pull messages from the Pub/Sub subscription and you should be able to send config messages to the device.