-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
122 lines (102 loc) · 3.25 KB
/
server.js
File metadata and controls
122 lines (102 loc) · 3.25 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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
require('dotenv').config();
const express = require('express');
const http = require('http');
const socketIo = require('socket.io');
const path = require('path');
const { v4: uuidv4 } = require('uuid');
// Create Express app
const app = express();
const server = http.createServer(app);
const io = socketIo(server);
// Set view engine
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
// Serve static files
app.use(express.static(path.join(__dirname, 'public')));
// Store active rooms and users
const chatRooms = new Map();
// Routes
app.get('/', (req, res) => {
res.render('index');
});
app.get('/chat/new', (req, res) => {
const roomId = uuidv4();
chatRooms.set(roomId, { users: [] });
res.redirect(`/chat/${roomId}`);
});
app.get('/chat/:roomId', (req, res) => {
const roomId = req.params.roomId;
// Check if room exists
if (!chatRooms.has(roomId)) {
chatRooms.set(roomId, { users: [] });
}
res.render('chat', { roomId });
});
// Socket.io connections
io.on('connection', (socket) => {
let currentRoomId = null;
let currentUser = null;
// User joins a room
socket.on('join-room', ({ roomId, username }) => {
// Store user information
currentRoomId = roomId;
currentUser = {
id: socket.id,
username
};
// Add user to room
socket.join(roomId);
// Add user to room list
if (chatRooms.has(roomId)) {
chatRooms.get(roomId).users.push(currentUser);
} else {
chatRooms.set(roomId, { users: [currentUser] });
}
// Broadcast user joined message
socket.to(roomId).emit('user-joined', { username });
// Send current user list
io.to(roomId).emit('room-users', {
users: chatRooms.get(roomId).users
});
});
// Listen for chat messages
socket.on('send-message', ({ message }) => {
if (currentRoomId && currentUser) {
// Broadcast message to room
io.to(currentRoomId).emit('receive-message', {
userId: socket.id,
username: currentUser.username,
message,
time: new Date().toISOString()
});
}
});
// User disconnects
socket.on('disconnect', () => {
if (currentRoomId && currentUser) {
// Remove user from room
const room = chatRooms.get(currentRoomId);
if (room) {
room.users = room.users.filter(user => user.id !== socket.id);
// If room is empty, remove it
if (room.users.length === 0) {
chatRooms.delete(currentRoomId);
} else {
// Notify others that user left
socket.to(currentRoomId).emit('user-left', {
username: currentUser.username
});
// Update user list
io.to(currentRoomId).emit('room-users', {
users: room.users
});
}
}
}
});
});
// Start server
const PORT = process.env.PORT || 3000;
server.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});