forked from dontcallmedom/node-sharedcanvas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
85 lines (73 loc) · 2.52 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
var express = require('express');
var EventEmitter = require('events').EventEmitter;
var emitter = new EventEmitter();
var app = express.createServer();
var fs = require('fs');
var eventQueue = [];
var queueStart = 0;
app.configure(function(){
emitter.setMaxListeners(0);
app.use(express.logger());
app.set('port', process.env.PORT || 3000);
app.use(express.bodyParser());
app.use(express.static(__dirname + '/public', { maxAge: 86400}));
});
// Run on port 80 when in production mode
app.configure('production', function(){
app.use(express.errorHandler());
app.set('port', process.env.PORT || 80);
});
// receives connect events
app.post('/connect', function(req, res){
eventQueue.push({ua: req.body.ua});
emitter.emit("connect", req.body.ua);
res.end();
});
// receives draw events
app.post('/draw', function(req, res){
eventQueue.push({from: req.body.from, to: req.body.to});
emitter.emit("path", req.body.from, req.body.to, eventQueue.length);
res.end();
});
// receives clear signal
app.post('/clear', function(req, res){
queueStart = eventQueue.length;
emitter.emit("clear");
res.end();
});
app.get('/', function(req, res) {
fs.readFile(__dirname + '/public/index.html', 'utf8', function(err, text){
response.send(text);
});
});
// broadcast received draw/clear events
app.get('/stream', function(req, res) {
res.setHeader("Content-Type", 'text/event-stream');
res.setHeader("Cache-Control", "no-cache");
res.setHeader("Connection", "keep-alive");
res.writeHead(200);
if (req.headers["last-event-id"]) {
console.log("Last-Event-ID: " + req.headers["last-event-id"]);
for(var i=Math.max(queueStart,req.headers["last-event-id"]) ; i<eventQueue.length;i++) {
res.write("data: " + JSON.stringify({'from': eventQueue[i].from, 'to': eventQueue[i].to}) + "\n");
res.write("id:" + i + "\n\n");
}
}
// Heroku requires activity to avoid request timeout
setInterval(function() { res.write(":\n"); }, 30);
emitter.on("path", function(from, to, id) {
res.write("data: " + JSON.stringify({'from': from, 'to': to})+ "\n");
res.write("id: " + id + "\n\n");
});
emitter.on("clear", function() {
res.write("event: clear\n");
res.write("id: \n");
res.write("data: \n\n");
});
emitter.on("connect", function(ua) {
res.write("event: connect\n");
res.write("data: " + JSON.stringify({'ua': ua})+ "\n\n");
});
});
app.listen(app.set('port'));
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);