-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchatserver.js
31 lines (26 loc) · 950 Bytes
/
chatserver.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
const io=require("socket.io")(7500);
const users={};
io.on('connection',socket=>
{
socket.on('new-user-joined',name=>
{
users[socket.id]=name;
io.emit('online-',Object.keys(io.sockets.connected).length)
socket.broadcast.emit('user-joined',name);
console.log(`${name}--${socket.id} joined server`);
console.log(`${Object.keys(io.sockets.connected).length} clients connected`);
});
socket.on('send',message=>
{
console.log(`${users[socket.id]}'s MESSAGE = ${message}`);
socket.broadcast.emit('receive',{message:message , name:users[socket.id]});
});
socket.on('disconnect',message=>
{
socket.broadcast.emit('left',users[socket.id]);
console.log(`${users[socket.id]}--${socket.id} left server`);
io.emit('online-',Object.keys(io.sockets.connected).length)
delete users[socket.id];
console.log(`${Object.keys(io.sockets.connected).length} clients connected`);
});
});