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

标签 node.js amazon-web-services aws-iot

我有使用设备 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/

相关文章:

ios - 使用 IAM 用户凭证 iOS 的 AWS iOT 登录问题

arduino - 将 esp8266 连接到 AWS IoT

Go AWS MQTT 连接在订阅完成前丢失

javascript - YouTube使用 Node js自动上传

node.js - Nodejs 从不存在的电子邮件发送电子邮件

node.js - Node 生成子进程在 Windows 中不起作用

mysql - 使用 Erwin 为 Amazon Redshift 创建 DDL

amazon-web-services - YAML_FILE_ERROR 消息 : Expected Commands[0] to be of string type:

node.js - SequelizeJS 无法读取未定义的属性 'getTableName'

macos - 如何使用 amazon ec2 实例设置我的 mac ftp 客户端?