-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.3.js
46 lines (35 loc) · 1.22 KB
/
index.3.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
const Koa = require('koa');
const serve = require('koa-static');
const websockify = require('koa-websocket');
const cors = require('kcors');
const UUID = require('uuid/v4');
const WEB_PORT = process.env.WEB_PORT || 8080;
const app = websockify(new Koa());
app.use(cors({
origin: '*'
}));
app.use(serve('./public'));
const connections = new Map();
app.ws.use(async (ctx, next) => {
const uuid = UUID();
connections.set(uuid, ctx.websocket);
ctx.websocket.on('close', () => {
connections.delete(uuid);
});
ctx.websocket.on('message', (data) => {
const message = JSON.parse(data);
switch(message.type){
case 'offer':
connections.get(message.to) && connections.get(message.to).send(JSON.stringify(Object.assign(message, { from: uuid })));
break;
}
});
ctx.websocket.send(JSON.stringify({ type: 'welcome', uuid }));
//tell all exsiting connections about this new connection
for(let [id, connection] of connections){
id !== uuid && connection.send(JSON.stringify({ type: 'join', from: uuid }));
}
});
app.listen(WEB_PORT, () => {
console.info(`Server listening on port ${WEB_PORT}`);
})