-
Notifications
You must be signed in to change notification settings - Fork 6
/
server.js
74 lines (59 loc) · 1.8 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
const sio = require('socket.io');
const uuid = require('node-uuid');
const open = require('open');
const express = require('express');
const app = express();
const http = require('http').Server(app);
app.use(express.static('src'));
// Create and configure socket.io
const io = sio.listen(http, { log: true });
const port = process.env.PORT || 8080;
http.listen(port, () => {
console.log('Listening on port :' + port);
open('http://localhost:' + port + '/station.html');
});
// keeping track of connections
const sockets = {};
io.sockets.on('connection', socket => {
let id = uuid.v4();
// we have a unique identifier that can be sent to the client
sockets[id] = socket;
socket.emit('your-id', id);
// remove references to the disconnected socket
socket.on('disconnect', () => {
sockets[socket] = undefined;
delete sockets[socket];
});
socket.on('set-room-name', data => {
id = data.name;
sockets[id] = socket;
socket.emit('your-id', id);
});
// when a message is received forward it to the addressee
socket.on('message', message => {
if (sockets[message.to]) {
sockets[message.to].emit('message', message);
} else {
socket.emit('disconnected', message.from);
}
});
// when a listener logs on let the media streaming know about it
socket.on('logon', message => {
if (sockets[message.to]) {
sockets[message.to].emit('logon', message);
}
});
socket.on('logoff', message => {
if (sockets[message.to]) {
sockets[message.to].emit('logoff', message);
} else {
socket.emit('error', 'Does not exsist at server.');
}
});
socket.on('image-data', message => {
sockets[message.to].emit('image-data', message);
});
socket.on('chat', data => {
sockets[data.station].broadcast.emit('chat', data);
});
});