forked from ccoenraets/react-trader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
46 lines (39 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
var express = require('express'),
app = express(),
path = require('path'),
http = require('http').Server(app),
io = require('socket.io')(http),
feed = require('./feed');
app.use(express.static(path.join(__dirname, './www')));
io.on('connection', function (socket) {
console.log('User connected. Socket id %s', socket.id);
socket.on('join', function (rooms) {
console.log('Socket %s subscribed to %s', socket.id, rooms);
if (Array.isArray(rooms)) {
rooms.forEach(function(room) {
socket.join(room);
});
} else {
socket.join(rooms);
}
});
socket.on('leave', function (rooms) {
console.log('Socket %s unsubscribed from %s', socket.id, rooms);
if (Array.isArray(rooms)) {
rooms.forEach(function(room) {
socket.leave(room);
});
} else {
socket.leave(rooms);
}
});
socket.on('disconnect', function () {
console.log('User disconnected. %s. Socket id %s', socket.id);
});
});
feed.start(function(room, type, message) {
io.to(room).emit(type, message);
});
http.listen(3000, function () {
console.log('listening on: 3000');
});