需要保留数据顺序时可以使用 CoAP 吗?
Can CoAP be used when preserving data order is required?
我正在尝试使用 node-coap 通过 CoAP 从物联网设备发送传感器数据 node-coap。数据到达 CoAP 服务器时的顺序对我来说很重要。即使使用 confirmable
请求选项,我也找不到保留数据序列的方法。
我下面有一个小程序可以说明我的意思。
如果order/sequence数据很重要,可以不使用CoAP吗?如果可以,我做错了什么?
'use strict';
const coap = require('coap'),
cbor = require('cbor'),
server = coap.createServer();
const sequentialData = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let incomingData = [];
let numResponses = sequentialData.length;
server.on('request', (req, res) => {
const obj = cbor.decodeFirstSync(req.payload);
incomingData.push(obj.data);
res.end();
});
server.listen(() => {
const reqOpts = {
hostname: 'localhost',
method: 'POST',
pathname: '/sequential',
options: {
Accept: 'application/cbor'
}
};
sequentialData.forEach((item) => {
const req = coap.request(reqOpts);
req.write(cbor.encode({
data: item
}));
req
.on('response', (res) => {
res.pipe(process.stdout);
res.on('end', () => {
if (--numResponses === 0) {
console.log(`got data in this order`, incomingData);
process.exit();
}
})
});
req.end();
});
});
上面的节点程序每次都会输出不同的顺序运行。
只要您使用 UDP 作为传输就不能。
根据 RFC7252:
As CoAP is bound to unreliable transports such as UDP, CoAP messages
may arrive out of order, appear duplicated, or go missing without
notice. For this reason, CoAP implements a lightweight reliability
mechanism, without trying to re-create the full feature set of a
transport like TCP. It has the following features:
- Simple stop-and-wait retransmission reliability with exponential
back-off for Confirmable messages.
- Duplicate detection for both Confirmable and Non-confirmable
messages.
https://www.rfc-editor.org/rfc/rfc7252
在不同的实现中有一些努力使 CoAP-over-HTTP 成为可能,但它不属于 CoAP RFC 本身。
如果你迫不及待地使用CoAP,你可以尝试用这种方式挖掘。
我正在尝试使用 node-coap 通过 CoAP 从物联网设备发送传感器数据 node-coap。数据到达 CoAP 服务器时的顺序对我来说很重要。即使使用 confirmable
请求选项,我也找不到保留数据序列的方法。
我下面有一个小程序可以说明我的意思。
如果order/sequence数据很重要,可以不使用CoAP吗?如果可以,我做错了什么?
'use strict';
const coap = require('coap'),
cbor = require('cbor'),
server = coap.createServer();
const sequentialData = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let incomingData = [];
let numResponses = sequentialData.length;
server.on('request', (req, res) => {
const obj = cbor.decodeFirstSync(req.payload);
incomingData.push(obj.data);
res.end();
});
server.listen(() => {
const reqOpts = {
hostname: 'localhost',
method: 'POST',
pathname: '/sequential',
options: {
Accept: 'application/cbor'
}
};
sequentialData.forEach((item) => {
const req = coap.request(reqOpts);
req.write(cbor.encode({
data: item
}));
req
.on('response', (res) => {
res.pipe(process.stdout);
res.on('end', () => {
if (--numResponses === 0) {
console.log(`got data in this order`, incomingData);
process.exit();
}
})
});
req.end();
});
});
上面的节点程序每次都会输出不同的顺序运行。
只要您使用 UDP 作为传输就不能。
根据 RFC7252:
As CoAP is bound to unreliable transports such as UDP, CoAP messages may arrive out of order, appear duplicated, or go missing without notice. For this reason, CoAP implements a lightweight reliability mechanism, without trying to re-create the full feature set of a transport like TCP. It has the following features:
- Simple stop-and-wait retransmission reliability with exponential back-off for Confirmable messages.
- Duplicate detection for both Confirmable and Non-confirmable messages.
https://www.rfc-editor.org/rfc/rfc7252
在不同的实现中有一些努力使 CoAP-over-HTTP 成为可能,但它不属于 CoAP RFC 本身。
如果你迫不及待地使用CoAP,你可以尝试用这种方式挖掘。