-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
67 lines (53 loc) · 1.54 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
/*jshint esversion: 6*/
const express = require('express');
const app = express();
const http = require('http').Server(app);
const io = require('socket.io')(http);
const http_port = process.env.PORT || 3000;
let waiting = [];
let matched = [];
app.use('/', express.static('root'));
app.use('/', express.static('views'));
app.use('/assets', express.static('assets'));
app.use('/dist', express.static('dist'));
http.listen(http_port, function(){
console.log('listening on *:3000');
});
io.on('connection', function(socket){
let uid = socket.handshake.query.uid;
join(uid, socket);
socket.on('disconnect', function(){
leave(uid, socket);
});
socket.on('user.update', function(data){
io.to(data.room).emit('user.update', data);
});
console.log({matched: matched.length, waiting: waiting.length});
});
const join = (uid, socket)=>{
let room = {};
if (waiting.length > 0) {
room = waiting.pop();
room.users.push(uid);
matched.push(room);
waiting = [];
}else{
room.name = matched.length + 1;
room.users = [uid];
waiting.push(room);
}
socket.join(room.name);
io.to(room.name).emit('user.joined', {id: uid, room: room.name});
};
const leave = (uid, socket) => {
// - Looks for the room with the given uid (inneficient lookup for now).
matched.forEach(function(match_room, room_idx){
let user_in_room = match_room.users.some(function(user){
return (user == uid);
});
if (user_in_room) {
io.to(match_room.name).emit('user.disconnected');
matched.splice(room_idx, 1);
}
});
};