-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.js
More file actions
39 lines (29 loc) · 1.11 KB
/
app.js
File metadata and controls
39 lines (29 loc) · 1.11 KB
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
// ----------------------------------------------------------------------------
var express = require('express');
var app = express();
app.use(express.static('public', {'extensions': ['html']}));
app.use('/pnacl', express.static('pnacl'));
app.use('/app', express.static('public/app'));
// ----------------------------------------------------------------------------
var server = app.listen(3000, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});
// ----------------------------------------------------------------------------
var WebSocketServer = require('ws').Server;
var wss = new WebSocketServer({server: server, path: '/ws'});
wss.broadcast = function(sender, data) {
for(var i in wss.clients) {
var client = wss.clients[i];
if (client !== sender) {
wss.clients[i].send(data);
}
}
};
wss.on('connection', function(ws) {
ws.on('message', function(message) {
console.log('received: %s', message);
wss.broadcast(ws, message);
});
});