|
@@ -43,25 +43,25 @@ class Socket {
|
|
}
|
|
}
|
|
//打开
|
|
//打开
|
|
onOpen(event: Event) {
|
|
onOpen(event: Event) {
|
|
- console.log('WebSocket opened:', event)
|
|
|
|
|
|
+ // console.log('WebSocket opened:', event)
|
|
this.reconnectAttempts = 0
|
|
this.reconnectAttempts = 0
|
|
this.startHeartbeat()
|
|
this.startHeartbeat()
|
|
this.emit('open', event)
|
|
this.emit('open', event)
|
|
}
|
|
}
|
|
//收到的WebSocket消息
|
|
//收到的WebSocket消息
|
|
onMessage(event: MessageEvent) {
|
|
onMessage(event: MessageEvent) {
|
|
- console.log('WebSocket message received:', event.data)
|
|
|
|
|
|
+ // console.log('WebSocket message received:', event.data)
|
|
this.emit('message', event.data)
|
|
this.emit('message', event.data)
|
|
}
|
|
}
|
|
//错误
|
|
//错误
|
|
onError(event: Event) {
|
|
onError(event: Event) {
|
|
- console.error('WebSocket error:', event)
|
|
|
|
|
|
+ // console.error('WebSocket error:', event)
|
|
this.emit('error', event)
|
|
this.emit('error', event)
|
|
}
|
|
}
|
|
//重连逻辑中,在连接失败后自动重新连接
|
|
//重连逻辑中,在连接失败后自动重新连接
|
|
onClose(event: CloseEvent) {
|
|
onClose(event: CloseEvent) {
|
|
- console.log('WebSocket closed:', event)
|
|
|
|
- this.stopHeartbeat()
|
|
|
|
|
|
+ // console.log('WebSocket closed:', event)
|
|
|
|
+ // this.stopHeartbeat()
|
|
this.emit('close', event)
|
|
this.emit('close', event)
|
|
// 重连逻辑中,在连接失败后自动重新连接,但会限制重连的次数和每次重连之间的间隔时间
|
|
// 重连逻辑中,在连接失败后自动重新连接,但会限制重连的次数和每次重连之间的间隔时间
|
|
if (this.opts.maxReconnectAttempts !== 0 && this.reconnectAttempts < this.opts.maxReconnectAttempts!) {
|
|
if (this.opts.maxReconnectAttempts !== 0 && this.reconnectAttempts < this.opts.maxReconnectAttempts!) {
|
|
@@ -117,7 +117,7 @@ class Socket {
|
|
}
|
|
}
|
|
|
|
|
|
export function useSocket(url: string = import.meta.env.VITE_WEBSOCKET_SERVER, opts?: SocketOptions) {
|
|
export function useSocket(url: string = import.meta.env.VITE_WEBSOCKET_SERVER, opts?: SocketOptions) {
|
|
- console.log('websocket-url: ', url)
|
|
|
|
|
|
+ // console.log('websocket-url: ', url)
|
|
const socket = new Socket(url, opts)
|
|
const socket = new Socket(url, opts)
|
|
|
|
|
|
onUnmounted(() => {
|
|
onUnmounted(() => {
|