diff --git a/example/browser/src/index.ts b/example/browser/src/index.ts index 89b162bd0..6c461e7ba 100644 --- a/example/browser/src/index.ts +++ b/example/browser/src/index.ts @@ -47,3 +47,9 @@ stream.on('close', () => { stream.on('parser-error', (err: Error) => { console.error(err) }) + +setTimeout(() => { + stream.removeAllListeners() + stream.stop() + console.log('closed') +}, 10000) diff --git a/megalodon/src/firefish/web_socket.ts b/megalodon/src/firefish/web_socket.ts index f38e7656a..62cadb057 100644 --- a/megalodon/src/firefish/web_socket.ts +++ b/megalodon/src/firefish/web_socket.ts @@ -101,7 +101,7 @@ export default class WebSocket extends EventEmitter implements WebSocketInterfac private _resetConnection() { if (this._client) { this._client.close(1000) - this._client.removeAllListeners() + this._clearBinding() this._client = null } @@ -233,7 +233,11 @@ export default class WebSocket extends EventEmitter implements WebSocketInterfac if (this._client) { // In reconnect, we want to close the connection immediately, // because recoonect is necessary when some problems occur. - this._client.terminate() + if (isBrowser()) { + this._client.close() + } else { + this._client.terminate() + } } // Call connect methods console.log('Reconnecting') @@ -247,7 +251,7 @@ export default class WebSocket extends EventEmitter implements WebSocketInterfac * Clear binding event for websocket client. */ private _clearBinding() { - if (this._client) { + if (this._client && !isBrowser()) { this._client.removeAllListeners('close') this._client.removeAllListeners('pong') this._client.removeAllListeners('open') diff --git a/megalodon/src/mastodon/web_socket.ts b/megalodon/src/mastodon/web_socket.ts index 52517f102..74b8dfe7d 100644 --- a/megalodon/src/mastodon/web_socket.ts +++ b/megalodon/src/mastodon/web_socket.ts @@ -99,7 +99,7 @@ export default class Streaming extends EventEmitter implements WebSocketInterfac private _resetConnection() { if (this._client) { this._client.close(1000) - this._client.removeAllListeners() + this._clearBinding() this._client = null } @@ -132,7 +132,11 @@ export default class Streaming extends EventEmitter implements WebSocketInterfac if (this._client) { // In reconnect, we want to close the connection immediately, // because recoonect is necessary when some problems occur. - this._client.terminate() + if (isBrowser()) { + this._client.close() + } else { + this._client.terminate() + } } // Call connect methods console.log('Reconnecting') @@ -192,7 +196,7 @@ export default class Streaming extends EventEmitter implements WebSocketInterfac * Clear binding event for web socket client. */ private _clearBinding() { - if (this._client) { + if (this._client && !isBrowser()) { this._client.removeAllListeners('close') this._client.removeAllListeners('pong') this._client.removeAllListeners('open') diff --git a/megalodon/src/pleroma/web_socket.ts b/megalodon/src/pleroma/web_socket.ts index 083e38e64..d0e1f6013 100644 --- a/megalodon/src/pleroma/web_socket.ts +++ b/megalodon/src/pleroma/web_socket.ts @@ -101,7 +101,7 @@ export default class WebSocket extends EventEmitter implements WebSocketInterfac private _resetConnection() { if (this._client) { this._client.close(1000) - this._client.removeAllListeners() + this._clearBinding() this._client = null } @@ -134,7 +134,11 @@ export default class WebSocket extends EventEmitter implements WebSocketInterfac if (this._client) { // In reconnect, we want to close the connection immediately, // because recoonect is necessary when some problems occur. - this._client.terminate() + if (isBrowser()) { + this._client.close() + } else { + this._client.terminate() + } } // Call connect methods console.log('Reconnecting') @@ -194,7 +198,7 @@ export default class WebSocket extends EventEmitter implements WebSocketInterfac * Clear binding event for web socket client. */ private _clearBinding() { - if (this._client) { + if (this._client && !isBrowser()) { this._client.removeAllListeners('close') this._client.removeAllListeners('pong') this._client.removeAllListeners('open')