From f76e6b3b9f3c2af3629db2f8a0a005c7f37f691d Mon Sep 17 00:00:00 2001
From: AkiraFukushima
Date: Fri, 6 Oct 2023 00:46:55 +0900
Subject: [PATCH] Fix remove listeners for browser in WebSocket
---
example/browser/src/index.ts | 6 ++++++
megalodon/src/firefish/web_socket.ts | 10 +++++++---
megalodon/src/mastodon/web_socket.ts | 10 +++++++---
megalodon/src/pleroma/web_socket.ts | 10 +++++++---
4 files changed, 27 insertions(+), 9 deletions(-)
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')