-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
55 lines (43 loc) · 1.21 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
/*!
* app.js is the server component of slippery
*
*/
var Express = require('express');
var WebSocket = require('faye-websocket');
var PORT = 8080;
/*
* set up the application
*/
var app = module.exports = Express.createServer();
//import the socket.io module and start listening
app.configure(function () {
//setup view engine
//setup bodyParser, cookieParser and sessionStore
app.use(Express['static'](__dirname + '/public'));
app.use(app.router);
});
var connections = [];
function broadcast(event) {
connections.forEach(function (socket) {
console.log('sending ' + event.data);
socket.send(event.data);
});
}
//rewrite and use socket.io
app.addListener('upgrade', function(request, socket, head) {
console.log('upgrade');
var ws = new WebSocket(request, socket, head);
connections.push(ws);
ws.onmessage = function (event) {
broadcast(event);
};
ws.onclose = function (event) {
console.log('close', event.code, event.reason);
connections = connections.splice(connections.indexOf(ws));
ws = null;
};
});
//implement routes
app.listen(PORT, function () {
console.log('Server listening on port: http://localhost:' + PORT);
});