Update server.js

This commit is contained in:
pancakes-proxy 2025-03-11 10:35:16 -04:00 committed by GitHub
parent a0a5f65941
commit 337d8a3da4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -12,45 +12,48 @@ app.use(express.static('public'));
// Chat state
let isChatLocked = false; // Lock state
let messageHistory = []; // Store messages
const users = {}; // { socketId: username }
const admins = {}; // { socketId: adminUsername }
// Store usernames for connected users
const users = {}; // Format: { socketId: username }
// Serve the admin page
// Serve admin panel
app.get('/admin', (req, res) => {
res.sendFile(__dirname + '/public/admin.html');
});
// Handle socket connections
// Handle connections
io.on('connection', (socket) => {
console.log('A user connected:', socket.id);
console.log('User connected:', socket.id);
// Set a default username when a user connects
// Set a default username for regular users
users[socket.id] = "Anonymous";
// Send lock status and message history to the connected client
socket.emit('chat lock status', isChatLocked);
socket.emit('chat history', messageHistory);
// Handle username updates
socket.on('set username', (username) => {
users[socket.id] = username || "Anonymous"; // Update username or default to "Anonymous"
users[socket.id] = username || "Anonymous";
console.log(`User ${socket.id} set their username to: ${users[socket.id]}`);
});
// Handle admin login
socket.on('admin login', (adminUsername) => {
admins[socket.id] = adminUsername; // Add admin to admin list
console.log(`Admin logged in: ${adminUsername}`);
});
// Handle chat messages
socket.on('chat message', (msg) => {
if (!isChatLocked) {
const username = users[socket.id] || "Anonymous"; // Get the sender's username
const formattedMessage = `${username}: ${msg}`; // Format as "username: message"
if (!isChatLocked || admins[socket.id]) { // Bypass lock for admins
const isAdmin = !!admins[socket.id];
const username = isAdmin ? `${admins[socket.id]} {admin}` : users[socket.id] || "Anonymous";
const formattedMessage = `${username}: ${msg}`;
messageHistory.push(formattedMessage); // Add to message history
io.emit('chat message', formattedMessage); // Broadcast to all clients
} else {
socket.emit('chat locked'); // Notify sender if chat is locked
socket.emit('chat locked'); // Notify regular users if the chat is locked
}
});
// Handle admin actions
// Handle admin-specific actions
socket.on('admin lock', () => {
isChatLocked = true;
io.emit('chat lock status', isChatLocked); // Notify all clients
@ -77,8 +80,9 @@ io.on('connection', (socket) => {
// Handle disconnection
socket.on('disconnect', () => {
console.log('A user disconnected:', socket.id);
delete users[socket.id]; // Remove the user from the list
console.log('User disconnected:', socket.id);
delete users[socket.id];
delete admins[socket.id]; // Remove from admins if applicable
});
});
@ -86,4 +90,3 @@ io.on('connection', (socket) => {
server.listen(3000, () => {
console.log('Server is running on http://localhost:3000');
});