diff --git a/web/hooks/use-agent-conversation-realtime.ts b/web/hooks/use-agent-conversation-realtime.ts index ae03904..98efe5e 100644 --- a/web/hooks/use-agent-conversation-realtime.ts +++ b/web/hooks/use-agent-conversation-realtime.ts @@ -77,6 +77,9 @@ export function useAgentConversationRealtime() { websocketRef.current = socket socket.onopen = () => { + console.info("[agent-realtime] websocket connected", { + url: socket.url, + }) reconnectAttemptRef.current = 0 if (pingTimerRef.current) { window.clearInterval(pingTimerRef.current) @@ -180,7 +183,14 @@ export function useAgentConversationRealtime() { } } - socket.onclose = () => { + socket.onclose = (event) => { + console.log("[agent-realtime] websocket closed", { + url: socket.url, + readyState: socket.readyState, + code: event.code, + reason: event.reason, + wasClean: event.wasClean, + }) if (pingTimerRef.current) { window.clearInterval(pingTimerRef.current) pingTimerRef.current = null @@ -193,6 +203,10 @@ export function useAgentConversationRealtime() { } socket.onerror = () => { + console.log("[agent-realtime] websocket error", { + url: socket.url, + readyState: socket.readyState, + }) scheduleReconnect() } } diff --git a/web/lib/api/admin.ts b/web/lib/api/admin.ts index f963c2c..0061558 100644 --- a/web/lib/api/admin.ts +++ b/web/lib/api/admin.ts @@ -1,5 +1,6 @@ import { readSession } from "@/lib/auth" import { request } from "@/lib/api/client" +import { createWebSocketBaseUrl } from "@/lib/api/websocket" export type Paging = { page: number @@ -542,9 +543,7 @@ export function createAdminWebSocketUrl() { throw new Error("未登录或登录已过期") } - const baseUrl = ( - process.env.NEXT_PUBLIC_API_BASE_URL?.trim() || "" - ).replace(/^http/, "ws") + const baseUrl = createWebSocketBaseUrl() const params = new URLSearchParams({ accessToken: session.accessToken, }) diff --git a/web/lib/api/agent.ts b/web/lib/api/agent.ts index 659e8c1..550f0de 100644 --- a/web/lib/api/agent.ts +++ b/web/lib/api/agent.ts @@ -1,5 +1,6 @@ import { request } from "@/lib/api/client" import { readSession } from "@/lib/auth" +import { createWebSocketBaseUrl } from "@/lib/api/websocket" export type Paging = { page: number @@ -258,9 +259,7 @@ export function createAgentWebSocketUrl() { throw new Error("未登录或登录已过期") } - const baseUrl = ( - process.env.NEXT_PUBLIC_API_BASE_URL?.trim() || "" - ).replace(/^http/, "ws") + const baseUrl = createWebSocketBaseUrl() const params = new URLSearchParams({ accessToken: session.accessToken, }) diff --git a/web/lib/api/im.ts b/web/lib/api/im.ts index b1cacb1..bdb843c 100644 --- a/web/lib/api/im.ts +++ b/web/lib/api/im.ts @@ -1,5 +1,6 @@ import { request } from "@/lib/api/client" import { generateUUID } from "@/lib/utils" +import { createWebSocketBaseUrl } from "@/lib/api/websocket" export type Paging = { page: number @@ -124,7 +125,7 @@ export function getImVisitorId() { } export function createImWebSocketUrl() { - const baseUrl = API_BASE_URL.replace(/^http/, "ws") + const baseUrl = createWebSocketBaseUrl() const params = new URLSearchParams({ externalId: getImVisitorId(), externalSource: OPEN_IM_EXTERNAL_SOURCE, diff --git a/web/lib/api/websocket.ts b/web/lib/api/websocket.ts new file mode 100644 index 0000000..659a4a4 --- /dev/null +++ b/web/lib/api/websocket.ts @@ -0,0 +1,6 @@ +const API_BASE_URL = + process.env.NEXT_PUBLIC_API_BASE_URL?.trim() || "http://127.0.0.1:8083" + +export function createWebSocketBaseUrl() { + return API_BASE_URL.replace(/^http/, "ws").replace(/\/$/, "") +}