-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
73 lines (61 loc) · 1.81 KB
/
index.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
const express = require("express");
const path = require("path");
const http = require("http");
const cors = require("cors");
const socketIO = require("socket.io");
const PORT = process.env.PORT || 4000;
const app = express();
const server = http.createServer(app);
const io = socketIO(server, {
cors: {
origins: ["https://localhost:3000"],
credentials: true,
},
});
app.use(cors());
let users = [];
//Add this before the app.get() block
io.on("connection", (socket) => {
console.log(`⚡: ${socket.id} user just connected!`);
//Listens and logs the message to the console
socket.on("message", (data) => {
console.log(data);
});
//sends the message to all the users on the server
socket.on("message", (data) => {
io.emit("messageResponse", data);
});
//Listens when a new user joins the server
socket.on("newUser", (data) => {
//Adds the new user to the list of users
users.push(data);
// console.log(users);
//Sends the list of users to the client
io.emit("newUserResponse", users);
});
socket.on("disconnect", () => {
console.log("🔥: A user disconnected");
//Updates the list of users when a user disconnects from the server
users = users.filter((user) => user.socketID !== socket.id);
// console.log(users);
//Sends the list of users to the client
io.emit("userLeft", users);
socket.disconnect();
});
});
// app.get('/api', (req, res) => {
// res.json({
// message: 'Hello world',
// });
// });
//serve static assets in production
if (process.env.NODE_ENV === "production") {
//set static folder
app.use(express.static("client/build"));
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "client", "build", "index.html"));
});
}
server.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});