From 9e678a08a6dbf9bee87491edcfdaebb931ccab51 Mon Sep 17 00:00:00 2001 From: Steven Enamakel Date: Thu, 5 Feb 2026 20:12:42 +0530 Subject: [PATCH] Enhance socket connection logging and URL handling - Added logging for the backend URL in the `runtime_socket_connect` function to improve traceability. - Updated the `SocketManager` to convert HTTP/HTTPS URLs to WebSocket URLs, enhancing connection handling. - Improved error logging to capture detailed error chains during connection failures, aiding in debugging. --- src-tauri/src/commands/runtime.rs | 1 + src-tauri/src/runtime/socket_manager.rs | 39 ++++++++++++++++++------- src/utils/tauriSocket.ts | 6 +++- 3 files changed, 35 insertions(+), 11 deletions(-) diff --git a/src-tauri/src/commands/runtime.rs b/src-tauri/src/commands/runtime.rs index 43fbdd223..a92190e48 100644 --- a/src-tauri/src/commands/runtime.rs +++ b/src-tauri/src/commands/runtime.rs @@ -386,6 +386,7 @@ pub async fn runtime_socket_connect( url: Option, ) -> Result<(), String> { let backend_url = url.unwrap_or_else(get_backend_url); + log::info!("[socket-cmd] runtime_socket_connect to {}", backend_url); socket_mgr.connect(&backend_url, &token).await } diff --git a/src-tauri/src/runtime/socket_manager.rs b/src-tauri/src/runtime/socket_manager.rs index a8e9ce957..5179412a1 100644 --- a/src-tauri/src/runtime/socket_manager.rs +++ b/src-tauri/src/runtime/socket_manager.rs @@ -127,7 +127,12 @@ impl SocketManager { // Disconnect existing connection first self.disconnect().await?; - log::info!("[socket-mgr] Connecting to {}", url); + // Convert https:// → wss:// and http:// → ws:// for direct WebSocket + let ws_url = url + .replace("https://", "wss://") + .replace("http://", "ws://"); + + log::info!("[socket-mgr] Connecting to {} (ws: {})", url, ws_url); // Update status *self.shared.status.write() = ConnectionStatus::Connecting; @@ -144,12 +149,15 @@ impl SocketManager { let s_tool_call = Arc::clone(&self.shared); let s_any = Arc::clone(&self.shared); - let client = ClientBuilder::new(url) + let client = ClientBuilder::new(&ws_url) .namespace("/") .auth(json!({"token": token})) + // Pass JWT in opening headers so the server can authenticate + // the engine.io handshake before the Socket.io CONNECT packet + .opening_header("Authorization", format!("Bearer {}", token)) .reconnect(true) .max_reconnect_attempts(0) // unlimited - .transport_type(rust_socketio::TransportType::WebsocketUpgrade) + .transport_type(rust_socketio::TransportType::Websocket) // --- Connection established --- .on("connect", move |_payload, _client: Client| { let shared = Arc::clone(&s_connect); @@ -265,8 +273,11 @@ impl SocketManager { .connect() .await .map_err(|e| { - log::error!("[socket-mgr] Connection error: {e}"); - format!("Socket connection failed: {e}") + // Use {:?} to get the full error chain — Display loses inner detail + log::error!("[socket-mgr] Connection error: {e:?}"); + *self.shared.status.write() = ConnectionStatus::Disconnected; + Self::emit_state_change(&self.shared); + format!("Socket connection failed: {e:?}") })?; log::info!("[socket-mgr] ClientBuilder.connect() returned successfully"); @@ -304,7 +315,12 @@ impl SocketManager { // Disconnect existing connection first self.disconnect().await?; - log::info!("[socket-mgr] Connecting to {} (iOS)", url); + // Convert https:// → wss:// and http:// → ws:// for direct WebSocket + let ws_url = url + .replace("https://", "wss://") + .replace("http://", "ws://"); + + log::info!("[socket-mgr] Connecting to {} (ws: {}, iOS)", url, ws_url); // Update status *self.shared.status.write() = ConnectionStatus::Connecting; @@ -318,12 +334,13 @@ impl SocketManager { let s_error = Arc::clone(&self.shared); let s_any = Arc::clone(&self.shared); - let client = ClientBuilder::new(url) + let client = ClientBuilder::new(&ws_url) .namespace("/") .auth(json!({"token": token})) + .opening_header("Authorization", format!("Bearer {}", token)) .reconnect(true) .max_reconnect_attempts(0) // unlimited - .transport_type(rust_socketio::TransportType::WebsocketUpgrade) + .transport_type(rust_socketio::TransportType::Websocket) // --- Connection established --- .on("connect", move |_payload, _client: Client| { let shared = Arc::clone(&s_connect); @@ -413,8 +430,10 @@ impl SocketManager { .connect() .await .map_err(|e| { - log::error!("[socket-mgr] Connection error: {e}"); - format!("Socket connection failed: {e}") + log::error!("[socket-mgr] Connection error: {e:?}"); + *self.shared.status.write() = ConnectionStatus::Disconnected; + Self::emit_state_change(&self.shared); + format!("Socket connection failed: {e:?}") })?; log::info!("[socket-mgr] ClientBuilder.connect() returned successfully"); diff --git a/src/utils/tauriSocket.ts b/src/utils/tauriSocket.ts index 2f082216c..b78e22795 100644 --- a/src/utils/tauriSocket.ts +++ b/src/utils/tauriSocket.ts @@ -37,10 +37,14 @@ export async function connectRustSocket(token: string): Promise { if (!isTauri()) return; try { + console.log('[TauriSocket] Connecting Rust socket to', BACKEND_URL); await invoke('runtime_socket_connect', { token, url: BACKEND_URL }); - console.log('[TauriSocket] Rust socket connecting'); + console.log('[TauriSocket] Rust socket connect call succeeded'); } catch (error) { console.error('[TauriSocket] Failed to connect Rust socket:', error); + // Ensure Redux status reflects the failure + const uid = getSocketUserId(); + store.dispatch(setStatusForUser({ userId: uid, status: 'disconnected' })); } }