aboutsummaryrefslogtreecommitdiff
path: root/web/routes
diff options
context:
space:
mode:
authorivanshen <iwshen11@gmail.com>2019-03-05 20:58:03 +0000
committerivanshen <iwshen11@gmail.com>2019-03-05 20:58:03 +0000
commit0a573cc3e918e751f5a9e02f5877f7786636539d (patch)
treee4726c734926e0c2dc4fcf1af3c94367476bb458 /web/routes
parent786d0aacd27f27875568c7a89d9e1df918fadbf3 (diff)
Added live update functionality (feat 8)
Diffstat (limited to 'web/routes')
-rw-r--r--web/routes/index.js13
-rw-r--r--web/routes/sse.js15
2 files changed, 27 insertions, 1 deletions
diff --git a/web/routes/index.js b/web/routes/index.js
index b2d0796..6e6b920 100644
--- a/web/routes/index.js
+++ b/web/routes/index.js
@@ -2,7 +2,7 @@ var express = require('express');
var router = express.Router();
var Mockpoints = require('../models/mockpoints');
var Points = require('../models/points');
-
+const connections = [];
/* GET home page. */
router.get('/', function(req, res, next) {
// for now just redirect to /map
@@ -11,6 +11,7 @@ router.get('/', function(req, res, next) {
/* GET Map page. */
router.get('/map', function(req,res) {
+ // Stream.emit("push", "test", { msg: "admit one" });
// load the map with all the points
// Mockpoints.get_points(function(err, points){
Points.get_points(function(err, points){
@@ -36,10 +37,20 @@ router.post('/mobilerequest', function(req, res) {
console.log(`err inserting mobile request into db: ${err}`);
res.status(500).send({ error: "boo:(" });
} else {
+ for(var i = 0; i < connections.length; i++) {
+ connections[i].sseSend(data);
+ }
console.log(result);
res.status(200).send({'status': 'success', 'data': data});
}
});
+
});
+
+router.get('/stream', function(req, res){
+ res.sseSetup()
+ res.sseSend('ok');
+ connections.push(res);
+});
module.exports = router;
diff --git a/web/routes/sse.js b/web/routes/sse.js
new file mode 100644
index 0000000..5cd9a3f
--- /dev/null
+++ b/web/routes/sse.js
@@ -0,0 +1,15 @@
+module.exports = function (req, res, next) {
+ res.sseSetup = function() {
+ res.writeHead(200, {
+ 'Content-Type': 'text/event-stream',
+ 'Cache-Control': 'no-cache',
+ 'Connection': 'keep-alive'
+ })
+ }
+
+ res.sseSend = function(data) {
+ res.write("data: " + JSON.stringify(data) + "\n\n");
+ }
+
+ next()
+} \ No newline at end of file