-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
120 lines (98 loc) · 3.56 KB
/
server.js
File metadata and controls
120 lines (98 loc) · 3.56 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// npm run dev
const http = require("http");
const express = require("express");
const socketio = require('socket.io');
const { addUser, removeUser } = require("./user");
const { addOrEditRoom, getRoom, removeRoom } = require("./room");
const app = express();
const server = http.createServer(app);
const io = socketio(server, {
cors: {
origin: "http://localhost:3000",
methods: ["GET", "POST"],
allowedHeaders: ["my-custom-header"],
credentials: true,
},
});
const PORT = process.env.PORT || 5010
io.on("connection", (socket) => {
socket.on("addRoom", ({room, game}, callBack) => {
const { curRoom, roomError } = addOrEditRoom({id: socket.id, name: room, game});
if (roomError) return callBack(roomError);
});
socket.on("join", ({ name, room, device, game }, callBack) => {
const { user, userError } = addUser({ id: socket.id, name, room, device });
if (userError) return callBack(userError);
console.log(`A connection has been made`);
socket.join(user.room);
callBack(null);
socket.on("getRoom", () => {
const { curRoom, roomError } = getRoom(room);
if (roomError) return callBack(roomError);
socket.emit("roomInfo", {
name: curRoom.name,
game: curRoom.game,
})
});
if (user.device === "controller"){
socket.broadcast
.to(user.room)
.emit("message", { user: "Admin", text: `${user.name} has joined!`});
callBack(null);
}
socket.on("sendMessage", ({message}) => {
io.to(user.room).emit("message", {
user: user.name,
text: message,
});
});
// THIS INFORMATION ONLY SHOWN AT VIEW
if (user.device === "controller"){
socket.broadcast
.to(user.room)
.emit("gamePlayers", { }); // SEND LIST OF CURRENT USER
callBack(null);
// get submission from controller, broadcast results to all
socket.on("controllerSubmission", ({input, game}) => {
// based on game, get output
// emit game output: gameState
});
}
socket.on("startGame", () => {
// based on game, get the gameInitState (sudoku board)
// NEED TO KNOW WHAT ROOM IS GIVEN WHICH BOARD
let gameInitState;
io.to(user.room).emit("gameInit", { gameInitState });
});
});
socket.on('disconnect', () => {
const user = removeUser(socket.id);
if (user !== undefined){
io.to(user.room).emit("message", {
user: "Admin",
text: `${user.name} just left the room`,
});
let roomUsers = io.in(user.room).fetchSockets();
if (roomUsers == 0){
removeRoom(user.room);
console.log(`Removing room ${user.room}`);
}
console.log(`A disconnection has been made`);
}
});
});
server.listen(PORT, () => console.log(`Server is connected to port ${PORT}`))
// ONLY ON VIEW SIDE
// if (user.device === "controller"){
// socket.broadcast
// .to(user.room)
// .emit("message", { user: "Admin", text: `${user.name} has joined!`});
// callBack(null);
// }
// ONLY ON CONTROLLER SIDE
// socket.on("sendMessage", () => {
// io.to(user.room).emit("message", {
// user: user.name,
// text: message,
// });
// });