如何检查客户端是否已连接?
从Device类示例中,我了解Device是mqtt.Client()
返回的实例
mqtt.js
有一个连接标记https://github.com/mqttjs/MQTT.js#connected
但是,device.connected
给了我undefined
答案 0 :(得分:0)
因为它不存在。
DeviceClient {
publish: [Function],
subscribe: [Function],
unsubscribe: [Function],
end: [Function],
handleMessage: [Function: bound ],
updateWebSocketCredentials: [Function],
getWebsocketHeaders: [Function],
updateCustomAuthHeaders: [Function],
simulateNetworkFailure: [Function],
_events:
{ connect: [Function],
close: [Function],
reconnect: [Function],
offline: [Function],
error: [Function],
message: [Function] },
_eventsCount: 6 }
虽然你可以听到" connect"事件并添加"已连接"属性如下所示
device.on('connect', function() {
device.connected = true;
});
再次检查DeviceClient,现在就可以了。
DeviceClient {
publish: [Function],
subscribe: [Function],
unsubscribe: [Function],
end: [Function],
handleMessage: [Function: bound ],
updateWebSocketCredentials: [Function],
getWebsocketHeaders: [Function],
updateCustomAuthHeaders: [Function],
simulateNetworkFailure: [Function],
_events:
{ connect: [Function],
close: [Function],
reconnect: [Function],
offline: [Function],
error: [Function],
message: [Function] },
_eventsCount: 6,
connected: true }
答案 1 :(得分:0)
在IoT规则引擎中创建规则并收听生命周期事件。设备连接主题将在连接IoT设备时收到消息。
https://docs.aws.amazon.com/iot/latest/developerguide/life-cycle-events.html
答案 2 :(得分:0)
我找到了获取当前状态的技巧 要在之后监视该状态,您应该使用先前答案中建议的设备连接主题 如果您已启用Fleet索引设置 确保启用“事物连接”:将事物连接数据包括在事物索引中。
然后,您可以运行搜索(针对特定的Thing或一组事物),并获取每个Thing的连接状态:
` AWS = require('aws-sdk');
var iot = new this.AWS.Iot({
accessKeyId: this.AccessKeyId,
secretAccessKey: this.SecretAccessKey,
//endpoint: this.host,
maxResults: 500,
region: this.regionData
});
var params = {
queryString: 'shadow.reported.isConnected.connected = *',
indexName: 'AWS_Things',
nextToken: nexttoken
};
iot.searchIndex(params, function(err, data)
for (const robot of data.things[0])
connectivity = robot.connectivity.connected;`
注意-上面的代码不是有效的示例,仅用于说明原理 注意2-请记住搜索结果出现在页面中,并且需要以这种方式处理