-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.js
58 lines (49 loc) · 1.6 KB
/
server.js
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
const express = require('express');
const app = express();
const http = require('http');
const server = http.createServer(app);
const { Server } = require('socket.io');
const io = new Server(server);
const path = require('path');
const { formatMessage } = require('./utils/message');
const {
joinUser,
getCurrentUser,
disconnectUser,
roomUsers,
} = require('./utils/user');
app.use(express.static(path.join(__dirname, 'public')));
const botName = 'ChatCord Bot';
io.on('connection', (socket) => {
socket.on('joinRoom', ({ username, room }) => {
const user = joinUser(socket.id, username, room);
socket.join(user.room);
socket.broadcast
.to(user?.room)
.emit('message', formatMessage(botName, `${user.username} joined`));
socket.emit('message', formatMessage(botName, 'Welcome to the ChatCord'));
// Sent the user list in the frontend
io.to(user?.room).emit('userList', {
room: user?.room,
users: roomUsers(user?.room),
});
});
socket.on('chatMessage', (msg) => {
const user = getCurrentUser(socket.id);
io.to(user?.room).emit('message', formatMessage(user.username, msg));
});
socket.on('disconnect', () => {
const user = getCurrentUser(socket.id);
disconnectUser(socket.id);
// Sent the user list in the frontend
io.to(user?.room).emit('userList', {
room: user?.room,
users: roomUsers(user?.room),
});
io.to(user?.room).emit(
'message',
formatMessage(botName, `${user?.username} has leave from the ChatCord`)
);
});
});
server.listen(3000, () => console.log('Project up and running on 3000'));