Skip to content

Commit

Permalink
Fix remove listeners for browser in WebSocket
Browse files Browse the repository at this point in the history
  • Loading branch information
h3poteto committed Oct 5, 2023
1 parent 38cd712 commit f76e6b3
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
6 changes: 6 additions & 0 deletions example/browser/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
10 changes: 7 additions & 3 deletions megalodon/src/firefish/web_socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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')
Expand All @@ -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')
Expand Down
10 changes: 7 additions & 3 deletions megalodon/src/mastodon/web_socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down
10 changes: 7 additions & 3 deletions megalodon/src/pleroma/web_socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down

0 comments on commit f76e6b3

Please sign in to comment.