- 聊天 WebSocket 提到 app 级,语音分片与对话共用连接,补齐心跳、重连与连接单飞 - 语音输入走 asr_start/asr_chunk/asr_stop,中间结果实时回填,链路异常退回整段转写 - 图片选完先上屏再 OCR,识别原文以 format 消息并入对话 - 失败状态挂在原提问上可点击重试,401 静默重登后原样重发 - 消息区改为暖白纸面与头像+时间布局,常见问题与清空收进更多菜单
205 lines
5.3 KiB
JavaScript
205 lines
5.3 KiB
JavaScript
// AI 聊天 / 语音识别 WebSocket(与 health 服务同域)
|
|
const WS_BASE_URL = 'wss://api.huashengtec.com/health-ws'
|
|
//const WS_BASE_URL = 'ws://127.0.0.1:9005'
|
|
|
|
const WS_HEARTBEAT_INTERVAL = 30000
|
|
const WS_MAX_RECONNECT_ATTEMPTS = 3
|
|
const WS_RECONNECT_DELAY = 3000
|
|
|
|
class AIChatSocket {
|
|
constructor(url = `${WS_BASE_URL}/chat`, options = {}) {
|
|
this.url = url
|
|
this.options = {
|
|
heartbeatInterval: WS_HEARTBEAT_INTERVAL,
|
|
autoReconnect: true,
|
|
maxReconnectAttempts: WS_MAX_RECONNECT_ATTEMPTS,
|
|
reconnectDelay: WS_RECONNECT_DELAY,
|
|
...options
|
|
}
|
|
|
|
this.socketTask = null
|
|
this.isConnected = false
|
|
this.isConnecting = false
|
|
this.connectPromise = null
|
|
this.reconnectAttempts = 0
|
|
|
|
this._openCallback = null
|
|
this._closeCallback = null
|
|
this._errorCallback = null
|
|
this._messageCallback = null
|
|
this._heartbeatTimer = null
|
|
}
|
|
|
|
// 连接 Promise 单飞:调用方必须 await 成功后再发送,避免断线后的首条消息竞态
|
|
connect() {
|
|
if (this.isConnected) return Promise.resolve()
|
|
if (this.connectPromise) return this.connectPromise
|
|
|
|
this.isConnecting = true
|
|
this.connectPromise = new Promise((resolve, reject) => {
|
|
let settled = false
|
|
const succeed = () => {
|
|
if (settled) return
|
|
settled = true
|
|
this.isConnecting = false
|
|
this.connectPromise = null
|
|
resolve()
|
|
}
|
|
const fail = (err) => {
|
|
if (settled) return
|
|
settled = true
|
|
this.isConnecting = false
|
|
this.connectPromise = null
|
|
reject(err instanceof Error ? err : new Error('WebSocket 连接失败'))
|
|
}
|
|
|
|
try {
|
|
this.socketTask = wx.connectSocket({
|
|
url: this.url,
|
|
header: {
|
|
'content-type': 'application/json',
|
|
...this.options.header
|
|
},
|
|
protocols: this.options.protocols || [],
|
|
fail
|
|
})
|
|
} catch (err) {
|
|
fail(err)
|
|
return
|
|
}
|
|
|
|
if (!this.socketTask) {
|
|
fail(new Error('SocketTask creation failed'))
|
|
return
|
|
}
|
|
|
|
this.socketTask.onOpen(() => {
|
|
this.isConnected = true
|
|
this.reconnectAttempts = 0
|
|
this._startHeartbeat()
|
|
if (this._openCallback) this._openCallback()
|
|
succeed()
|
|
})
|
|
|
|
this.socketTask.onClose((res) => {
|
|
this.isConnected = false
|
|
this._stopHeartbeat()
|
|
fail(new Error(`WebSocket 已关闭:${res.code || 'unknown'}`))
|
|
if (this._closeCallback) this._closeCallback(res)
|
|
if (this.options.autoReconnect && this.reconnectAttempts < this.options.maxReconnectAttempts) {
|
|
this._reconnect()
|
|
}
|
|
})
|
|
|
|
this.socketTask.onError((err) => {
|
|
if (this._errorCallback) this._errorCallback(err)
|
|
fail(new Error(err?.errMsg || 'WebSocket 连接错误'))
|
|
})
|
|
|
|
this.socketTask.onMessage((res) => {
|
|
if (!this._messageCallback) return
|
|
try {
|
|
const data = typeof res.data === 'string' ? JSON.parse(res.data) : res.data
|
|
this._messageCallback(data)
|
|
} catch (err) {
|
|
this._messageCallback({
|
|
source: 'system',
|
|
type: 'error',
|
|
content: '收到无法解析的服务消息',
|
|
id: ''
|
|
})
|
|
}
|
|
})
|
|
})
|
|
|
|
return this.connectPromise
|
|
}
|
|
|
|
send(data) {
|
|
const message = typeof data === 'string' ? data : JSON.stringify(data)
|
|
if (!this.isConnected || !this.socketTask) {
|
|
return Promise.reject(new Error('WebSocket 尚未连接'))
|
|
}
|
|
|
|
return new Promise((resolve, reject) => {
|
|
try {
|
|
this.socketTask.send({
|
|
data: message,
|
|
success: resolve,
|
|
fail: (err) => reject(new Error(err?.errMsg || '消息发送失败'))
|
|
})
|
|
} catch (err) {
|
|
reject(err)
|
|
}
|
|
})
|
|
}
|
|
|
|
close() {
|
|
this._stopHeartbeat()
|
|
this.options.autoReconnect = false
|
|
const task = this.socketTask
|
|
this.socketTask = null
|
|
this.isConnected = false
|
|
this.isConnecting = false
|
|
this.connectPromise = null
|
|
if (task) {
|
|
task.close({
|
|
code: 1000,
|
|
reason: 'User closed',
|
|
fail: (err) => console.error('[AIChatSocket] Close failed:', err)
|
|
})
|
|
}
|
|
}
|
|
|
|
onOpen(callback) {
|
|
this._openCallback = callback
|
|
}
|
|
|
|
onClose(callback) {
|
|
this._closeCallback = callback
|
|
}
|
|
|
|
onError(callback) {
|
|
this._errorCallback = callback
|
|
}
|
|
|
|
onMessage(callback) {
|
|
this._messageCallback = callback
|
|
}
|
|
|
|
_reconnect() {
|
|
this.reconnectAttempts++
|
|
setTimeout(() => {
|
|
if (!this.options.autoReconnect || this.isConnected) return
|
|
this.connect().catch((err) => {
|
|
console.error('[AIChatSocket] Reconnect failed:', err)
|
|
})
|
|
}, this.options.reconnectDelay)
|
|
}
|
|
|
|
_startHeartbeat() {
|
|
this._stopHeartbeat()
|
|
this._heartbeatTimer = setInterval(() => {
|
|
if (!this.isConnected || !this.socketTask) return
|
|
this.send({
|
|
source: 'client',
|
|
type: 'ping',
|
|
content: '',
|
|
id: `heartbeat_${Date.now()}`
|
|
}).catch((err) => {
|
|
console.error('[AIChatSocket] Heartbeat failed:', err)
|
|
this._stopHeartbeat()
|
|
})
|
|
}, this.options.heartbeatInterval)
|
|
}
|
|
|
|
_stopHeartbeat() {
|
|
if (this._heartbeatTimer) {
|
|
clearInterval(this._heartbeatTimer)
|
|
this._heartbeatTimer = null
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = AIChatSocket
|