feat: implement websocket URL creation and enhance websocket connection logging
This commit is contained in:
@@ -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()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
})
|
||||
|
||||
@@ -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,
|
||||
})
|
||||
|
||||
+2
-1
@@ -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,
|
||||
|
||||
@@ -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(/\/$/, "")
|
||||
}
|
||||
Reference in New Issue
Block a user