-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
50 lines (40 loc) · 1.29 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
const express = require('express');
const { createServer } = require('http');
const { Server } = require('socket.io');
const PORT = process.env.PORT || 3001;
const app = express();
const httpServer = createServer(app);
const io = new Server(httpServer, {
cors: {
origin: 'https://alexbos.co',
},
});
io.on('connection', (socket) => {
console.log('Client connected (' + socket.id + ')');
socket.on('room:join', (roomId, displayName) => {
socket.join(roomId);
socket.to(roomId).emit('client:joined', { id: socket.id, displayName });
});
socket.on('room:members', (message) => {
console.log('Updating Room Members for client: ' + message.target);
io.to(message.target).emit('room:members', message.payload);
});
socket.on('game:start', (word) => {
console.log('Starting game in room ' + socket.id);
io.to(socket.id).emit('game:start', word);
});
socket.on('game:end', (roomId, gameStats) => {
io.to(roomId).emit('game:end', socket.id, gameStats);
});
socket.on('disconnecting', () => {
for (const roomId of socket.rooms) {
if (roomId !== socket.id) {
socket.to(roomId).emit('client:left', socket.id);
}
}
});
socket.on('disconnect', () =>
console.log('Client disconnected (' + socket.id + ')')
);
});
httpServer.listen(PORT, () => console.log(`Listening on ${PORT}`));