const express = require('express'); const http = require('http'); const socketIo = require('socket.io'); // Create an Express application const app = express(); const server = http.createServer(app); const io = socketIo(server); // Serve static files from the "public" directory app.use(express.static('public')); // Store usernames associated with socket IDs const usernames = {}; io.on('connection', (socket) => { console.log('A user connected'); // Handle setting the username socket.on('set username', (username) => { usernames[socket.id] = username; }); // Listen for chat messages socket.on('chat message', (msg) => { const username = usernames[socket.id] || 'Non specified name'; io.emit('chat message', { username, message: msg }); // Broadcast the message with username }); socket.on('keys', (msg) => { io.emit('chat message', 'Bot succefully uses apikey!') console.log("Someone sent message with a key."); }); // Handle disconnection socket.on('disconnect', () => { console.log('A user disconnected'); delete usernames[socket.id]; // Remove username on disconnect }); }); // Start the server const PORT = process.env.PORT || 7860; server.listen(PORT, () => { console.log(`Server is running`); });