mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-03-03 23:00:18 +01:00
✨ feat: improve WebRTC connection state handling and enhance WebSocket connection logic
This commit is contained in:
parent
2eda62cf67
commit
bf753bb5dd
2 changed files with 17 additions and 8 deletions
|
|
@ -69,9 +69,8 @@ class WebRTCConnection {
|
||||||
|
|
||||||
const state = this.peerConnection.connectionState;
|
const state = this.peerConnection.connectionState;
|
||||||
logger.info(`Connection state changed to ${state}`);
|
logger.info(`Connection state changed to ${state}`);
|
||||||
this.state = state;
|
|
||||||
|
|
||||||
if (state === 'failed' || state === 'closed') {
|
if (state === 'failed' || state === 'closed' || state === 'disconnected') {
|
||||||
this.cleanup();
|
this.cleanup();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
@ -148,8 +147,8 @@ class AudioHandler {
|
||||||
rtcConnection.addIceCandidate(candidate);
|
rtcConnection.addIceCandidate(candidate);
|
||||||
});
|
});
|
||||||
|
|
||||||
socket.on('vad-status', (status) => {
|
socket.on('vad-status', (speaking) => {
|
||||||
logger.debug(`VAD status from ${socket.id}: ${JSON.stringify(status)}`);
|
logger.debug(`VAD status from ${socket.id}: ${JSON.stringify(speaking)}`);
|
||||||
});
|
});
|
||||||
|
|
||||||
socket.on('disconnect', () => {
|
socket.on('disconnect', () => {
|
||||||
|
|
|
||||||
|
|
@ -18,11 +18,14 @@ class WebSocketManager extends EventEmitter {
|
||||||
private reconnectAttempts = 0;
|
private reconnectAttempts = 0;
|
||||||
private readonly MAX_RECONNECT_ATTEMPTS = 5;
|
private readonly MAX_RECONNECT_ATTEMPTS = 5;
|
||||||
private isConnected = false;
|
private isConnected = false;
|
||||||
|
private isConnecting = false;
|
||||||
|
|
||||||
connect(url: string) {
|
connect(url: string) {
|
||||||
if (this.socket && this.socket.connected) {
|
if (this.isConnecting || (this.socket && this.socket.connected)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.isConnecting = true;
|
||||||
this.socket = io(url, {
|
this.socket = io(url, {
|
||||||
transports: ['websocket'],
|
transports: ['websocket'],
|
||||||
reconnectionAttempts: this.MAX_RECONNECT_ATTEMPTS,
|
reconnectionAttempts: this.MAX_RECONNECT_ATTEMPTS,
|
||||||
|
|
@ -33,11 +36,13 @@ class WebSocketManager extends EventEmitter {
|
||||||
|
|
||||||
private setupEventHandlers() {
|
private setupEventHandlers() {
|
||||||
if (!this.socket) {
|
if (!this.socket) {
|
||||||
|
this.isConnecting = false;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.socket.on('connect', () => {
|
this.socket.on('connect', () => {
|
||||||
this.isConnected = true;
|
this.isConnected = true;
|
||||||
|
this.isConnecting = false;
|
||||||
this.reconnectAttempts = 0;
|
this.reconnectAttempts = 0;
|
||||||
this.emit('connectionChange', true);
|
this.emit('connectionChange', true);
|
||||||
});
|
});
|
||||||
|
|
@ -51,6 +56,7 @@ class WebSocketManager extends EventEmitter {
|
||||||
this.reconnectAttempts++;
|
this.reconnectAttempts++;
|
||||||
this.emit('connectionChange', false);
|
this.emit('connectionChange', false);
|
||||||
if (this.reconnectAttempts >= this.MAX_RECONNECT_ATTEMPTS) {
|
if (this.reconnectAttempts >= this.MAX_RECONNECT_ATTEMPTS) {
|
||||||
|
this.isConnecting = false;
|
||||||
this.emit('error', 'Failed to connect after maximum attempts');
|
this.emit('error', 'Failed to connect after maximum attempts');
|
||||||
this.disconnect();
|
this.disconnect();
|
||||||
}
|
}
|
||||||
|
|
@ -80,6 +86,7 @@ class WebSocketManager extends EventEmitter {
|
||||||
this.socket = null;
|
this.socket = null;
|
||||||
}
|
}
|
||||||
this.isConnected = false;
|
this.isConnected = false;
|
||||||
|
this.isConnecting = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
sendMessage(type: string, payload?: MessagePayload) {
|
sendMessage(type: string, payload?: MessagePayload) {
|
||||||
|
|
@ -91,7 +98,7 @@ class WebSocketManager extends EventEmitter {
|
||||||
}
|
}
|
||||||
|
|
||||||
getConnectionState() {
|
getConnectionState() {
|
||||||
return this.isConnected;
|
return { isConnected: this.isConnected, isConnecting: this.isConnecting };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -103,8 +110,11 @@ const useWebSocket = () => {
|
||||||
const eventHandlersRef = useRef<Record<string, EventHandler>>({});
|
const eventHandlersRef = useRef<Record<string, EventHandler>>({});
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (wsConfig?.url && !webSocketManager.getConnectionState()) {
|
if (wsConfig?.url) {
|
||||||
webSocketManager.connect(wsConfig.url);
|
const state = webSocketManager.getConnectionState();
|
||||||
|
if (!state.isConnected && !state.isConnecting) {
|
||||||
|
webSocketManager.connect(wsConfig.url);
|
||||||
|
}
|
||||||
|
|
||||||
const handleConnectionChange = (connected: boolean) => setIsConnected(connected);
|
const handleConnectionChange = (connected: boolean) => setIsConnected(connected);
|
||||||
webSocketManager.on('connectionChange', handleConnectionChange);
|
webSocketManager.on('connectionChange', handleConnectionChange);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue