Implement Server-Sent-Events for device events

This commit is contained in:
Andrew Stewart 2014-04-08 13:21:03 -07:00
parent cf31cfa7b9
commit 1c56b5942f
1 changed files with 26 additions and 0 deletions

View File

@ -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;