-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
53 lines (46 loc) · 1.41 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
const express = require("express");
const bodyParser = require("body-parser");
const app = express();
const port = process.env.PORT || 5000;
const io = require("socket.io", {
transports: ["websocket"],
upgrade: false
})();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// Frontend: Run the React app
const path = require('path');
app.use(express.static(path.join(__dirname, 'client/build')));
app.get('/', function(req, res) {
res.sendFile(path.join(__dirname, 'client/build', 'index.html'));
});
// Set up websockets
io.on("connection", function(socket) {
console.log(
"Client #" +
io.engine.clientsCount +
" just connected. Socket ID is " +
socket.id
);
socket.on("chat message", function(msg) {
console.log(
"Incoming chat message from " + socket.id + ": " + JSON.stringify(msg)
);
io.emit("chat message", msg);
});
socket.on("disconnect", function() {
console.log(
"Client #" +
io.engine.clientsCount +
" (socket ID " +
socket.id +
") just DISCONNECTED. Number of remaining clients is " +
io.engine.clientsCount
);
socket.emit("chat message", "Clinet " + socket.id + " says good bye!");
});
});
const websocket_port = 8000;
io.listen(websocket_port);
console.log("Websockets listening on port ", websocket_port);
app.listen(port, () => console.log(`Listening on port ${port}`));