-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
36 lines (23 loc) · 839 Bytes
/
app.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
const express = require('express');
const socket = require('socket.io');
const app = express();
server = app.listen(5000, function(){
console.log('server is running on port 5000');
});
io = socket(server);
let activeUsers = [];
io.on('connection', (socket) => {
const { username } = socket.request._query;
activeUsers = [ ...activeUsers, username ];
console.log('USER CONNECTED ::: ', socket.id, username);
io.emit('USER_CONNECTED', username);
socket.on('disconnect', () => {
console.log('USER DISCONNECTED ::: ', socket.id);
activeUsers = activeUsers.filter(item => item != username);
io.emit('USER_DISCONNECTED', username);
});
socket.emit('GET_ACTIVE_USERS', activeUsers);
socket.on('SEND_MESSAGE', function(data){
io.emit('RECEIVE_MESSAGE', data);
});
});