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
|
websocketRef.current = socket
|
||||||
|
|
||||||
socket.onopen = () => {
|
socket.onopen = () => {
|
||||||
|
console.info("[agent-realtime] websocket connected", {
|
||||||
|
url: socket.url,
|
||||||
|
})
|
||||||
reconnectAttemptRef.current = 0
|
reconnectAttemptRef.current = 0
|
||||||
if (pingTimerRef.current) {
|
if (pingTimerRef.current) {
|
||||||
window.clearInterval(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) {
|
if (pingTimerRef.current) {
|
||||||
window.clearInterval(pingTimerRef.current)
|
window.clearInterval(pingTimerRef.current)
|
||||||
pingTimerRef.current = null
|
pingTimerRef.current = null
|
||||||
@@ -193,6 +203,10 @@ export function useAgentConversationRealtime() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
socket.onerror = () => {
|
socket.onerror = () => {
|
||||||
|
console.log("[agent-realtime] websocket error", {
|
||||||
|
url: socket.url,
|
||||||
|
readyState: socket.readyState,
|
||||||
|
})
|
||||||
scheduleReconnect()
|
scheduleReconnect()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import { readSession } from "@/lib/auth"
|
import { readSession } from "@/lib/auth"
|
||||||
import { request } from "@/lib/api/client"
|
import { request } from "@/lib/api/client"
|
||||||
|
import { createWebSocketBaseUrl } from "@/lib/api/websocket"
|
||||||
|
|
||||||
export type Paging = {
|
export type Paging = {
|
||||||
page: number
|
page: number
|
||||||
@@ -542,9 +543,7 @@ export function createAdminWebSocketUrl() {
|
|||||||
throw new Error("未登录或登录已过期")
|
throw new Error("未登录或登录已过期")
|
||||||
}
|
}
|
||||||
|
|
||||||
const baseUrl = (
|
const baseUrl = createWebSocketBaseUrl()
|
||||||
process.env.NEXT_PUBLIC_API_BASE_URL?.trim() || ""
|
|
||||||
).replace(/^http/, "ws")
|
|
||||||
const params = new URLSearchParams({
|
const params = new URLSearchParams({
|
||||||
accessToken: session.accessToken,
|
accessToken: session.accessToken,
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import { request } from "@/lib/api/client"
|
import { request } from "@/lib/api/client"
|
||||||
import { readSession } from "@/lib/auth"
|
import { readSession } from "@/lib/auth"
|
||||||
|
import { createWebSocketBaseUrl } from "@/lib/api/websocket"
|
||||||
|
|
||||||
export type Paging = {
|
export type Paging = {
|
||||||
page: number
|
page: number
|
||||||
@@ -258,9 +259,7 @@ export function createAgentWebSocketUrl() {
|
|||||||
throw new Error("未登录或登录已过期")
|
throw new Error("未登录或登录已过期")
|
||||||
}
|
}
|
||||||
|
|
||||||
const baseUrl = (
|
const baseUrl = createWebSocketBaseUrl()
|
||||||
process.env.NEXT_PUBLIC_API_BASE_URL?.trim() || ""
|
|
||||||
).replace(/^http/, "ws")
|
|
||||||
const params = new URLSearchParams({
|
const params = new URLSearchParams({
|
||||||
accessToken: session.accessToken,
|
accessToken: session.accessToken,
|
||||||
})
|
})
|
||||||
|
|||||||
+2
-1
@@ -1,5 +1,6 @@
|
|||||||
import { request } from "@/lib/api/client"
|
import { request } from "@/lib/api/client"
|
||||||
import { generateUUID } from "@/lib/utils"
|
import { generateUUID } from "@/lib/utils"
|
||||||
|
import { createWebSocketBaseUrl } from "@/lib/api/websocket"
|
||||||
|
|
||||||
export type Paging = {
|
export type Paging = {
|
||||||
page: number
|
page: number
|
||||||
@@ -124,7 +125,7 @@ export function getImVisitorId() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function createImWebSocketUrl() {
|
export function createImWebSocketUrl() {
|
||||||
const baseUrl = API_BASE_URL.replace(/^http/, "ws")
|
const baseUrl = createWebSocketBaseUrl()
|
||||||
const params = new URLSearchParams({
|
const params = new URLSearchParams({
|
||||||
externalId: getImVisitorId(),
|
externalId: getImVisitorId(),
|
||||||
externalSource: OPEN_IM_EXTERNAL_SOURCE,
|
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