43 lines
862 B
TypeScript
43 lines
862 B
TypeScript
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;
|
|
}
|
|
}
|