From 074715b002ae3a2d905cb8c0429851b265d6fe6d Mon Sep 17 00:00:00 2001 From: Guoyu Su Date: Thu, 2 Jul 2026 19:47:41 +0800 Subject: [PATCH] fix(redis): connect pubsub to desktop websocket server --- .../src/components/redis/RedisPubSubPanel.vue | 2 +- apps/desktop/src/lib/api.ts | 6 +----- apps/desktop/src/lib/http.ts | 6 +++++- apps/desktop/src/lib/tauri.ts | 5 +++++ src-tauri/src/commands/redis_pubsub_server.rs | 13 ++++++++++++- src-tauri/src/lib.rs | 1 + 6 files changed, 25 insertions(+), 8 deletions(-) diff --git a/apps/desktop/src/components/redis/RedisPubSubPanel.vue b/apps/desktop/src/components/redis/RedisPubSubPanel.vue index e22a17d94f..f4cfb1866f 100644 --- a/apps/desktop/src/components/redis/RedisPubSubPanel.vue +++ b/apps/desktop/src/components/redis/RedisPubSubPanel.vue @@ -65,7 +65,7 @@ async function connect() { if (ws && ws.readyState === WebSocket.OPEN) return; connecting.value = true; try { - ws = api.redisPubSubConnect(props.connectionId); + ws = await api.redisPubSubConnect(props.connectionId); ws.onopen = () => { connected.value = true; diff --git a/apps/desktop/src/lib/api.ts b/apps/desktop/src/lib/api.ts index 6cd6a328de..c30d71f527 100644 --- a/apps/desktop/src/lib/api.ts +++ b/apps/desktop/src/lib/api.ts @@ -1,7 +1,6 @@ import { isTauriRuntime } from "./tauriRuntime"; import type * as TauriModule from "./tauri"; import { appendDebugLog } from "./debugLog"; -import { apiWebSocketUrl } from "./webPath"; // --------------------------------------------------------------------------- // Lazy backend resolution (avoids top-level await) @@ -311,13 +310,10 @@ export const redisFlushDb = forward("redisFlushDb"); export const redisExecuteCommand = forward("redisExecuteCommand"); export const redisLoadMore = forward("redisLoadMore"); export const redisPubSubPublish = forward("redisPubSubPublish"); +export const redisPubSubConnect = forward("redisPubSubConnect"); export const redisSlowlogGet = forward("redisSlowlogGet"); export const redisClusterMasterNodes = forward("redisClusterMasterNodes"); -export function redisPubSubConnect(connectionId: string): WebSocket { - return new WebSocket(apiWebSocketUrl(`/api/redis/pubsub/ws?connectionId=${encodeURIComponent(connectionId)}`)); -} - // etcd export const etcdListPrefix = forward("etcdListPrefix"); export const etcdGet = forward("etcdGet"); diff --git a/apps/desktop/src/lib/http.ts b/apps/desktop/src/lib/http.ts index 3053d22725..1fd4199326 100644 --- a/apps/desktop/src/lib/http.ts +++ b/apps/desktop/src/lib/http.ts @@ -111,7 +111,7 @@ import type { CreateDatabaseSqlOptions } from "@/lib/createDatabaseSql"; import type { DatabaseNameSqlOptions, DropTableChildObjectSqlOptions, DropObjectSqlOptions, DuplicateTableStructureSqlOptions, CopyTableDataSqlOptions, SchemaNameSqlOptions, TableAdminSqlOptions } from "@/lib/dbAdminSql"; import type { BuildDatabaseSqlExportOptions, BuildExportInsertStatementsOptions } from "@/lib/databaseExport"; import type { DataCompareFromTablesOptions, DataCompareFromTablesPreparation, DataCompareSyncPlan, DataCompareSyncPlanOptions, DataComparePreparation, DataComparePreparationOptions } from "@/lib/dataCompare"; -import { apiUrl } from "@/lib/webPath"; +import { apiUrl, apiWebSocketUrl } from "@/lib/webPath"; import type { DataGridSavePreparation } from "./tauri"; import type { NacosConfigHistoryKey, @@ -1631,6 +1631,10 @@ export async function redisPubSubPublish(connectionId: string, db: number, chann return post("/api/redis/pubsub/publish", { connectionId, db, channel, message }); } +export async function redisPubSubConnect(connectionId: string): Promise { + return new WebSocket(apiWebSocketUrl(`/api/redis/pubsub/ws?connectionId=${encodeURIComponent(connectionId)}`)); +} + export async function redisSlowlogGet(connectionId: string, count: number, nodeHost?: string, nodePort?: number): Promise { return post("/api/redis/slowlog-get", { connectionId, count, nodeHost, nodePort }); } diff --git a/apps/desktop/src/lib/tauri.ts b/apps/desktop/src/lib/tauri.ts index ba4b491783..c6a7895e25 100644 --- a/apps/desktop/src/lib/tauri.ts +++ b/apps/desktop/src/lib/tauri.ts @@ -1364,6 +1364,11 @@ export async function redisPubSubPublish(connectionId: string, db: number, chann return invoke("redis_pubsub_publish", { connectionId, db, channel, message }); } +export async function redisPubSubConnect(connectionId: string): Promise { + const port = await invoke("redis_pubsub_server_port"); + return new WebSocket(`ws://127.0.0.1:${port}/api/redis/pubsub/ws?connectionId=${encodeURIComponent(connectionId)}`); +} + export async function redisSlowlogGet(connectionId: string, count: number, nodeHost?: string, nodePort?: number): Promise { return invoke("redis_slowlog_get", { connectionId, count, nodeHost, nodePort }); } diff --git a/src-tauri/src/commands/redis_pubsub_server.rs b/src-tauri/src/commands/redis_pubsub_server.rs index 298bd8e0e3..dff120814b 100644 --- a/src-tauri/src/commands/redis_pubsub_server.rs +++ b/src-tauri/src/commands/redis_pubsub_server.rs @@ -10,6 +10,8 @@ use serde::Deserialize; use dbx_core::connection::AppState; +const DEFAULT_PUBSUB_PORT: u16 = 4224; + #[derive(Deserialize)] #[serde(rename_all = "camelCase")] struct PubSubWsParams { @@ -20,6 +22,15 @@ pub fn build_pubsub_router(state: Arc) -> Router { Router::new().route("/api/redis/pubsub/ws", get(ws_handler)).with_state(state) } +fn pubsub_server_port() -> u16 { + std::env::var("DBX_PORT").ok().and_then(|p| p.parse().ok()).unwrap_or(DEFAULT_PUBSUB_PORT) +} + +#[tauri::command] +pub fn redis_pubsub_server_port() -> u16 { + pubsub_server_port() +} + async fn ws_handler( ws: WebSocketUpgrade, Query(params): Query, @@ -136,7 +147,7 @@ async fn handle_command(sink: &mut redis::aio::PubSubSink, text: &str) -> Result pub fn start_pubsub_server(state: Arc) { let router = build_pubsub_router(state); tauri::async_runtime::spawn(async move { - let port: u16 = std::env::var("DBX_PORT").ok().and_then(|p| p.parse().ok()).unwrap_or(4224); + let port = pubsub_server_port(); let addr = std::net::SocketAddr::from(([0, 0, 0, 0], port)); let listener = match tokio::net::TcpListener::bind(addr).await { Ok(listener) => listener, diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index e2ea625f81..ee9e3337e6 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -896,6 +896,7 @@ pub fn run() { commands::redis_cmd::redis_execute_command, commands::redis_cmd::redis_load_more, commands::redis_cmd::redis_pubsub_publish, + commands::redis_pubsub_server::redis_pubsub_server_port, commands::redis_cmd::redis_slowlog_get, commands::redis_cmd::redis_cluster_master_nodes, commands::etcd_cmd::etcd_list_prefix,