diff --git a/packages/websocket/src/client/index.ts b/packages/websocket/src/client/index.ts index 5d6d4c4e..54d898c9 100644 --- a/packages/websocket/src/client/index.ts +++ b/packages/websocket/src/client/index.ts @@ -53,13 +53,11 @@ export class WebSocket extends EventEmitter { return } this.client.on('open', (event) => { - console.debug('client WebSocket call open', this.onopen) this.onopen?.(event) manager.add(this) }) this.client.on('message', (event) => this.onmessage?.(event)) this.client.on('close', (event) => { - console.log('client WebSocket call close', this.onclose) this.onclose?.(event) manager.del(this) }) @@ -101,3 +99,4 @@ export class WebSocket extends EventEmitter { this.removeAllListeners() } } +global.setGlobal('WebSocket', WebSocket) diff --git a/packages/websocket/src/client/netty/handler.ts b/packages/websocket/src/client/netty/handler.ts index 308013f9..0a23c527 100644 --- a/packages/websocket/src/client/netty/handler.ts +++ b/packages/websocket/src/client/netty/handler.ts @@ -29,7 +29,7 @@ export class WebSocketClientHandler extends WebSocketClientHandlerAdapter { } channelInactive(ctx: any) { console.debug(`${ctx} channelInactive`) - this.client.onclose({ code: 0, reason: 'server connection channel inactive!' }) + this.client.onclose({ code: 0, reason: 'client connection channel inactive!' }) } channelRead0(ctx: any, msg: any) { console.trace(`${ctx} channelRead0 ${msg}`) @@ -50,7 +50,7 @@ export class WebSocketClientHandler extends WebSocketClientHandlerAdapter { if (frame instanceof TextWebSocketFrame) { this.client.onmessage({ data: frame.text() }) } else if (frame instanceof CloseWebSocketFrame) { - this.client.onclose({ code: 0, reason: 'server send CloseWebSocketFrame!' }) + this.client.onclose({ code: 0, reason: 'server close connection!' }) } } exceptionCaught(ctx: any, cause: Error) { diff --git a/packages/websocket/src/client/netty/index.ts b/packages/websocket/src/client/netty/index.ts index 4857d44a..3ad34510 100644 --- a/packages/websocket/src/client/netty/index.ts +++ b/packages/websocket/src/client/netty/index.ts @@ -100,9 +100,11 @@ export class NettyWebSocket extends Transport { handler.handshakeFuture.addListener(new ChannelFutureListener((future: any) => { try { future.sync() + // only trigger onconnect when not have error this.onconnect({}) } catch (error) { - this.onerror({ error }) + // ignore error exceptionCaught from handler + // this.onerror({ error }) } })) })) diff --git a/packages/websocket/src/client/transport.ts b/packages/websocket/src/client/transport.ts index 90b5bac2..b12356e2 100644 --- a/packages/websocket/src/client/transport.ts +++ b/packages/websocket/src/client/transport.ts @@ -57,6 +57,8 @@ export abstract class Transport extends EventEmitter { this.doClose(code, reason) } catch (error) { this.onerror({ error }) + } finally { + this.removeAllListeners() } } else { console.debug(`${this.id} call close but state is ${this.readyStatus}`) @@ -69,7 +71,6 @@ export abstract class Transport extends EventEmitter { } onconnect(event: Event) { - console.debug(`${this.id} call onconnect`) if (this.readyStatus != WebSocket.OPEN) { this.readyStatus = WebSocket.OPEN this.emit('open', event) diff --git a/packages/websocket/src/socket.io/client.ts b/packages/websocket/src/socket.io/client.ts index baed82c7..b31a43e0 100644 --- a/packages/websocket/src/socket.io/client.ts +++ b/packages/websocket/src/socket.io/client.ts @@ -200,7 +200,7 @@ export class Client< * @private */ private close(): void { - console.debug(`client ${this.id} clise - reason: forcing transport close`) + console.debug(`client ${this.id} close - reason: forcing transport close`) if ("open" === this.conn.readyState) { console.debug("forcing transport close") this.conn.close()