我正在使用蔚蓝物联网设备SDK (1.3.31)连接模拟设备。对于某些设备,----我在打这个电话时得到了这个异常:
//pseudo code
deviceInfo.device = registryManager.getDevice(deviceId);
String deviceConnectionString = new StringBuilder()
.append("HostName=").append(hubHostname)
.append(";DeviceId=").append(device.getDeviceId())
.append(";SharedAccessKey=").append(device.getPrimaryKey())
.toString();
DeviceClient deviceClient = new DeviceClient(deviceConnectionString, IotHubClientProtocol.MQTT);
deviceClient.open();而出现泡沫的例外:
java.io.IOException: Unable to subscribe to topic :devices/sim3_0001/messages/devicebound/# because java.io.EOFExceptionConnection lost
at com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttIotHubConnection.open(MqttIotHubConnection.java:142) ~[iot-device-client-1.2.30.jar:na]
at com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttTransport.open(MqttTransport.java:83) ~[iot-device-client-1.2.30.jar:na]
at com.microsoft.azure.sdk.iot.device.DeviceIO.open(DeviceIO.java:212) ~[iot-device-client-1.2.30.jar:na]
at com.microsoft.azure.sdk.iot.device.DeviceClient.open(DeviceClient.java:188) ~[iot-device-client-1.2.30.jar:na]我对库代码做了一些深入研究,发现在订阅得到响应之前,我的连接就被丢弃了;这是一个被消耗并转换为IOException的异常:
Connection lost (32109) - java.io.EOFException
at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:146)
at java.lang.Thread.run(Thread.java:745)
Lost connection to the server. Reconnecting 0 time.
Caused by: java.io.EOFException
at java.io.DataInputStream.readByte(DataInputStream.java:267)
at org.eclipse.paho.client.mqttv3.internal.wire.MqttInputStream.readMqttWireMessage(MqttInputStream.java:65)
at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:107)此外,我连接了iothub资源管理器,看看是否有任何额外的有用信息。这就是它在我公开的电话里说出来的:
body:
protocol: Mqtt
authType: { "scope": "device", "type": "sas", "issuer": "iothub" }
time: 2017-06-13T21:13:48.2406702Z
operationName: deviceConnect
category: Connections
level: Information
deviceId: sim3_0001
ipAddress: xx.xx.xxx.XXX
enqueuedTimeUtc: Tue Jun 13 2017 17:13:53 GMT-0400 (EDT)
offset: 355136
applicationProperties:
category: Connections
level: Information
operationName: deviceConnect
sequenceNumber: 705
annotations:
x-opt-sequence-number: 705
x-opt-offset: 355136
x-opt-enqueued-time: Tue Jun 13 2017 17:13:53 GMT-0400 (EDT)
offset: 355536
enqueuedTimeUtc: Tue Jun 13 2017 17:13:53 GMT-0400 (EDT)
body:
protocol: Mqtt
authType: { "scope": "device", "type": "sas", "issuer": "iothub" }
time: 2017-06-13T21:13:48.3656729Z
operationName: deviceDisconnect
category: Connections
level: Error
statusCode: 404
statusType: 404104
statusDescription: DeviceConnectionClosedRemotely
deviceId: sim3_0001
ipAddress: xx.xx.xxx.XXX
annotations:
x-opt-sequence-number: 706
x-opt-offset: 355536
x-opt-enqueued-time: Tue Jun 13 2017 17:13:53 GMT-0400 (EDT)
applicationProperties:
category: Connections
level: Error
operationName: deviceDisconnect
sequenceNumber: 706另一个注意事项:新设备成功地连接/订阅,似乎它们在重新连接时会更频繁地失败。如果我将打开的调用放在一个循环中,并在1秒睡眠后重试,这些设备最终连接成功。
发布于 2017-07-24 02:10:21
解决这个问题的真正答案是,用于MQTT协议的azure物联网集线器设备库只允许每个应用程序一个连接。库只创建一个MQTT连接,因此,如果您创建第二个设备并试图连接其中一个设备,其中一个将被强制断开。
发布于 2017-06-13 09:01:26
根据您的错误信息java.io.IOException: Unable to subscribe to topic :devices/sim3_0001/messages/devicebound/# because java.io.EOFExceptionConnection lost,我认为您试图通过使用devices/{device_id}/messages/devicebound/#作为主题筛选器来接收云对设备消息。Azure官方文件Migrating a device app from AMQP to MQTT & Receiving cloud-to-device messages分节Communicate with your IoT hub using the MQTT protocol解释了您问题的原因如下。
接收云对设备消息 要从IoT集线器接收消息,设备应该使用
devices/{device_id}/messages/devicebound/#作为主题筛选器进行订阅。主题筛选器中的多级通配符#仅用于允许设备接收主题名称中的其他属性。IoT集线器不允许使用#或通配符过滤子主题。由于IoT集线器不是一个通用的发布子消息代理,它只支持文档化的主题名称和主题筛选器。
对于Azure IoT集线器,"MQTT不支持放弃/拒绝,因此我们只显示从IoTHub接收到的消息并返回完成“。在样本代码上有一个官方的GitHub,您可以参考它来修改代码并使其与MQTT一起工作。
https://stackoverflow.com/questions/44506089
复制相似问题