电话咨询

This commit is contained in:
duoaohui
2026-06-04 14:32:52 +08:00
parent edda31e8bf
commit b89b930c41
4 changed files with 134 additions and 20 deletions

View File

@@ -184,6 +184,65 @@ class WebSocketService {
})
}
/**
* 当前连接是否处于可发送状态H5 下要求 readyState === OPEN
* 与 send() 的就绪判断保持一致
* @returns {boolean}
*/
isConnected() {
// #ifdef H5
return !!(this.ws && this.ws.readyState === WebSocket.OPEN)
// #endif
// #ifndef H5
return !!this.ws
// #endif
}
/**
* 等待 WebSocket 进入可用OPEN状态
*
* 场景SSO / web-view 登录后 connect() 为异步建链CONNECTING→OPEN
* 设备预览结束立即入会时,首个 sendWithAck 可能早于 socket OPEN
* 导致 send() 返回 -1 → 业务侧抛 "连接未就绪"。
* 本方法在未连接时主动发起一次连接connect 对 OPEN/CONNECTING 幂等),
* 并等待 _connected 事件;已连接则立即 resolve超时 reject。
*
* @param {number} [timeoutMS=10000] - 最长等待时间
* @returns {Promise<boolean>}
*/
waitForConnected(timeoutMS = 10000) {
if (this.isConnected()) return Promise.resolve(true)
// 未建链 / 已断开:主动发起一次连接(幂等,不会重复建链)
this.connect()
return new Promise((resolve, reject) => {
let settled = false
let timer = null
let poll = null
const cleanup = () => {
this.off('_connected', onConnected)
if (timer) clearTimeout(timer)
if (poll) clearInterval(poll)
}
const onConnected = () => {
if (settled) return
settled = true
cleanup()
resolve(true)
}
timer = setTimeout(() => {
if (settled) return
settled = true
cleanup()
reject({ code: -1, message: '连接会议服务器超时' })
}, timeoutMS)
// 轮询兜底:防止 _onOpen 在 listener 注册前已触发而错过事件
poll = setInterval(() => {
if (this.isConnected()) onConnected()
}, 100)
this.on('_connected', onConnected)
})
}
/**
* 注册事件监听
* @param {string} event - 事件类型