import { useGlobalWS } from "@/hooks/useGlobalWS.js";// WebSocket服务管理
|
class WebSocketService {
|
constructor() {
|
this.socketTask = null
|
this.connected = false
|
this.url = ''
|
this.userId = ''
|
this.onMessageCallback = null
|
this.onOpenCallback = null
|
this.onCloseCallback = null
|
this.onErrorCallback = null
|
this.pingTimer = null
|
}
|
|
// 初始化WebSocket连接
|
init(userId, url) {
|
if (!url) {
|
console.error('WebSocket初始化失败:缺少url')
|
return
|
}
|
|
this.userId = userId || ''
|
this.url = url + encodeURIComponent(this.userId)
|
this.connect()
|
}
|
|
// 建立WebSocket连接
|
connect() {
|
try {
|
// 关闭现有连接
|
this.close()
|
|
// 使用uni-app的WebSocket API
|
this.socketTask = uni.connectSocket({
|
url: this.url,
|
success: () => {
|
// console.log('WebSocket连接已请求')
|
},
|
fail: (error) => {
|
// console.error('WebSocket连接请求失败:', error)
|
if (this.onErrorCallback) {
|
this.onErrorCallback(error)
|
}
|
}
|
})
|
|
// 监听连接打开
|
this.socketTask.onOpen(() => {
|
this.connected = true
|
// console.log('WebSocket已连接,userId:', this.userId)
|
this.startPing()
|
if (this.onOpenCallback) {
|
this.onOpenCallback()
|
}
|
})
|
|
// 监听连接关闭
|
this.socketTask.onClose(() => {
|
this.connected = false
|
// console.log('WebSocket已关闭')
|
this.stopPing()
|
if (this.onCloseCallback) {
|
this.onCloseCallback()
|
}
|
})
|
|
// 监听连接错误
|
this.socketTask.onError((error) => {
|
// console.error('WebSocket错误:', error)
|
this.connected = false
|
this.stopPing()
|
if (this.onErrorCallback) {
|
this.onErrorCallback(error)
|
}
|
})
|
|
// 监听消息
|
this.socketTask.onMessage((res) => {
|
try {
|
const message = JSON.parse(res.data)
|
// console.log('收到WebSocket消息:', message)
|
if (this.onMessageCallback) {
|
this.onMessageCallback(message)
|
}
|
} catch (error) {
|
// console.error('解析WebSocket消息失败:', error)
|
}
|
})
|
} catch (error) {
|
// console.error('WebSocket连接失败:', error)
|
if (this.onErrorCallback) {
|
this.onErrorCallback(error)
|
}
|
}
|
}
|
|
// 发送消息
|
send(type, to, payload) {
|
if (!type || !this.socketTask || !this.connected) {
|
console.error('WebSocket发送失败:连接未建立')
|
return
|
}
|
|
const message = {
|
type,
|
from: String(this.userId),
|
to: String(to),
|
payload: payload || null,
|
timestamp: Date.now()
|
}
|
|
this.socketTask.send({
|
data: JSON.stringify(message),
|
success: () => {
|
// console.log('WebSocket消息发送成功:', message)
|
useGlobalWS();
|
},
|
fail: (error) => {
|
// console.error('WebSocket消息发送失败:', error)
|
}
|
})
|
}
|
|
// 开始心跳检测
|
startPing() {
|
this.stopPing()
|
// 每25秒发送一次心跳
|
this.pingTimer = setInterval(() => {
|
if (this.connected) {
|
this.send('ping', 'system')
|
}
|
}, 25000)
|
}
|
|
// 停止心跳检测
|
stopPing() {
|
if (this.pingTimer) {
|
clearInterval(this.pingTimer)
|
this.pingTimer = null
|
}
|
}
|
|
// 关闭连接
|
close() {
|
if (this.socketTask) {
|
this.socketTask.close({
|
code: 1000,
|
reason: '正常关闭',
|
success: () => {
|
// console.log('WebSocket已关闭')
|
},
|
fail: (error) => {
|
// console.error('WebSocket关闭失败:', error)
|
}
|
})
|
this.socketTask = null
|
}
|
this.stopPing()
|
this.connected = false
|
}
|
|
// 设置消息回调
|
setOnMessageCallback(callback) {
|
this.onMessageCallback = callback
|
}
|
|
// 获取当前消息回调(用于检查是否已设置)
|
getOnMessageCallback() {
|
return this.onMessageCallback
|
}
|
|
// 设置连接打开回调
|
setOnOpenCallback(callback) {
|
this.onOpenCallback = callback
|
}
|
|
// 设置连接关闭回调
|
setOnCloseCallback(callback) {
|
this.onCloseCallback = callback
|
}
|
|
// 设置错误回调
|
setOnErrorCallback(callback) {
|
this.onErrorCallback = callback
|
}
|
|
// 获取连接状态
|
getConnected() {
|
return this.connected
|
}
|
}
|
|
// 导出单例实例
|
export default new WebSocketService()
|