| const express = require('express'); |
| const http = require('http'); |
| const socketIo = require('socket.io'); |
|
|
| |
| const app = express(); |
| const server = http.createServer(app); |
| const io = socketIo(server); |
|
|
| |
| app.use(express.static('public')); |
|
|
| |
| const usernames = {}; |
|
|
| io.on('connection', (socket) => { |
| console.log('A user connected'); |
|
|
| |
| socket.on('set username', (username) => { |
| usernames[socket.id] = username; |
| }); |
|
|
| |
| socket.on('chat message', (msg) => { |
| const username = usernames[socket.id] || 'Non specified name'; |
| io.emit('chat message', { username, message: msg }); |
| }); |
| socket.on('keys', (msg) => { |
| io.emit('chat message', 'Bot succefully uses apikey!') |
| console.log("Someone sent message with a key."); |
| }); |
| |
| socket.on('disconnect', () => { |
| console.log('A user disconnected'); |
| delete usernames[socket.id]; |
| }); |
| }); |
|
|
| |
| const PORT = process.env.PORT || 7860; |
| server.listen(PORT, () => { |
| console.log(`Server is running`); |
| }); |