-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
48 lines (38 loc) · 1.38 KB
/
server.js
File metadata and controls
48 lines (38 loc) · 1.38 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
const express = require("express");
const path = require("path");
const app = express();
const PORT = 5000;
const server = require("http").createServer(app);
const io = require("socket.io")(server);
app.use(express.static(path.join(__dirname, "public")));
let users = [];
// Handle new socket connections
io.on("connection", function(socket){
// Handle a new user joining
socket.on("newuser", function(username){
users.push(username);
// console.log(users + "joined");
// Notify other users that a new user has joined
socket.broadcast.emit("update", username + " joined");
// Send the updated user list to all clients
io.emit("userList", users);
});
// Handle a user leaving
socket.on("exituser", function(username){
users = users.filter(user => user !== username);
// console.log(users + "left");
// Notify other users that a user has left
socket.broadcast.emit("update", username + " left");
// Send the updated user list to all clients
io.emit("userList", users);
});
// Handle incoming chat messages
socket.on("chat", function(message){
// Broadcast the message to all other users
socket.broadcast.emit("chat", message);
});
});
// Start the server
server.listen(PORT, '0.0.0.0', () => {
console.log(`Server is running on port ${PORT}`);
});