-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocketio.js
77 lines (65 loc) · 2.22 KB
/
socketio.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
73
74
75
76
77
var usernames = {};
var rooms = [];
var roomAd = {};
module.exports.sock = (server) => {
const socket = require('socket.io');
const options = {
maxHttpBufferSize: 5e7, // max http buffer size is 50mb
}; // instance of socket io is created
const io = socket(server, options);
io.sockets.on('connection', (socket) => {
function updateuser(roomId) {
let x = roomAd[roomId];
let arr = [];
if (x !== undefined) {
x.forEach((ele) => {
arr.push(usernames[ele]);
});
io.emit('update', arr, roomId);
}
}
// EVENT HANDLERS
socket.on('change-user', (username) => {
function getKeyByValue(object, value) {
return Object.keys(object).find((key) => object[key] === value);
}
let socketid = getKeyByValue(usernames, username);
io.sockets.in(socketid).emit('personal-ide', username);
});
socket.on('getidedata', function (text, user) {
io.emit('adminsideide', text, user);
});
socket.on('delete-room', (roomId) => {
rooms = rooms.filter((ele) => ele === roomId);
io.emit('delete-roomadmin', roomId);
});
socket.on('createroom', (roomId, username) => {
socket.username = username;
socket.room = roomId;
usernames[socket.id] = username;
socket.join(roomId);
if (!rooms.find((ele) => ele === roomId)) {
rooms.push(roomId);
roomAd[roomId] = [socket.id];
} else {
roomAd[roomId].push(socket.id);
}
updateuser(roomId);
socket.on('page-visibility-change', (visibility) => {
const username = usernames[socket.id];
const roomId = socket.room;
let action;
if (visibility === 'hidden') {
action = 'switched the Window/Tab';
} else {
action = 'is back on Page';
}
// Log the visibility change or notify admin
console.log(`User ${username} in room ${roomId} ${action}`);
io.emit('visibility-change', { username, roomId, action });
// You can also send a notification to the admin using socket.emit
// socket.emit('visibility-change-notification', { username, roomId, visibility, action });
});
});
});
};