mobile: deprecation.

This commit is contained in:
Rajeh Taher
2024-10-14 03:39:46 +03:00
parent 647f8d767f
commit 61a0ff3178
18 changed files with 47 additions and 533 deletions

View File

@@ -0,0 +1,57 @@
import WebSocket from 'ws'
import { DEFAULT_ORIGIN } from '../../Defaults'
import { AbstractSocketClient } from './types'
export class WebSocketClient extends AbstractSocketClient {
protected socket: WebSocket | null = null
get isOpen(): boolean {
return this.socket?.readyState === WebSocket.OPEN
}
get isClosed(): boolean {
return this.socket === null || this.socket?.readyState === WebSocket.CLOSED
}
get isClosing(): boolean {
return this.socket === null || this.socket?.readyState === WebSocket.CLOSING
}
get isConnecting(): boolean {
return this.socket?.readyState === WebSocket.CONNECTING
}
async connect(): Promise<void> {
if(this.socket) {
return
}
this.socket = new WebSocket(this.url, {
origin: DEFAULT_ORIGIN,
headers: this.config.options?.headers as {},
handshakeTimeout: this.config.connectTimeoutMs,
timeout: this.config.connectTimeoutMs,
agent: this.config.agent,
})
this.socket.setMaxListeners(0)
const events = ['close', 'error', 'upgrade', 'message', 'open', 'ping', 'pong', 'unexpected-response']
for(const event of events) {
this.socket?.on(event, (...args: any[]) => this.emit(event, ...args))
}
}
async close(): Promise<void> {
if(!this.socket) {
return
}
this.socket.close()
this.socket = null
}
send(str: string | Uint8Array, cb?: (err?: Error) => void): boolean {
this.socket?.send(str, cb)
return Boolean(this.socket)
}
}