-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocket.js
More file actions
58 lines (46 loc) · 1.39 KB
/
socket.js
File metadata and controls
58 lines (46 loc) · 1.39 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
const socketIo = require('socket.io');
let io;
const origin_url = process.env.ORIGIN_URL
exports.socketInit = (server) => {
io = socketIo(server, {
cors: {
origin: origin_url, // This is your client's origin. Adjust if necessary.
methods: ["GET", "POST"],
credentials: true
}
});
io.on('connection', (socket) => {
console.log('A user connected with socket id:', socket.id);
socket.on('disconnect', () => {
console.log('User disconnected with socket id:', socket.id);
});
// Additional debugging events
socket.on('connect_error', (error) => {
console.log('Connection Error:', error);
});
socket.on('connect_timeout', (timeout) => {
console.log('Connection Timeout:', timeout);
});
socket.on('reconnect', (attemptNumber) => {
console.log('Reconnected after:', attemptNumber, 'attempts');
});
socket.on('reconnect_attempt', () => {
console.log('Reconnect Attempted');
});
socket.on('reconnecting', (attemptNumber) => {
console.log('Reconnecting. Attempt:', attemptNumber);
});
socket.on('reconnect_error', (error) => {
console.log('Reconnection Error:', error);
});
socket.on('reconnect_failed', () => {
console.log('Reconnection Failed');
});
});
};
exports.getIo = () => {
if (!io) {
throw new Error('Socket.io not initialized!');
}
return io;
};