-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
72 lines (55 loc) · 1.88 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
69
70
71
72
const path =require('path');
const http = require('http');
const express = require('express');
const socketio = require('socket.io');
const session = require('express-session');
const formatMessage = require('./utils/message.js')
const { userJoin, getCurrentUser, userLeave, getRoomUsers } = require('./utils/users.js')
const app = express();
const server = http.createServer(app);
const io =socketio(server);
//Set static
app.use(express.static(path.join(__dirname,'public')));
//Run when client connect
io.on('connection',socket=>
{
const botName='ChatBot';
//Runs when client disconnects
socket.on('disconnect',()=>
{
//const user=getCurrentUser(socket.id)
const user=userLeave(socket.id)
if (user){
io.to(user.room).emit('message',formatMessage(botName,`User ${user.username} has left ${user.room}`));
io.to(user.room).emit('roomUsers',{
room:user.room,
users:getRoomUsers(user.room)
})
}
//socket.broadcast.to(user.room).emit('message',formatMessage(botName,`User has ${user.username} left ${user.room}`));
})
socket.on('joinRoom',({username,room})=>
{
const user=userJoin(socket.id, username, room);
socket.emit("message",formatMessage(botName,`Welcome ${user.username} to ${user.room}!`));
socket.join(user.room)
//Broadcast when a user connects
socket.broadcast.to(user.room).emit(
'message',
formatMessage(botName,`A user ${user.username} has joined the ${user.room}`)
)
io.to(user.room).emit('roomUsers',{
room:user.room,
users:getRoomUsers(user.room)
})
})
//Chat message
socket.on('chatMessage', msg=>
{
const user=getCurrentUser(socket.id)
io.to(user.room).emit('message',formatMessage(user.username,msg));
console.log(msg);
})
})
const PORT = 3000 || process.env.PORT;
server.listen(PORT,()=> console.log(`Server running on port ${PORT}`));