This commit is contained in:
Халимов Рустам
2026-03-10 21:11:59 +03:00
parent a3c0d35263
commit b4b4b8e0d3
80 changed files with 23310 additions and 0 deletions

View File

@@ -0,0 +1,42 @@
import { io, Socket } from 'socket.io-client';
let socket: Socket | null = null;
export function connectSocket(token: string): Socket {
if (socket?.connected) {
return socket;
}
// Clean up old socket instance if it exists but is disconnected
if (socket) {
socket.removeAllListeners();
socket.disconnect();
socket = null;
}
socket = io(window.location.origin, {
auth: { token },
transports: ['websocket', 'polling'],
});
socket.on('connect', () => {
console.log('Socket подключён');
});
socket.on('connect_error', (err) => {
console.error('Ошибка подключения Socket:', err.message);
});
return socket;
}
export function getSocket(): Socket | null {
return socket;
}
export function disconnectSocket() {
if (socket) {
socket.disconnect();
socket = null;
}
}