-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
68 lines (48 loc) · 1.61 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
59
60
61
62
63
64
65
66
67
68
const path = require('path');
const http = require('http');
const express = require('express');
const socketio = require('socket.io');
const app = express();
const server = http.createServer(app);
const io = socketio(server);
const formatMessage = require('./utils/messages');
const {userJoin, getCurrentUser, userLeavesChat, getRoomUsers} = require('./utils/users');
const port = process.env.PORT || 3000;
const botname = 'Chaton Bot :'
app.use(express.static(path.join(__dirname, 'public')));
io.on('connection', (socket) => {
socket.on('joinRoom', ({username, room}) => {
const user = userJoin(socket.id, username, room)
socket.join(user.room)
//when a new user connects
socket.emit('message', formatMessage(botname,'Welcome to chaton'));
socket.broadcast
.to(user.room)
.emit('message', formatMessage(botname,`${user.username} has joined the chat`));
io.to(user.room).emit('roomUsers',{
room:user.room,
users: getRoomUsers(user.room)
})
//when a user disconnects
socket.on('disconnect', ()=>{
const user = userLeavesChat(socket.id)
if(user){
io.to(user.room)
.emit('message',
formatMessage(botname,`${user.username} has left the chat`));
}
io.to(user.room).emit('roomUsers',{
room:user.room,
users: getRoomUsers(user.room)
})
})
})
//listen for chatMessage
socket.on('chatMessage', (message)=>{
const user = getCurrentUser(socket.id)
io.to(user.room).emit('message', formatMessage(user.username, message))
})
})
server.listen(port, () => {
console.log("Server is running on port " + port)
})