75 lines
1.8 KiB
JavaScript
75 lines
1.8 KiB
JavaScript
// APP连接状态管理器
|
|
class AppConnectionManager {
|
|
constructor() {
|
|
this.connected = false
|
|
this.connecting = false
|
|
this.headerInfo = null
|
|
this.connectionPromise = null
|
|
this.lastConnectTime = null
|
|
this.connectionTimeout = 5 * 60 * 1000 // 5分钟超时重连
|
|
}
|
|
|
|
// 检查是否需要重新连接
|
|
needsReconnect() {
|
|
if (!this.connected) return true
|
|
if (!this.lastConnectTime) return true
|
|
|
|
const now = Date.now()
|
|
return (now - this.lastConnectTime) > this.connectionTimeout
|
|
}
|
|
|
|
// 获取连接状态
|
|
isConnected() {
|
|
return this.connected && !this.needsReconnect()
|
|
}
|
|
|
|
// 获取头部信息
|
|
getHeaderInfo() {
|
|
return this.headerInfo
|
|
}
|
|
|
|
// 设置连接状态
|
|
setConnected(headerInfo = null) {
|
|
this.connected = true
|
|
this.connecting = false
|
|
this.headerInfo = headerInfo
|
|
this.lastConnectTime = Date.now()
|
|
this.connectionPromise = null
|
|
console.debug('✅ APP连接状态已更新')
|
|
}
|
|
|
|
// 设置连接中状态
|
|
setConnecting(promise) {
|
|
this.connecting = true
|
|
this.connectionPromise = promise
|
|
}
|
|
|
|
// 重置连接状态
|
|
reset() {
|
|
this.connected = false
|
|
this.connecting = false
|
|
this.headerInfo = null
|
|
this.connectionPromise = null
|
|
this.lastConnectTime = null
|
|
console.debug('🔄 APP连接状态已重置')
|
|
}
|
|
|
|
// 获取当前连接Promise
|
|
getConnectionPromise() {
|
|
return this.connectionPromise
|
|
}
|
|
|
|
// 是否正在连接中
|
|
isConnecting() {
|
|
return this.connecting
|
|
}
|
|
}
|
|
|
|
// 创建全局实例
|
|
export const appConnectionManager = new AppConnectionManager()
|
|
|
|
// 导出便捷方法
|
|
export const isAppConnected = () => appConnectionManager.isConnected()
|
|
export const getAppHeaderInfo = () => appConnectionManager.getHeaderInfo()
|
|
export const resetAppConnection = () => appConnectionManager.reset()
|