首先,我在编程方面相当新,在 NODE 方面绝对是新人,所以也许我的代码组织得不好。
我正在使用 RaspberryPi 订阅 9 个事件。以下代码仅处理 2 个事件。我逐渐添加了更多订阅。虽然订阅数为 7 或更少,但一切都很好。如果我尝试更多,我会收到 ECONNRESET 错误。
例如,我尝试订阅 8 个事件,我成功订阅了 6 个,但其中 2 个出现错误。
代码:
var http = require('http');
var SPI = require('pi-spi');
var dotstar = require('dotstar');
var sleep = require('sleep');
spi = SPI.initialize('/dev/spidev0.0');
const ledStripLength = 250;
const ledStrip = new dotstar.Dotstar(spi, {
length: ledStripLength
});
//1. Defining message for subscription
var E1 = {
method: 'post',
body: {"destUrl":"http://192.168.100.200:3000"}, // Javascript object payload
json: true,
url: "http://192.168.1.91/rest/events/valuechange1/notifs", //<-- event we subscribe to.
headers: {
'Content-Type': 'application/json'
}
};
var E2 = {
method: 'post',
body: {"destUrl":"http://192.168.100.200:3000"}, // Javascript object payload
json: true,
url: "http://192.168.1.91/rest/events/valuechange2/notifs", //<-- event we subscribe to.
headers: {
'Content-Type': 'application/json'
}
};
var E3 = {
method: 'post',
body: {"destUrl":"http://192.168.100.200:3000"}, // Javascript object payload
json: true,
url: "http://192.168.1.91/rest/events/Z1_Changed/notifs", //<-- event we subscribe to.
headers: {
'Content-Type': 'application/json'
}
};
var E4 = {
method: 'post',
body: {"destUrl":"http://192.168.100.200:3000"}, // Javascript object payload
json: true,
url: "http://192.168.1.91/rest/events/Z2_Changed/notifs", //<-- event we subscribe to.
headers: {
'Content-Type': 'application/json'
}
};
var E5 = {
method: 'post',
body: {"destUrl":"http://192.168.100.200:3000"}, // Javascript object payload
json: true,
url: "http://192.168.1.91/rest/events/Z3_Changed/notifs", //<-- event we subscribe to.
headers: {
'Content-Type': 'application/json'
}
};
var E6 = {
method: 'post',
body: {"destUrl":"http://192.168.100.200:3000"}, // Javascript object payload
json: true,
url: "http://192.168.1.91/rest/events/Z4_Changed/notifs", //<-- event we subscribe to.
headers: {
'Content-Type': 'application/json'
}
};
var E7 = {
method: 'post',
body: {"destUrl":"http://192.168.100.200:3000"}, // Javascript object payload
json: true,
url: "http://192.168.1.91/rest/events/Z5_Changed/notifs", //<-- event we subscribe to.
headers: {
'Content-Type': 'application/json'
}
};
var E8 = {
method: 'post',
body: {"destUrl":"http://192.168.100.200:3000"}, // Javascript object payload
json: true,
url: "http://192.168.1.91/rest/events/PenChangeEnded/notifs", //<-- event we subscribe to.
headers: {
'Content-Type': 'application/json'
}
};
//2. Sending subscriptions.
request(E1, function (err, res, body) {
if (err) {
console.log('Error :', err);
return;
}
console.log(' Body :', JSON.stringify(body)); // Just printing the return message as we subscribe.
console.log('subscribed to E1');
});
request(E2, function (err, res, body) {
if (err) {
console.log('Error :', err);
return;
}
console.log(' Body :', JSON.stringify(body)); // Just printing the return message as we subscribe.
console.log('subscribed to E2');
});
request(E3, function (err, res, body) {
if (err) {
console.log('Error :', err);
return;
}
console.log(' Body :', JSON.stringify(body)); // Just printing the return message as we subscribe.
console.log('subscribed to E3');
});
request(E4, function (err, res, body) {
if (err) {
console.log('Error :', err);
return;
}
console.log(' Body :', JSON.stringify(body)); // Just printing the return message as we subscribe.
console.log('subscribed to E4');
});
request(E5, function (err, res, body) {
if (err) {
console.log('Error :', err);
return;
}
console.log(' Body :', JSON.stringify(body)); // Just printing the return message as we subscribe.
console.log('subscribed to E5');
});
request(E6, function (err, res, body) {
if (err) {
console.log('Error :', err);
return;
}
console.log(' Body :', JSON.stringify(body)); // Just printing the return message as we subscribe.
console.log('subscribed to E6');
});
request(E7, function (err, res, body) {
if (err) {
console.log('Error :', err);
return;
}
console.log(' Body :', JSON.stringify(body)); // Just printing the return message as we subscribe.
console.log('subscribed to E7');
});
request(E8, function (err, res, body) {
if (err) {
console.log('Error :', err);
return;
}sleep.msleep(500);
console.log(' Body :', JSON.stringify(body)); // Just printing the return message as we subscribe.
console.log('subscribed to E9');
});
//3. Just a simple server waiting for the events.
var subServer = http.createServer(function(req, res){
var method = req.method; //GET / POST / DELETE / OPTIONS ...
//var body = req.body;
if(method == 'POST'){
var body = []; //Payload of the message;
req.on('data', function(chunk){
body.push(chunk);
var body2 = JSON.parse(body);
console.log("Event received:", body.toString()); //Here we have the body to analyze.
console.log("Event received:", body2.id);
if(body2.id=="valuechange1"){
ledStrip.all(255, 255, 255, 1);
ledStrip.sync();
}
if(body2.id=="DrawStartExecution"){
ledStrip.all(255, 255, 0, 1);
ledStrip.sync();
}
});
}
//responding for incoming event
res.statusCode = 200;
res.end();
}
);
//4. Starting the server on specified port.
subServer.listen(3000, "192.168.100.200", () => {
console.log("Server started");
});
然后,所有订阅都有效,但 E1 和 E8 除外。这就是我在控制台中得到的:
Server started
Error : { Error: read ECONNRESET
at _errnoException (util.js:1022:11)
at TCP.onread (net.js:615:25) code: 'ECONNRESET', errno: 'ECONNRESET', syscall: 'read' }
Error : { Error: read ECONNRESET
at _errnoException (util.js:1022:11)
at TCP.onread (net.js:615:25) code: 'ECONNRESET', errno: 'ECONNRESET', syscall: 'read' }
Body : {"id":"urn:uuid:5d18b8d3-44da-47f6-6ed5-0050c2899089"}
subscribed to E2
Body : {"id":"urn:uuid:925c66f4-8433-4be4-620c-0050c2899089"}
subscribed to E3
Body : {"id":"urn:uuid:db25d7f0-c8ad-4f1f-666d-0050c2899089"}
subscribed to E4
Body : {"id":"urn:uuid:19e5a4d2-0170-4399-6a0e-0050c2899089"}
subscribed to E5
Body : {"id":"urn:uuid:5ac590b7-4dc5-4748-6ee1-0050c2899089"}
subscribed to E6
Body : {"id":"urn:uuid:9f00acd1-8dfb-4b3f-621d-0050c2899089"}
subscribed to E7