-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
105 lines (85 loc) · 2.85 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
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
const express = require("express");
const app = express();
const path = require("path");
const http = require("http");
const cors = require("cors");
const { Server } = require("socket.io");
const { addUser, getUsersInRoom, removeUser } = require("./users");
const SECRET_KEY = process.env.SECRET_KEY || "iamthere";
const homeDir = path.join(__dirname, "client", "build");
console.log(homeDir);
app.use(cors());
app.use(express.static(homeDir));
const port = process.env.PORT || 5000;
const server = http.createServer(app);
const io = new Server(server, {
cors: {
// origin: "https://daxter-drift.herokuapp.com",
origin: "http://localhost:3000",
methods: ["GET", "POST"],
},
});
// const io = new Server(server);
io.on("connection", (socket) => {
console.log(`User Connected: ${socket.id}`);
socket.on("join_room", (data, cb) => {
const user = addUser(socket.id, data.username, data.roomname);
// console.log(getUsersInRoom(data.roomname));
// if the username pre-exists, return error
if (user.error) {
console.log(user);
cb(user);
return;
}
// console.log("JOINING ROOM IN SERVER");
// If the user do not pre-exits, then admit it
socket.join(data.roomname);
// console.log(
// `User with ID: ${user.user.id}, name: ${user.user.username}, joined room: ${user.user.roomname}`
// );
// updating the user count in the room
const totalUsers = getUsersInRoom(data.roomname);
io.to(data.roomname).emit("meta_info", {
totalActiveUsers: totalUsers.length,
secretKey: SECRET_KEY,
});
// send the message to the other users
socket.to(data.roomname).emit("receive_message", {
uid: new Date().getMilliseconds(),
message: `${data.username}, has joined us 🎊`,
author: "admin",
});
});
socket.on("send_message", (data) => {
// console.log(data);
// here should the encryption run
socket.to(data.roomname).emit("receive_message", data);
});
socket.on("disconnect", () => {
// info of the user who left
const leftUser = removeUser(socket.id);
// console.log("User Disconnected", socket.id);
// if app reloads, on the main screen
if (leftUser === undefined) {
return;
}
// updating total number of users in the room
const totalUsers = getUsersInRoom(leftUser.roomname);
io.to(leftUser.roomname).emit("meta_info", {
totalActiveUsers: totalUsers.length,
secretKey: SECRET_KEY,
});
// sending broadcast of person leaving the room
socket.to(leftUser.roomname).emit("receive_message", {
uid: new Date().getMilliseconds(),
message: `${leftUser.username}, left the room!`,
author: "admin",
});
});
});
app.get("*", (req, res) => {
res.sendFile(path.join(homeDir, "index.html"));
});
server.listen(port, () => {
console.log(`Server is running on #${port}`);
});