-
Notifications
You must be signed in to change notification settings - Fork 14
/
server-socketio.js
54 lines (46 loc) · 1.57 KB
/
server-socketio.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
var app = require('http').createServer(handler)
, io = require('socket.io').listen(app)
, path = require('path')
, fs = require('fs')
, canto1 = require('./divinecomedy.js').canto1;
io.configure(function() {
io.set('transports', [ 'websocket' ]);
if (process.env.IISNODE_VERSION) {
// If this node.js application is hosted in IIS, assume it is hosted
// in IIS virtual directory named 'dante' and set up the socket.io's resource
// value for socket.io to recognize requests that target it.
// Note a corresponding change in the client index-socketio.html, as well
// as necessary URL rewrite rule in web.config.
io.set('resource', '/dante/socket.io');
}
});
function handler (req, res) {
fs.readFile(path.resolve(__dirname, 'index-socketio.html'),
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index-socketio.html');
}
res.writeHead(200);
res.end(data);
}
);
}
io.sockets.on('connection', function (socket) {
function schedule(line) {
if (line < canto1.length)
setTimeout(function() {
if (socket) {
socket.emit('divinecomedy', canto1[line]);
schedule(line + 1);
}
}, 2000);
else if (socket) {
socket.disconnect();
socket = null;
}
}
socket.emit('divinecomedy', canto1[0]);
schedule(1);
});
app.listen(process.env.PORT || 8888);