我正在尝试使用node-coap通过CoAP从IoT设备传输传感器数据 . 到达CoAP服务器时,数据的顺序对我来说很重要 . 即使使用 confirmable
请求选项,我也找不到保留数据序列的方法 .
我有一个小程序,显示我的意思 .
Can CoAP not be used if order/sequence of data is important? If it can, what am I doing wrong?
'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();
});
});
上面的节点程序每次运行时都会输出不同的顺序 .
1 回答
只要你使用UDP作为传输就可以了 .
根据RFC7252:
https://tools.ietf.org/html/rfc7252
有一些努力在不同的实现中进行CoAP-over-HTTP,但它不属于CoAP RFC本身 .
如果您绝对不得不使用CoAP,您可能会尝试以这种方式进行挖掘 .