-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
64 lines (45 loc) · 1.9 KB
/
server.js
File metadata and controls
64 lines (45 loc) · 1.9 KB
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
const express=require("express");
const path=require("path");
const http=require("http");
const socketio=require("socket.io");
const formatmessage=require("./utils/messages");
const {userJoin, getCurrentUser,userLeave,getRoomUsers}=require("./utils/users");
const app=express();
const server=http.createServer(app);
const io=socketio(server);
const botname='ChatBot';
//Set Static Folder
app.use(express.static(path.join(__dirname,'public')));
//Run when client connects
io.on('connection',socket=>{
socket.on('joinRoom', ({username, room})=>{
const user=userJoin(socket.id,username,room );
socket.join(user.room);
//When user joins room
socket.emit('message',formatmessage(botname,`Hello ${user.username}! Welcome to ChatBot. Your Room is ${room}`));
//When a user enters the chat
socket.broadcast.to(user.room).emit('message',formatmessage(botname,`${user.username} has joined the chat`));
//Listen for chat message
socket.on('chatmessage',(msg)=>{
io.to(user.room).emit('message',formatmessage(`${user.username}`,msg));
});
//When a user disconnects
socket.on('disconnect',()=>{
const user=userLeave(socket.id);
if(user){
io.to(user.room).emit('message',formatmessage(botname,`${user.username} has left the chat`))
};
});
//Send users room and info
io.to(user.room).emit('roomusers',{
room:user.room,
users:getRoomUsers(user.room)
});
});
console.log('New WS Connection...');
});
const PORT=3000 || process.env.PORT;
server.listen(PORT, ()=>console.log(`Server is running on port ${PORT}`));
//socket.broadcast.emit()=>to show message to everyone except the connecting client
//socket.emit()=>to show message only to the connecting client
//io.emit=>To show message to all clients in general