Skip to content

Commit

Permalink
Optimize webscoket reconnection
Browse files Browse the repository at this point in the history
  • Loading branch information
lylwx committed Nov 7, 2023
1 parent fff3f52 commit ca00fce
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions Wox.UI.Tauri/src/utils/WoxMessageHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export class WoxMessageHelper {
} = {}
private woxQueryCallback: ((data: WOXMESSAGE.WoxMessageResponseResult[]) => void | undefined) | undefined
private woxRequestCallback: ((data: WOXMESSAGE.WoxMessage) => void | undefined) | undefined
private connectTimes: number = 1

private shouldReconnect() {
// Check if the WebSocket is in a closed or closing state
Expand All @@ -24,24 +25,35 @@ export class WoxMessageHelper {
/*
Reconnect to Wox Server
*/
private reconnect() {
private doReconnect() {
this.connectTimes++
setTimeout(() => {
this.connectWebsocketServer()
}, 200 * (this.connectTimes > 5 ? 5 : this.connectTimes))
}

/*
connect to Wox Server
*/
private connectWebsocketServer() {
if (this.ws) {
this.ws.close()
}
this.ws = new WebSocket(`ws://127.0.0.1:${this.port}/ws`)
this.ws.onopen = (_) => {
WoxLogHelper.getInstance().log(`Websocket Opened`)
this.connectTimes = 1
}
this.ws.onclose = (_) => {
WoxLogHelper.getInstance().log(`Websocket OnClose: ${JSON.stringify(event)}`)
if (this.shouldReconnect()) {
this.reconnect()
this.doReconnect()
}
}
this.ws.onerror = (event) => {
WoxLogHelper.getInstance().log(`Websocket OnError: ${JSON.stringify(event)}`)
if (this.shouldReconnect()) {
this.reconnect()
this.doReconnect()
}
}
this.ws.onmessage = (event) => {
Expand Down Expand Up @@ -107,7 +119,7 @@ export class WoxMessageHelper {
return
}
this.port = port
this.reconnect()
this.connectWebsocketServer()
}

/**
Expand Down Expand Up @@ -148,9 +160,9 @@ export class WoxMessageHelper {
*/
public sendQueryMessage(params: {
[key: string]: string
}, callback: (data: WOXMESSAGE.WoxMessageResponseResult[]) => void) {
}, callback: (data: WOXMESSAGE.WoxMessageResponseResult[]) => void): Promise<WOXMESSAGE.WoxMessage> {
this.woxQueryCallback = callback
this.sendMessage(WoxMessageMethodEnum.QUERY.code, params)
return this.sendMessage(WoxMessageMethodEnum.QUERY.code, params)
}

/*
Expand Down

0 comments on commit ca00fce

Please sign in to comment.