-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
50 lines (44 loc) · 1.23 KB
/
server.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
var express = require('express'),
http = require('http'),
app = express(),
server = http.createServer(app),
io = require('socket.io').listen(server);
var sockets = {};
var time = function () {
return new Date().getTime();
};
io.on('connection', function (socket) {
console.log("We have a connection! Socket ID: " + socket.id);
for (var k in sockets) {
var s = sockets[k];
s.disconnect();
delete sockets[k];
}
sockets[socket.id] = socket;
socket.emit('data', {
notice: "New Socket.IO connection.",
session_id: socket.id,
time: time()
});
});
io.on('blah', function (data) {
console.log("Got blah: " + data);
})
app.configure(function () {
app.use(express.static(__dirname + '/public'));
app.use(express.logger('dev'));
app.use(express.bodyParser());
});
app.put('/push/:id', function (req, res) {
console.log("PUT to /push/" + req.params.id);
var id = req.params.id;
if (sockets[id]) {
sockets[id].emit('data', req.body);
res.send("OK");
} else {
res.status(404).send("Session ID not found");
}
});
var port = process.env.PORT || 3000;
server.listen(port);
console.log("Server running on port " + port);