AWS IoT - 使用相同 clientId 和证书的两个设备导致重新连接错误
AWS IoT - Two devices using the same clientId and certs cause re-connections error
我有一个简单的 nodejs AWS Iot 设备实现。
var awsIot = require('aws-iot-device-sdk');
const device = awsIot.device({
keyPath: 'private.pem.key',
certPath: 'certificate.pem.crt',
caPath: 'CA.pem',
clientId: 'device1',
host: 'xxxxyyyyy1.iot.ap-southeast-1.amazonaws.com'
});
device.on('connect', () => {
console.log('DEVICE connect!');
});
device.on('message', (topic, payload) => {
console.log('DEVICE message!');
});
device.on('close', () => {
console.log('DEVICE closed!');
});
device.on('error', error => {
console.log('DEVICE error!');
console.log(error);
});
device.on('offline', () => {
console.log('DEVICE offline!');
});
脚本 运行 如果我执行它的单个实例就没问题。
输出:
dev@dev1:~/dev$ node test1.js
DEVICE connect!
但是,如果我 运行 同时在两台不同的机器上使用相同的脚本,两台设备都会反复断开连接并重新连接。
dev@dev1:~/dev$ node test1.js
DEVICE connect!
DEVICE offline!
DEVICE closed!
DEVICE connect!
DEVICE offline!
DEVICE closed!
DEVICE connect!
DEVICE offline!
DEVICE closed!
DEVICE connect!
^C
dev@dev1:~/dev$
dev@dev2:~/dev$ node test1.js
DEVICE connect!
DEVICE offline!
DEVICE closed!
DEVICE connect!
DEVICE offline!
DEVICE closed!
DEVICE connect!
DEVICE offline!
DEVICE closed!
DEVICE connect!
^C
dev@dev2:~/dev2$
AWS 安全策略:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "iot:*",
"Resource": "*"
}
]
}
嗯,在 MQTT 中,AWS IoT 使用的协议,每个设备的 Clientid 应该是唯一的
The client identifier (ClientId) identifies each MQTT client that
connects to an MQTT broker. The broker uses the ClientID to identify
the client and the current state of the client.Therefore, this ID
should be unique per client and broker. In MQTT 3.1.1 (the current
standard), you can send an empty ClientId, if you don’t need a state
to be held by the broker. The empty ClientID results in a connection
without any state. In this case, the clean session flag must be set to
true or the broker will reject the connection.
客户端 ID 也用于导致您出现问题的客户端接管
Usually, a disconnected client tries to reconnect. Sometimes, the
broker still has an half-open connection for the client. In MQTT, if
the broker detects a half-open connection, it performs a ‘client
take-over’. The broker closes the previous connection to the same
client (determined by the client identifier), and establishes a new
connection with the client. This behavior ensures that the half-open
connection does not stop the disconnected client from re-establishing
a connection.
来自 AWS-IoT 文档
The message broker uses the client ID to identify each client. The client ID is passed in from the client to the message broker as part of the MQTT payload. Two clients with the same client ID are not allowed to be connected concurrently to the message broker. When a client connects to the message broker using a client ID that another client is using, a CONNACK message will be sent to both clients and the currently connected client will be disconnected.
https://docs.aws.amazon.com/iot/latest/developerguide/protocols.html
我有一个简单的 nodejs AWS Iot 设备实现。
var awsIot = require('aws-iot-device-sdk');
const device = awsIot.device({
keyPath: 'private.pem.key',
certPath: 'certificate.pem.crt',
caPath: 'CA.pem',
clientId: 'device1',
host: 'xxxxyyyyy1.iot.ap-southeast-1.amazonaws.com'
});
device.on('connect', () => {
console.log('DEVICE connect!');
});
device.on('message', (topic, payload) => {
console.log('DEVICE message!');
});
device.on('close', () => {
console.log('DEVICE closed!');
});
device.on('error', error => {
console.log('DEVICE error!');
console.log(error);
});
device.on('offline', () => {
console.log('DEVICE offline!');
});
脚本 运行 如果我执行它的单个实例就没问题。 输出:
dev@dev1:~/dev$ node test1.js
DEVICE connect!
但是,如果我 运行 同时在两台不同的机器上使用相同的脚本,两台设备都会反复断开连接并重新连接。
dev@dev1:~/dev$ node test1.js
DEVICE connect!
DEVICE offline!
DEVICE closed!
DEVICE connect!
DEVICE offline!
DEVICE closed!
DEVICE connect!
DEVICE offline!
DEVICE closed!
DEVICE connect!
^C
dev@dev1:~/dev$
dev@dev2:~/dev$ node test1.js
DEVICE connect!
DEVICE offline!
DEVICE closed!
DEVICE connect!
DEVICE offline!
DEVICE closed!
DEVICE connect!
DEVICE offline!
DEVICE closed!
DEVICE connect!
^C
dev@dev2:~/dev2$
AWS 安全策略:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "iot:*",
"Resource": "*"
}
]
}
嗯,在 MQTT 中,AWS IoT 使用的协议,每个设备的 Clientid 应该是唯一的
The client identifier (ClientId) identifies each MQTT client that connects to an MQTT broker. The broker uses the ClientID to identify the client and the current state of the client.Therefore, this ID should be unique per client and broker. In MQTT 3.1.1 (the current standard), you can send an empty ClientId, if you don’t need a state to be held by the broker. The empty ClientID results in a connection without any state. In this case, the clean session flag must be set to true or the broker will reject the connection.
客户端 ID 也用于导致您出现问题的客户端接管
Usually, a disconnected client tries to reconnect. Sometimes, the broker still has an half-open connection for the client. In MQTT, if the broker detects a half-open connection, it performs a ‘client take-over’. The broker closes the previous connection to the same client (determined by the client identifier), and establishes a new connection with the client. This behavior ensures that the half-open connection does not stop the disconnected client from re-establishing a connection.
来自 AWS-IoT 文档
The message broker uses the client ID to identify each client. The client ID is passed in from the client to the message broker as part of the MQTT payload. Two clients with the same client ID are not allowed to be connected concurrently to the message broker. When a client connects to the message broker using a client ID that another client is using, a CONNACK message will be sent to both clients and the currently connected client will be disconnected.
https://docs.aws.amazon.com/iot/latest/developerguide/protocols.html