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

时间:2017-12-29 07:39:40

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

我有使用设备sdk的连接

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

我还想使用新的职位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());
    });

1 个答案:

答案 0 :(得分:0)

回答每个与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>
});