-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
36 lines (30 loc) · 1.23 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
const express = require('express');
const http = require('http');
const socketIo = require('socket.io');
const app = express();
const server = http.createServer(app);
const io = socketIo(server);
app.use(express.static('public'));
const users = {};
io.on('connection', (socket) => {
socket.on('join', (username) => {
users[socket.id] = username;
io.emit('updateUserList', Object.values(users)); // Send the user list to all connected clients
// console.log('User joined:', username);
socket.broadcast.emit('chatMessage', { user: 'System', text: `${username} has joined the chat.` });
});
socket.on('disconnect', () => {
const username = users[socket.id];
delete users[socket.id];
io.emit('updateUserList', Object.values(users)); // Send the updated user list to all connected clients
socket.broadcast.emit('chatMessage', { user: 'System', text: `${username} has left the chat.` });
});
socket.on('chatMessage', (message) => {
const username = users[socket.id];
io.emit('chatMessage', { user: username, text: message });
});
});
const PORT = process.env.PORT || 3000;
server.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});