-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
133 lines (111 loc) · 3.39 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
const express = require('express');
const http = require('http');
const {v4: uuidv4} = require('uuid');
const cors = require('cors');
const PORT = process.env.PORT || 5000;
const app = express();
const server = http.createServer(app);
app.use(cors());
const io = require('socket.io')(server, {
cors: {
origin: '*',
methods: ['GET', 'POST']
}
});
let connectedUsers = [];
let rooms = [];
app.get('/api/room-exists/:roomID', (req, res) => {
const { roomID } = req.params;
const room = rooms.find((room) => room.id === roomID);
if(room) {
return res.send({ roomExists: true })
} else {
return res.send({ roomExists: false })
}
});
io.on('connection', (socket) => {
console.log(`User Connected: ${socket.id}`);
socket.on('create-new-room', (data) => {
createNewRoomHandler(data, socket);
});
socket.on('join-room', (data) => {
joinRoomHandler(data, socket);
});
socket.on('disconnect', () => {
disconnectHandler(socket);
});
socket.on('connection-signal', data => {
signalingHandler(data, socket);
});
socket.on('connection-init', data => {
initializeConnectionHandler(data, socket);
});
});
const createNewRoomHandler = (data, socket) => {
const { username } = data;
const roomID = uuidv4();
const newUser = {
username,
id: uuidv4(),
socketID: socket.id,
roomID
};
connectedUsers = [...connectedUsers, newUser];
const newRoom = {
id: roomID,
connectedUsers: [newUser]
}
socket.join(roomID);
rooms = [...rooms, newRoom];
socket.emit('room-id', roomID);
socket.emit('room-update', { connectedUsers: newRoom.connectedUsers });
};
const joinRoomHandler = (data, socket) => {
const {username, roomID} = data;
const newUser = {
username,
id: uuidv4(),
socketID: socket.id,
roomID
};
const room = rooms.find(room => room.id === roomID);
room.connectedUsers = [...room.connectedUsers, newUser];
socket.join(roomID);
connectedUsers = [...connectedUsers, newUser];
room.connectedUsers.forEach(user => {
if(user.socketID !== socket.id) {
const data = {
connectedUserSocketID: socket.id
};
io.to(user.socketID).emit('prepare-connection', data);
}
});
io.to(roomID).emit('room-update', { connectedUsers: room.connectedUsers });
};
const disconnectHandler = (socket) => {
const user = connectedUsers.find((user) => user.socketID === socket.id);
if(user) {
const room = rooms.find(room => room.id === user.roomID);
room.connectedUsers = room.connectedUsers.filter((user) => user.socketID !== socket.id);
socket.leave(user.roomID);
if(room.connectedUsers.length > 0) {
io.to(room.id).emit('user-disconnected', { socketID: socket.id });
io.to(room.id).emit('room-update', { connectedUsers: room.connectedUsers });
} else {
rooms = rooms.filter(room => room.id !== room.id);
}
}
}
const signalingHandler = (data, socket) => {
const { connectedUserSocketID, signal } = data;
const signalingData = {signal, connectedUserSocketID: socket.id };
io.to(connectedUserSocketID).emit('connection-signal', signalingData);
};
const initializeConnectionHandler = (data, socket) => {
const {connectedUserSocketID } = data;
const initData = {connectedUserSocketID: socket.id};
io.to(connectedUserSocketID).emit('connection-init', initData);
};
server.listen(PORT, () => {
console.log(`Server is started at PORT:${PORT}`);
});