将azure-iot-device sdk用于节点可在独立的节点程序中运行。当尝试在React Web应用程序中使用相同的节点代码时,与IoT中心的连接失败,出现以下错误:“ iothub-errorcode:InvalidProtocolVersion”:
stream.js:61 WebSocket connection to 'wss://my-iothub.azure-devices.net/' failed: Error during WebSocket handshake: Unexpected response code: 400
App.js
var Protocol = require('azure-iot-device-mqtt').Mqtt;
var Client = require('azure-iot-device').Client;
var connectionString = "HostName=my-iothub.azure-devices.net;DeviceId=<redacted>;SharedAccessKey=<redacted>=";
var client = Client.fromConnectionString(connectionString, Protocol);
var connectCallback = function (err) {
if (err) {
console.error('Could not connect: ' + err.message);
}
...
};
client.open(connectCallback);
标题(来自Chrome开发者工具)
Request URL: wss://my-iothub.azure-devices.net/
Request Method: GET
Status Code: 400 Bad Request
Content-Length: 158
Content-Type: application/json; charset=utf-8
Date: Wed, 02 Oct 2019 21:18:10 GMT
iothub-errorcode: InvalidProtocolVersion
Server: Microsoft-HTTPAPI/2.0
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Cache-Control: no-cache
Connection: Upgrade
Host: my-iothub.azure-devices.net
Origin: http://localhost:3000
Pragma: no-cache
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits
Sec-WebSocket-Key: <redacted>
Sec-WebSocket-Protocol: mqtt
Sec-WebSocket-Version: 13
Upgrade: websocket
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_0)
可能与:https://github.com/Azure/azure-sdk-for-js/issues/3473#issuecomment-499719031] >>
将azure-iot-device sdk用于节点可在独立的节点程序中运行。当尝试在React Web应用程序中使用相同的节点代码时,与IoT中心的连接失败“ iothub-errorcode:...
我假设您正在尝试从Microsoft Azure中部署的React Web应用程序中利用IOT集线器,如果是这种情况,请尝试以下操作: