@satorijs/core
Advanced tools
Comparing version 4.2.0 to 4.2.1
@@ -144,2 +144,3 @@ import { Context, Service, Logger, z } from 'cordis'; | ||
protected socket: WebSocket; | ||
protected connectionId: number; | ||
protected abstract prepare(): Awaitable<WebSocket>; | ||
@@ -146,0 +147,0 @@ protected abstract accept(socket: WebSocket): void; |
{ | ||
"name": "@satorijs/core", | ||
"description": "Core components of Satorijs", | ||
"version": "4.2.0", | ||
"version": "4.2.1", | ||
"type": "module", | ||
@@ -60,3 +60,3 @@ "main": "lib/index.cjs", | ||
"peerDependencies": { | ||
"cordis": "^3.17.8" | ||
"cordis": "^3.18.0" | ||
}, | ||
@@ -67,5 +67,5 @@ "dependencies": { | ||
"@satorijs/protocol": "^1.4.2", | ||
"cordis": "^3.17.8", | ||
"cordis": "^3.18.0", | ||
"cosmokit": "^1.6.2" | ||
} | ||
} |
@@ -39,2 +39,3 @@ import { Awaitable, remove, Time } from 'cosmokit' | ||
protected socket: WebSocket | ||
protected connectionId = 0 | ||
@@ -51,7 +52,29 @@ protected abstract prepare(): Awaitable<WebSocket> | ||
async start() { | ||
let _retryCount = 0 | ||
let retryCount = 0 | ||
const connectionId = ++this.connectionId | ||
const logger = this.ctx.logger('adapter') | ||
const { retryTimes, retryInterval, retryLazy } = this.config | ||
const reconnect = async (initial = false) => { | ||
const reconnect = (initial: boolean, message: string) => { | ||
if (!this.getActive() || connectionId !== this.connectionId) return | ||
let timeout = retryInterval | ||
if (retryCount >= retryTimes) { | ||
if (initial) { | ||
return this.setStatus(Status.OFFLINE, new Error(message)) | ||
} else { | ||
timeout = retryLazy | ||
} | ||
} | ||
retryCount++ | ||
this.setStatus(Status.RECONNECT) | ||
logger.warn(`${message}, will retry in ${Time.format(timeout)}...`) | ||
setTimeout(() => { | ||
if (!this.getActive() || connectionId !== this.connectionId) return | ||
connect() | ||
}, timeout) | ||
} | ||
const connect = async (initial = false) => { | ||
logger.debug('websocket client opening') | ||
@@ -62,3 +85,3 @@ let socket: WebSocket | ||
} catch (error) { | ||
logger.warn(error) | ||
reconnect(initial, error.toString() || `failed to prepare websocket`) | ||
return | ||
@@ -75,26 +98,9 @@ } | ||
socket.addEventListener('close', ({ code, reason }) => { | ||
this.socket = null | ||
if (this.socket === socket) this.socket = null | ||
logger.debug(`websocket closed with ${code}`) | ||
if (!this.getActive()) return | ||
const message = reason.toString() || `failed to connect to ${url}, code: ${code}` | ||
let timeout = retryInterval | ||
if (_retryCount >= retryTimes) { | ||
if (initial) { | ||
return this.setStatus(Status.OFFLINE, new Error(message)) | ||
} else { | ||
timeout = retryLazy | ||
} | ||
} | ||
_retryCount++ | ||
this.setStatus(Status.RECONNECT) | ||
logger.warn(`${message}, will retry in ${Time.format(timeout)}...`) | ||
setTimeout(() => { | ||
if (this.getActive()) reconnect() | ||
}, timeout) | ||
reconnect(initial, reason.toString() || `failed to connect to ${url}, code: ${code}`) | ||
}) | ||
socket.addEventListener('open', () => { | ||
_retryCount = 0 | ||
retryCount = 0 | ||
this.socket = socket | ||
@@ -106,3 +112,3 @@ logger.info('connect to server: %c', url) | ||
reconnect(true) | ||
connect(true) | ||
} | ||
@@ -109,0 +115,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
117610
2435
Updatedcordis@^3.18.0