Files
SmartParks_uniapp/utils/websocket.js
liyuanchao a6245b29cd
Some checks failed
Uniapp 自动化打包 CI/CD / 打包 Uniapp 项目 (push) Has been cancelled
消息推送
2025-09-14 17:43:16 +08:00

116 lines
2.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

class WebSocketService {
constructor() {
this.socketTask = null
this.isConnected = false
this.heartbeatTimer = null
this.reconnectTimer = null
this.url = ""
this.dispatchHandler = null // 统一分发处理器
}
connect(url) {
if (this.isConnected) return
this.url = url
this.socketTask = uni.connectSocket({
url: url, // 鉴权
success: () => console.log("连接请求已发送")
})
// this.socketTask = uni.connectSocket({ url })
console.log('WebSocketService', 'connect called, url:', url)
// 注意:跨平台统一使用 socketTask 的事件监听
if (this.socketTask) {
this.socketTask.onOpen(() => {
console.log("WebSocket 已连接")
this.isConnected = true
this.startHeartbeat()
})
this.socketTask.onMessage((res) => {
console.log("📩 收到消息:", res.data)
let msg
try {
msg = JSON.parse(res.data)
} catch (e) {
msg = res.data
}
// 统一分发
if (this.dispatchHandler) {
this.dispatchHandler(msg)
} else {
console.warn("未注册 dispatchHandler消息未处理", msg)
}
})
this.socketTask.onClose(() => {
console.log("❌ WebSocket 已关闭")
this.isConnected = false
this.reconnect()
})
this.socketTask.onError((err) => {
console.error("⚠️ WebSocket 错误:", err)
this.isConnected = false
this.reconnect()
})
} else {
console.error("WebSocketService: socketTask 获取失败,可能平台不支持")
}
}
send(data) {
if (this.isConnected && this.socketTask) {
this.socketTask.send({
data: typeof data === "string" ? data : JSON.stringify(data),
})
console.log('WebSocketService', 'send:', data)
}
}
startHeartbeat() {
console.log('WebSocketService', 'startHeartbeat')
this.clearHeartbeat()
this.heartbeatTimer = setInterval(() => {
if (this.isConnected) {
this.send("ping")
console.log('WebSocketService', 'heartbeat ping sent')
}
}, 30000)
}
clearHeartbeat() {
if (this.heartbeatTimer) {
clearInterval(this.heartbeatTimer)
this.heartbeatTimer = null
}
}
reconnect() {
if (this.reconnectTimer) return
console.log("⏳ 5秒后尝试重连...")
this.reconnectTimer = setTimeout(() => {
this.connect(this.url)
this.reconnectTimer = null
}, 5000)
}
close() {
this.clearHeartbeat()
if (this.socketTask) {
this.socketTask.close()
this.socketTask = null
}
this.isConnected = false
console.log('WebSocketService', 'close called')
}
setDispatchHandler(handler) {
this.dispatchHandler = handler
}
}
export default new WebSocketService()