From 3171dd39da52bd4998ff17ec04d9b6157a931c87 Mon Sep 17 00:00:00 2001 From: Canmert Celikler Date: Wed, 19 Mar 2025 17:25:45 +0300 Subject: [PATCH 1/2] fix: do not crash app when a reserved close code is passed the following line throws an error when the "code" argument passed to the close method is a reserved number. This update makes the plugin handle this error that otherwise crashes the app. --- src/websocket/index.android.ts | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/websocket/index.android.ts b/src/websocket/index.android.ts index 0dee052..ec88242 100644 --- a/src/websocket/index.android.ts +++ b/src/websocket/index.android.ts @@ -23,7 +23,19 @@ export class WebsocketConnection implements IWebsocketConnection { } close(code: number, reason: string) { - return this.nativeConnection.close(code, reason); + try { + if ((code >= 1004 && code <= 1006) || (code >= 1015 && code <= 2999)) { + console.warn(`Code ${code} is reserved and may not be used.`); + // Fall back to an accepted code. Otherwise, the error will be caught by try/catch, but the connection won't be closed. + // https://github.com/square/okhttp/blob/master/okhttp/src/commonJvmAndroid/kotlin/okhttp3/internal/ws/WebSocketProtocol.kt#L146 + // https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent/code + code = 1000; + } + return this.nativeConnection.close(code, reason); + } catch (error) { + console.error(error); + return false; + } } cancel() { From b44f301b0722d6be6ee87912befcb674d8448cdf Mon Sep 17 00:00:00 2001 From: Canmert Celikler Date: Thu, 20 Mar 2025 12:01:57 +0300 Subject: [PATCH 2/2] fix: align with iOS close code --- src/websocket/index.android.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/websocket/index.android.ts b/src/websocket/index.android.ts index ec88242..2d0bb5c 100644 --- a/src/websocket/index.android.ts +++ b/src/websocket/index.android.ts @@ -29,7 +29,7 @@ export class WebsocketConnection implements IWebsocketConnection { // Fall back to an accepted code. Otherwise, the error will be caught by try/catch, but the connection won't be closed. // https://github.com/square/okhttp/blob/master/okhttp/src/commonJvmAndroid/kotlin/okhttp3/internal/ws/WebSocketProtocol.kt#L146 // https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent/code - code = 1000; + code = 1001; } return this.nativeConnection.close(code, reason); } catch (error) {