Skip to content

Commit

Permalink
fix: keepalive options and reconnect canuse inner close event emi…
Browse files Browse the repository at this point in the history
…tted
  • Loading branch information
starknt committed May 23, 2023
1 parent f504900 commit 1f7ff92
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
14 changes: 12 additions & 2 deletions src/base/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export class LiveClient<E extends Record<EventKey, any>> extends EventEmitter<Me

private socket: ISocket | IWebSocket
private timeout: any
private readonly HEARTBEAT_TIME = 30 * 1000
private zlib: IZlib
private live = false
private firstMessage: LiveHelloMessage
Expand Down Expand Up @@ -137,7 +138,7 @@ export class LiveClient<E extends Record<EventKey, any>> extends EventEmitter<Me
this.online = packet.data
clearTimeout(this.timeout)

this.timeout = setTimeout(() => this.heartbeat(), 1000 * 30)
this.timeout = setTimeout(() => this.heartbeat(), this.HEARTBEAT_TIME)

// @ts-expect-error heartbeat event allow
this.emit('heartbeat', this.online)
Expand Down Expand Up @@ -165,6 +166,15 @@ export class LiveClient<E extends Record<EventKey, any>> extends EventEmitter<Me
this.on(CLOSE_EVENT, (e) => {
// @ts-expect-error close event
this.emit('close', e)

if (this.options.keepalive) {
this.closed = true
this.online = 0
this.live = false
clearTimeout(this.timeout)
// console.log('try reconnect to ', this.roomId)
this.socket.reconnect()
}
})
}

Expand Down Expand Up @@ -196,7 +206,7 @@ export class LiveClient<E extends Record<EventKey, any>> extends EventEmitter<Me

close() {
// @ts-expect-error close event
this.emit('close')
this.emit('close', this.socket.type === 'tcp' ? false : { code: 0, reason: 'close', wasClean: true })

if (!this.live)
return
Expand Down
3 changes: 3 additions & 0 deletions src/base/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ export type Merge<A, B> = {
export type Nullable<T> = null | undefined | T

export interface ISocket {
type: 'tcp'
write(data: Uint8Array): void
end(): void
reconnect(): void
}

export interface IWebSocket {
type: 'websocket'
send(data: Uint8Array): void
close(): void
reconnect(): void
Expand Down Expand Up @@ -71,6 +73,7 @@ export const DEFAULT_WS_OPTIONS: Options = {
}

export interface BaseLiveClientOptions extends Options {
timeout: number
socket: ISocket | IWebSocket
room: number
zlib: IZlib
Expand Down
4 changes: 3 additions & 1 deletion src/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export class KeepLiveWS<E extends Record<EventKey, any> = { }> extends LiveClien
const liveOptions: BaseLiveClientOptions = {
...resolvedOptions,
socket: {
type: 'websocket',
send: (data) => {
this.ws.send(data)
},
Expand All @@ -37,12 +38,13 @@ export class KeepLiveWS<E extends Record<EventKey, any> = { }> extends LiveClien
reconnect: () => {
this.ws?.close()
this.ws = null!
const socket = new WebSocket(options.ssl ? WEBSOCKET_SSL_URL : WEBSOCKET_URL)
const socket = new WebSocket(resolvedOptions.url ?? options.ssl ? WEBSOCKET_SSL_URL : WEBSOCKET_URL)
socket.binaryType = 'arraybuffer'
this.ws = socket
this._bindEvent(socket)
},
} as IWebSocket,
timeout: 30 * 1000, // 30s
room: roomId,
zlib: inflates,
}
Expand Down
10 changes: 7 additions & 3 deletions src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ export class KeepLiveTCP<E extends Record<EventKey, any> = { }> extends LiveClie
const liveOptions: BaseLiveClientOptions = {
...resolvedOptions,
socket: {
end: () => {
this.tcpSocket.end()
},
type: 'tcp',
write: (data) => {
this.tcpSocket.write(data)
},
Expand All @@ -65,7 +63,11 @@ export class KeepLiveTCP<E extends Record<EventKey, any> = { }> extends LiveClie
this.tcpSocket = socket
this._bindEvent(socket)
},
end: () => {
this.tcpSocket.end()
},
} as ISocket,
timeout: 30 * 1000, // 30s
zlib: inflates,
room: roomId,
}
Expand Down Expand Up @@ -126,7 +128,9 @@ export class KeepLiveWS<E extends Record<EventKey, any> = { }> extends LiveClien

const liveOptions: BaseLiveClientOptions = {
...resolvedOptions,
timeout: 30 * 1000, // 30s
socket: {
type: 'websocket',
send: (data) => {
this.ws.send(data, (err) => {
if (err)
Expand Down

0 comments on commit 1f7ff92

Please sign in to comment.