-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.js
89 lines (83 loc) · 2.51 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
const express = require('express')
const {Server} = require('socket.io')
const app = express()
const http = require('http');
const ACTIONS = require('./src/Action');
const path = require('path');
const server = http.createServer(app);
const io = new Server(server);
app.use(express.static('build'));
app.use((req, res, next) => {
res.sendFile(path.join(__dirname, 'build','index.html'));
})
const getAllConnectedClients = (id) => {
return Array.from(io.sockets.adapter.rooms.get(id) || []).map((socketId) => {
return {
socketId,
username: userSocketMap[socketId]
}
})
}
const userSocketMap = {};
const doubts = {};
io.on('connection', (socket)=>{
console.log('connected', socket.id);
socket.on(ACTIONS.JOIN, ({id, username})=> {
userSocketMap[socket.id] = username;
socket.join(id);
socket.emit('user-joined', {
socketId: socket.id,
username: username
})
console.log(socket.id, username);
const clients = getAllConnectedClients(id);
clients.forEach(( {socketId} )=>{
io.to(socketId).emit(ACTIONS.JOINED, {
clients,
username,
socketId: socket.id
});
})
});
socket.on('lock_access', ({id, access}) => {
console.log(access);
const clients = getAllConnectedClients(id);
clients.forEach(({socketId}) => {
io.to(socketId).emit('access_change', {
access
})
})
})
socket.on(ACTIONS.DOUBT, ({id, username, doubt}) => {
doubts[username] = doubt;
console.log(doubts);
const clients = getAllConnectedClients(id);
clients.forEach(( {socketId} )=>{
io.to(socketId).emit(ACTIONS.DOUBT, {
doubts,
username,
socketId: socket.id
});
})
})
socket.on(ACTIONS.CODE_CHANGE, ({id, code})=> {
socket.in(id).emit(ACTIONS.SYNC_CODE, {
code
})
})
socket.on('disconnecting', ()=> {
const rooms = [...socket.rooms];
rooms.forEach((roomId)=> {
socket.in(roomId).emit(ACTIONS.DISCONNECTED, {
socketId: socket.id,
username: userSocketMap[socket.id]
});
})
delete userSocketMap[socket.id];
socket.leave();
})
})
const PORT = process.env.PORT || 8000;
server.listen(PORT, ()=> {
console.log("Connected server successfully!");
})