-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
52 lines (42 loc) · 1.42 KB
/
index.js
File metadata and controls
52 lines (42 loc) · 1.42 KB
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
const express = require('express');
const path = require('path');
const app = express();
const { Server } = require('ws');
app.use(express.json());
if (process.env.NODE_ENV === "production") {
// Set static folder
app.use(express.static(path.join(__dirname, 'munchkin/build')));
app.get('*', function (req, res) {
res.sendFile(path.join(__dirname, 'munchkin/build', 'index.html'));
});
} else {
app.use(express.static(path.join(__dirname, 'munchkin/build')));
app.get('/', function (req, res) {
res.sendFile(path.join(__dirname, 'munchkin/build', 'index.html'));
});
}
const PORT = process.env.PORT || 5000;
const server = app.listen(PORT, () => console.log(`Server started on port ${PORT}`));
const wss = new Server({ server, path: '/munckin' });
// ws
wss.on('connection', (ws, req) => {
//console.log(ws);
let roomNumber = 'null';
let temp = req.url.split('?')
if (temp[1] !== undefined) temp = temp[1].split('=');
if (temp[0] === 'room') roomNumber = temp[1];
ws.room = roomNumber;
// console.log(ws.room);
ws.on('message', msg => {
// console.log(msg);
if (msg.data != 'connect')
wss.clients.forEach(
element => {
if (element.room == ws.room && element != ws) element.send(msg)
}
);
});
ws.on('close', (ws) => {
// console.log(wss.clients.size);
});
});