Implement Server-Sent-Events for device events
This commit is contained in:
parent
cf31cfa7b9
commit
1c56b5942f
26
lib/api.js
26
lib/api.js
|
@ -145,6 +145,32 @@ namespace("Cylon", function() {
|
|||
});
|
||||
});
|
||||
|
||||
this.server.get("/robots/:robotname/devices/:devicename/events/:eventname", function(req, res) {
|
||||
var robotname = req.params.robotname,
|
||||
devicename = req.params.devicename,
|
||||
eventname = req.params.eventname;
|
||||
|
||||
master.findRobotDevice(robotname, devicename, function(err, device) {
|
||||
if (err) { res.json(err); }
|
||||
|
||||
res.writeHead(200, {
|
||||
'Content-Type': 'text/event-stream',
|
||||
'Connection': 'keep-alive',
|
||||
'Cache-Control': 'no-cache'
|
||||
});
|
||||
|
||||
var writeData = function(data) {
|
||||
res.write("data: " + JSON.stringify(data) + "\n\n")
|
||||
};
|
||||
|
||||
device.on(eventname, writeData);
|
||||
|
||||
res.on('close', function() {
|
||||
device.removeListener(eventname, writeData);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
this.server.get("/robots/:robotname/devices/:devicename/commands", function(req, res) {
|
||||
var robotname = req.params.robotname,
|
||||
devicename = req.params.devicename;
|
||||
|
|
Loading…
Reference in New Issue