-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
49 lines (41 loc) · 1.25 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
const http = require("http");
const express = require("express");
const path = require("path"); // Import the path module
const socketio = require("socket.io");
const app = express();
//Express STuff
app.use("/static", express.static("static"));
const server = http.createServer(app);
const io = socketio(server);
const users = {};
io.on("connection", (socket) => {
socket.on("new-user-joined", (name) => {
users[socket.id] = name;
socket.broadcast.emit("user-joined", name);
});
socket.on("send", (message) => {
socket.broadcast.emit("receive", {
message: message,
name: users[socket.id],
});
});
socket.on("disconnect", (message) => {
socket.broadcast.emit("left", users[socket.id]);
delete users[socket.id];
});
});
// Define the path to the current directory
const currentDirPath = __dirname;
// End Points
app.get("/", (req, res) => {
// Serve the index.html file from the current directory
res.sendFile("index.html", { root: currentDirPath });
});
app.get("/users", (req, res) => {
res.json({ length: Object.keys(users).length });
});
const PORT = process.env.PORT || 3000;
server.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
module.exports = Object.keys(users).length;