File size: 1,321 Bytes
7c9e5f0
 
4602eff
7c9e5f0
4602eff
 
 
 
7c9e5f0
4602eff
 
7c9e5f0
90cf2c6
 
 
7c9e5f0
4602eff
 
90cf2c6
 
 
 
 
4602eff
7c9e5f0
948b936
90cf2c6
7c9e5f0
5a700f8
6f284bc
5a700f8
 
4602eff
 
 
90cf2c6
7c9e5f0
 
 
4602eff
c388863
4602eff
9076c96
7c9e5f0
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
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`);
});