actually fixing the bug the last commit was supposed to fix

This commit is contained in:
2025-12-27 10:30:15 +08:00
parent b168d674bd
commit 40d9872876
2 changed files with 22 additions and 7 deletions

View File

@@ -22,7 +22,7 @@ async fn init_ws_after_auth() {
const MAX_ATTEMPTS: u8 = 5; const MAX_ATTEMPTS: u8 = 5;
const BACKOFF: Duration = Duration::from_millis(300); const BACKOFF: Duration = Duration::from_millis(300);
for attempt in 1..=MAX_ATTEMPTS { for _attempt in 1..=MAX_ATTEMPTS {
if get_access_token().await.is_some() { if get_access_token().await.is_some() {
init_ws_client().await; init_ws_client().await;
return; return;

View File

@@ -47,13 +47,22 @@ impl WS_EVENT {
pub const INITIALIZED: &str = "initialized"; pub const INITIALIZED: &str = "initialized";
} }
fn on_connected(_payload: Payload, socket: RawClient) { fn emit_initialize(socket: &RawClient) {
info!("WebSocket connected. Sending initialization request.");
if let Err(e) = socket.emit(WS_EVENT::CLIENT_INITIALIZE, json!({})) { if let Err(e) = socket.emit(WS_EVENT::CLIENT_INITIALIZE, json!({})) {
error!("Failed to emit client-initialize: {:?}", e); error!("Failed to emit client-initialize: {:?}", e);
} }
} }
fn on_connected(_payload: Payload, socket: RawClient) {
info!("WebSocket connected. Sending initialization request.");
emit_initialize(&socket);
}
fn on_reconnect(_payload: Payload, socket: RawClient) {
info!("WebSocket reconnected. Re-sending initialization request.");
emit_initialize(&socket);
}
fn on_initialized(payload: Payload, _socket: RawClient) { fn on_initialized(payload: Payload, _socket: RawClient) {
match payload { match payload {
Payload::Text(values) => { Payload::Text(values) => {
@@ -398,7 +407,12 @@ pub async fn init_ws_client() {
} }
Err(e) => { Err(e) => {
error!("Failed to initialize WebSocket client: {}", e); error!("Failed to initialize WebSocket client: {}", e);
// We should probably inform the user or retry here, but for now we just log it. // If we failed because no token, clear the WS client to avoid stale retries
let mut guard = lock_w!(FDOLL);
if let Some(clients) = guard.clients.as_mut() {
clients.ws_client = None;
clients.is_ws_initialized = false;
}
} }
} }
} }
@@ -406,9 +420,8 @@ pub async fn init_ws_client() {
pub async fn build_ws_client( pub async fn build_ws_client(
app_config: &AppConfig, app_config: &AppConfig,
) -> Result<rust_socketio::client::Client, String> { ) -> Result<rust_socketio::client::Client, String> {
let token_result = crate::services::auth::get_access_token().await; // Always fetch a fresh/valid token (refreshing if needed)
let token = match crate::services::auth::get_access_token().await {
let token = match token_result {
Some(t) => t, Some(t) => t,
None => return Err("No access token available for WebSocket connection".to_string()), None => return Err("No access token available for WebSocket connection".to_string()),
}; };
@@ -444,6 +457,8 @@ pub async fn build_ws_client(
.on(WS_EVENT::DOLL_UPDATED, on_doll_updated) .on(WS_EVENT::DOLL_UPDATED, on_doll_updated)
.on(WS_EVENT::DOLL_DELETED, on_doll_deleted) .on(WS_EVENT::DOLL_DELETED, on_doll_deleted)
.on(WS_EVENT::INITIALIZED, on_initialized) .on(WS_EVENT::INITIALIZED, on_initialized)
// rust-socketio fires Event::Connect on initial connect AND reconnects
// so we resend initialization there instead of a dedicated reconnect event.
.on(Event::Connect, on_connected) .on(Event::Connect, on_connected)
.auth(json!({ "token": token })) .auth(json!({ "token": token }))
.connect() .connect()