-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
207 lines (184 loc) · 5.55 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
const fs = require('fs');
const os = require('os');
const hostname = '0.0.0.0';
const keys = './keys';
const port = 8080;
const protocol = 'https';
const root = '/dist';
const mimeTypes = {
'css': 'text/css',
'html': 'text/html',
'jpeg': 'image/jpeg',
'jpg': 'image/jpeg',
'js': 'text/javascript',
'png': 'image/png',
'svg': 'image/svg+xml',
};
const listeners = {};
const stations = {};
const localIp = os.networkInterfaces()['en0']
.filter(interface => interface.family === 'IPv4')
.map(interface => interface.address)[0];
function createServer(proto, handler) {
if (proto === 'https') {
return require('https').createServer({
key: fs.readFileSync(`${keys}/client-key.pem`),
cert: fs.readFileSync(`${keys}/client-cert.pem`)
}, handler);
} else {
return require('http').createServer(handler);
}
}
function handleRequest(req, res) {
if (req.url === '/') {
req.url = '/index.html';
}
loadFile(__dirname + root + req.url, req, res);
}
function loadFile(path, req, res) {
console.log(`server: ${path}`);
fs.readFile(path, (err, data) => {
const mimeType = mimeTypes[path.split('.').pop()];
if (err) {
if (mimeType) {
res.writeHead(404);
res.end(JSON.stringify(err));
} else {
loadFile(__dirname + root + '/index.html', req, res);
}
return;
}
res.writeHead(200, {'Content-Type': mimeType || 'text/plain'});
res.end(data);
});
}
function exists(stationId) {
return stations[stationId];
}
function isOwner(stationId, socket) {
return stations[stationId] && stations[stationId].owner.id === socket.id;
}
function handleSockets(socket) {
// connect to radio
console.log('connect', socket.id);
listeners[socket.id] = {
id: socket.id,
name: null,
owns: null,
};
socket.emit('connected', socket.id, localIp);
socket.emit('stations.updated', stations);
io.emit('listeners.updated', listeners);
// disconnect from radio
socket.on('disconnect', () => {
console.log('disconnect', socket.id);
const myStationId = listeners[socket.id].owns;
if (typeof myStationId === 'string') {
delete stations[myStationId];
io.emit('stations.updated', stations);
}
if (listeners[socket.id]) {
delete listeners[socket.id];
io.emit('listeners.updated', listeners);
}
socket.emit('disconnected', socket.id);
});
// add a station
socket.on('add', (stationId, stationName) => {
if (!exists(stationId)) {
console.log('add', stationId);
listeners[socket.id].owns = stationId;
stations[stationId] = {
broadcasting: false,
id: stationId,
listeners: {},
name: stationName,
owner: listeners[socket.id],
time: 0
};
socket.emit('added', stationId);
io.emit('stations.updated', stations);
}
});
// remove a station
socket.on('remove', (stationId) => {
if (exists(stationId) && isOwner(stationId, socket)) {
console.log('remove', stationId);
listeners[socket.id].owns = null;
delete stations[stationId];
socket.emit('removed', stationId);
io.emit('stations.updated', stations);
}
});
// update a station
socket.on('update', (stationId, stationName) => {
if (exists(stationId) && isOwner(stationId, socket)) {
console.log('update', stationId);
stations[stationId].name = stationName;
socket.emit('updated', stationId);
io.emit('stations.updated', stations);
}
});
// join a station
socket.on('join', (stationId) => {
if (exists(stationId)) {
console.log('join', stationId);
stations[stationId].listeners[socket.id] = listeners[socket.id];
socket.join(stationId);
socket.emit('joined', stationId);
io.to(stationId).emit('listener.joined', socket.id);
io.emit('stations.updated', stations);
}
});
// leave a station
socket.on('leave', (stationId) => {
if (exists(stationId)) {
console.log('leave', stationId);
delete stations[stationId].listeners[socket.id];
socket.leave(stationId);
socket.emit('left', stationId);
io.emit('listener.left', socket.id);
io.emit('stations.updated', stations);
}
});
// start a broadcast
socket.on('start', (stationId) => {
if (exists(stationId) && isOwner(stationId, socket)) {
stations[stationId].broadcasting = true;
stations[stationId].time = new Date().getTime();
socket.emit('started', stationId);
io.emit('stations.updated', stations);
}
});
// stop a broadcast
socket.on('stop', (stationId) => {
if (exists(stationId) && isOwner(stationId, socket)) {
stations[stationId].broadcasting = false;
stations[stationId].time = 0;
socket.emit('stopped', stationId);
io.emit('stations.updated', stations);
}
});
// update listener name
socket.on('updateName', (listenerName) => {
listeners[socket.id].name = listenerName;
io.emit('listeners.updated', listeners);
});
// negotiate audio
socket.on('offer', (offer, recipientId) => {
io.to(recipientId).emit('offer', offer, socket.id);
});
socket.on('answer', (answer, recipientId) => {
io.to(recipientId).emit('answer', answer, socket.id);
});
socket.on('candidate', (candidate, recipientId) => {
io.to(recipientId).emit('candidate', candidate, socket.id);
});
}
// setup http server
const app = createServer(protocol, handleRequest);
app.listen(port, hostname);
console.log(`server: ${protocol}://${hostname}:${port}`);
// setup websockets
let io = require('socket.io')(app);
io.on('connection', handleSockets);