-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
74 lines (61 loc) · 1.99 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
const path = require('path');
const express = require('express');
const app = express();
const http = require('http').createServer(app);
const io = require('socket.io')(http);
const shortid = require('shortid');
app.use(express.static(path.join(__dirname, 'build')));
app.get('*', (_, res) => {
const filePath = path.join(__dirname, 'build/index.html');
res.sendFile(filePath);
});
const getUsersToUsernames = (roomCode) => {
const room = io.sockets.adapter.rooms[roomCode];
const roomUsers = {};
if (room && room.sockets) {
for (const clientId of Object.keys(room.sockets)) {
const {
userCode,
username,
} = io.sockets.connected[clientId];
roomUsers[userCode] = username || null;
}
}
return roomUsers;
}
io.on('connection', (socket) => {
socket.on('join-room', (roomCode) => {
socket.join(roomCode);
io.to(roomCode).emit('users-changed', getUsersToUsernames(roomCode));
});
socket.on('leave-room', (roomCode) => {
socket.leave(roomCode);
io.to(roomCode).emit('users-changed', getUsersToUsernames(roomCode));
});
socket.on('create-room', (respond) => {
const roomCode = shortid.generate();
return respond(roomCode);
});
socket.on('set-user-code', (userCode) => {
socket.userCode = userCode;
});
socket.on('update-username', (roomCode, username) => {
socket.username = username;
io.to(roomCode).emit('users-changed', getUsersToUsernames(roomCode));
});
socket.on('start-game', (roomCode, board) => {
io.to(roomCode).emit('start-game', board);
});
socket.on('update-mouse-position', (roomCode, position) => {
io.to(roomCode).emit(
'mouse-position-updated',
socket.userCode,
position,
);
});
});
const port = process.env.PORT || 5000;
http.listen(
port,
() => console.log(`game server listening at post:${port}`)
);