-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
38 lines (32 loc) · 973 Bytes
/
index.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
const express = require('express');
const path = require('path');
const app = express()
const http = require('http').Server(app)
const io = require('socket.io')(http, {
serveClient: false,
});
const { SocketState } = require('./socket-state')
const RoomManager = require('./room');
RoomManager.setIO(io);
app.use(express.static(path.resolve('client', 'build')))
app.get("*", function(request, response) {
response.sendFile(path.resolve('client', 'build', 'index.html'));
})
const port = process.env.PORT || 3000
http.listen(port, () => {
console.log(`example app listening at *:${port}`)
})
io.on('connect', (socket) => {
console.log("Connected");
RoomManager.onConnect(socket)
socket.on('disconnect', () => {
SocketState.delete(socket.id)
})
socket.on('setName', (name) => {
const data = SocketState.get(socket.id) || {}
SocketState.set(socket.id, {
...data,
name,
})
})
});