gpt4 book ai didi

node.js - aws-iot 设备和作业在同一进程中失去连接

转载 作者:太空宇宙 更新时间:2023-11-04 01:55:52 25 4
gpt4 key购买 nike

我有使用设备 SDK 的连接

const device = awsIot.device(config.DeviceOptions);

我还想使用新的 jobs sdk

const jobs = awsIot.jobs(config.DeviceOptions);

然后我明白

connect
offline
connection lost - will attempt reconnection in 1 seconds...
close
reconnect

如何在同一进程中使用两者而不出现连接问题?

我像下面这样使用它

var awsIot = require('aws-iot-device-sdk');
var SystemInfo = require('./trace/systeminfo');
// var JobsModule = require('./jobs/jobs');

var config = require('./config');

const device = awsIot.device(config.DeviceOptions);
const jobs = awsIot.jobs(config.DeviceOptions);

var timeout;
var count = 0;
const minimumDelay = 250;


device.subscribe('topic_2');


device
.on('connect', function () {
console.log('connect');
});
device
.on('close', function () {
console.log('close');
});
device
.on('reconnect', function () {
console.log('reconnect');
});
device
.on('offline', function () {
console.log('offline');
});
device
.on('error', function (error) {
console.log('error', error);
});
device
.on('message', function (topic, payload) {
console.log('message', topic, payload.toString());
});

最佳答案

要应答与 AWS 的每个连接,应该有一个唯一的 clientId。

如果您有影子、设备和作业正在运行,那么我们为连接创建了自己唯一的 clientId

var thingShadows = awsIot.thingShadow({
keyPath: <YourPrivateKeyPath>,
certPath: <YourCertificatePath>,
caPath: <YourRootCACertificatePath>,
clientId: <YourUniqueClientIdentifier>,
host: <YourCustomEndpoint>
});

我们所做的是

var device = awsIot.device({
keyPath: <YourPrivateKeyPath>,
certPath: <YourCertificatePath>,
caPath: <YourRootCACertificatePath>,
clientId: 'device -'+thingName,
host: <YourCustomEndpoint>
});

var thingShadows = awsIot.thingShadow({
keyPath: <YourPrivateKeyPath>,
certPath: <YourCertificatePath>,
caPath: <YourRootCACertificatePath>,
clientId: 'thingShadows-'+thingName,
host: <YourCustomEndpoint>
});

var jobs = awsIot.jobs({
keyPath: <YourPrivateKeyPath>,
certPath: <YourCertificatePath>,
caPath: <YourRootCACertificatePath>,
clientId: 'jobs-'+thingName,
host: <YourCustomEndpoint>
});

关于node.js - aws-iot 设备和作业在同一进程中失去连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48019211/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com