-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
76 lines (64 loc) · 2.1 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
73
74
75
76
const express = require('express');
const cors = require('cors');
const https = require("https");
const app = express();
// Enable CORS for all origins
app.use(cors());
const server = https.createServer(app);
const socket = require("socket.io");
const io = socket(server, {
cors: {
origin: "https://chatsolid.elivanstichelen.com/", // Allow requests from all origins
transports: ["websocket"],
}
});
// Store the list of users
const users = {};
// Set the timeout for inactive users
const ACTIVE_TIMEOUT = 10000;
io.on('connection', socket => {
// Add the user to the list of users
users[socket.id] = socket.id;
io.sockets.emit("allUsers", users);
socket.on('disconnect', () => {
delete users[socket.id]; // Remove user based on socket ID
io.sockets.emit("allUsers", users);
});
// Emit user's ID and the list of all users
socket.emit("yourID", socket.id);
// Handle call events
socket.on("callUser", (data) => {
io.to(data.userToCall).emit('hey', { signal: data.signalData, from: data.from });
});
socket.on("acceptCall", (data) => {
io.to(data.to).emit('callAccepted', data.signal);
});
// Handle user-initiated disconnection event
socket.on('userDisconnect', (userId) => {
delete users[userId];
io.sockets.emit("allUsers", users);
});
// Handle unexpected disconnection
socket.on('disconnect', () => {
delete users[socket.id];
io.sockets.emit("allUsers", users);
});
// Regularly clean up inactive users
setInterval(() => {
const now = new Date();
for (const id in users) {
if (now - users[id].lastActive > ACTIVE_TIMEOUT) {
delete users[id];
}
}
io.sockets.emit("allUsers", getActiveUsers());
}, ACTIVE_TIMEOUT);
});
// Get the list of active users
function getActiveUsers() {
return Object.keys(users).reduce((acc, id) => {
acc[id] = { lastActive: users[id].lastActive };
return acc;
}, {});
}
server.listen(3000, () => console.log('server is running on port 3000'));