-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
53 lines (41 loc) · 1.58 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 path = require("path");
const express = require("express");
const app = express();
const port = process.env.PORT || 3000;
// NGINX serving front end static folder
// Set static folder for front-end
// app.use(express.static("./client"));
// Create a server const for socket.io to use
// Normally we just do app.listen here
const server = app.listen(port, () => {
console.log(`Listening on port: ${port} \n`);
});
// Attach socket.io to the server
const io = require("socket.io")(server);
// Import socket handlers
const registerUserNameHandlers = require("./socket_logic/userNameHandler.js");
const registerRoomHandlers = require("./socket_logic/roomHandler.js");
const registerMessageHandlers = require("./socket_logic/messageHandler.js");
const registerDisconnectHandlers = require("./socket_logic/disconnectHandler.js");
// Server side objects
const users = new Set();
const rooms = new Set();
const roomList = new Set();
const onConnection = (socket) => {
// console.log(`Socket: ${socket.id} connected \n`);
socket.emit("connected");
io.emit("updateRoomList", [...roomList]);
// User Name Handlers
registerUserNameHandlers(io, socket, users);
// Room Handlers
registerRoomHandlers(io, socket, users, rooms, roomList);
// Message Handlers
registerMessageHandlers(io, socket, users);
// Disconnect Handler
registerDisconnectHandlers(io, socket, users, rooms, roomList);
};
io.on("connection", onConnection);
// NGINX is serving our index.html directly
// app.get("/simplesockets", function (req, res) {
// res.sendFile(path.join(__dirname, "./client/index.html"));
// });