-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrtrpServer.js
More file actions
199 lines (166 loc) · 5.1 KB
/
rtrpServer.js
File metadata and controls
199 lines (166 loc) · 5.1 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
const express = require("express");
const http = require("http");
const { Server } = require("socket.io");
const path = require("path");
const app = express();
const server = http.createServer(app);
// Room management
const rooms = new Map(); // Store all rooms
const MAX_ROOMS = 101;
// SOCKET.IO SETUP
const io = new Server(server, {
cors: {
origin: "*",
methods: ["GET", "POST"]
}
});
// FRONTEND FILES
// Serve frontend files
app.use(express.static(path.join(__dirname, "uiLayer")));
// Serve main UI page
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, "uiLayer", "homeView.html"));
});
//SOCKET LOGIC
io.on("connection", (socket) => {
console.log("User connected:", socket.id);
// Send current rooms list
socket.emit("rooms-list", Array.from(rooms.values()));
// Create room
socket.on("create-room", ({ roomName, password, isPrivate, maxUsers, owner }) => {
if (rooms.size >= MAX_ROOMS) {
socket.emit("error", "Maximum 101 rooms reached");
return;
}
if (rooms.has(roomName.trim().toLowerCase())) {
socket.emit("error", "Room name already exists");
return;
}
const room = {
id: roomName.trim().toLowerCase(),
name: roomName.trim(),
owner,
password: password || "",
isPrivate,
maxUsers,
users: [],
typingUsers: []
};
rooms.set(room.id, room);
// Notify creator that room is ready
socket.emit("room-created", room);
// Update rooms list for everyone
io.emit("rooms-list", Array.from(rooms.values()));
});
// Join room
socket.on("join-room", ({ roomId, username, password }) => {
const room = rooms.get(roomId);
if (!room) {
socket.emit("error", "Room not found");
return;
}
if (room.users.some(u => u.username === username)) {
socket.emit("error", "Username already in this room");
return;
}
if (room.users.length >= room.maxUsers) {
socket.emit("error", "Room is full");
return;
}
if (room.isPrivate && room.password !== password) {
socket.emit("error", "Incorrect password");
return;
}
// Add user to room
socket.join(roomId);
socket.currentRoom = roomId;
socket.username = username;
room.users.push({ id: socket.id, username });
rooms.set(roomId, room);
// Notify user
socket.emit("joined-room", room);
// Notify room with timestamp
io.to(roomId).emit("room-message", {
type: "system",
text: `${username} joined the room`,
timestamp: new Date()
});
// Update room info for all users in the room
io.to(roomId).emit("room-updated", room);
// Update rooms list for everyone
io.emit("rooms-list", Array.from(rooms.values()));
});
// Leave room
socket.on("leave-room", () => {
leaveRoom(socket);
});
// Send message
socket.on("chat-message", (msg) => {
if (!socket.currentRoom) return;
io.to(socket.currentRoom).emit("room-message", {
type: "user",
username: socket.username,
text: msg,
timestamp: new Date()
});
// Clear typing indicator
const room = rooms.get(socket.currentRoom);
if (room) {
room.typingUsers = room.typingUsers.filter(u => u !== socket.username);
io.to(socket.currentRoom).emit("typing-users", room.typingUsers);
}
});
// Typing indicator
socket.on("typing", (isTyping) => {
if (!socket.currentRoom) return;
const room = rooms.get(socket.currentRoom);
if (!room) return;
if (isTyping && !room.typingUsers.includes(socket.username)) {
room.typingUsers.push(socket.username);
} else if (!isTyping) {
room.typingUsers = room.typingUsers.filter(u => u !== socket.username);
}
io.to(socket.currentRoom).emit("typing-users", room.typingUsers);
});
// Disconnect
socket.on("disconnect", () => {
console.log("User disconnected:", socket.id);
leaveRoom(socket);
});
});
// Helper function to leave room
function leaveRoom(socket) {
if (!socket.currentRoom) return;
const roomId = socket.currentRoom;
const room = rooms.get(roomId);
if (!room) return;
const isOwner = room.owner === socket.username;
// If OWNER leaves the room delete whole room immediately
if (isOwner) {
// Notify all users in that room
socket.to(roomId).emit("room-deleted", roomId);
// Force all sockets to leave instantly
io.in(roomId).socketsLeave(roomId);
// Delete the room from Map
rooms.delete(roomId);
} else {
// Normal user leaving
room.users = room.users.filter(u => u.id !== socket.id);
room.typingUsers = room.typingUsers.filter(u => u !== socket.username);
io.to(roomId).emit("room-message", {
type: "system",
text: `${socket.username} left the room`,
timestamp: new Date()
});
io.to(roomId).emit("room-updated", room);
}
socket.leave(roomId);
socket.currentRoom = null;
// Update lobby room list for everyone
io.emit("rooms-list", Array.from(rooms.values()));
}
//SERVER START
const PORT = process.env.PORT || 4000;
server.listen(PORT, "0.0.0.0", () => {
console.log(`RTRP Server running on port ${PORT}`);
});