-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathserver-faye.js
47 lines (38 loc) · 1.1 KB
/
server-faye.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
var WebSocket = require('faye-websocket')
, http = require('http')
, path = require('path')
, fs = require('fs')
, canto1 = require('./divinecomedy.js').canto1;
var server = http.createServer(handler);
server.addListener('upgrade', function(request, socket, head) {
var ws = new WebSocket(request, socket, head);
function schedule(line) {
if (line < canto1.length)
setTimeout(function() {
if (ws) {
ws.send(canto1[line]);
schedule(line + 1);
}
}, 2000);
else if (ws)
ws.close();
}
ws.onclose = function(event) {
ws = null;
};
ws.send(canto1[0]);
schedule(1);
});
function handler (req, res) {
fs.readFile(path.resolve(__dirname, 'index-faye.html'),
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index-faye.html');
}
res.writeHead(200);
res.end(data);
}
);
}
server.listen(process.env.PORT || 8888);