Skip to content

Commit

Permalink
fix(websockets): persist websockets until they're closed
Browse files Browse the repository at this point in the history
  • Loading branch information
edusperoni committed Feb 27, 2023
1 parent f6e7ba3 commit a5e4c51
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/nativescript-websockets/websocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ const CLOSED = 3;

const CLOSE_NORMAL = 1000;

const openWebsockets = new Set<WebSocketPolyfill>();

// const WEBSOCKET_EVENTS = ['close', 'error', 'message', 'open'];

// type WebSocketEventDefinitions = {
Expand Down Expand Up @@ -118,6 +120,7 @@ export class WebSocket implements WebSocketPolyfill {
// );
this._registerEvents();
this.nativeBridge.connect(url, protocols, { headers });
openWebsockets.add(this);
// NativeWebSocketModule.connect(url, protocols, {headers}, this._socketId);
}
addEventListener(type: string, callback: EventListenerOrEventListenerObject | null, options?: boolean | AddEventListenerOptions): void {
Expand Down Expand Up @@ -265,6 +268,7 @@ export class WebSocket implements WebSocketPolyfill {
}
_websocketClosed(code: number, reason: string, wasClean: boolean) {
this.readyState = this.CLOSED;
openWebsockets.delete(this);
this.dispatchEvent(
new WebSocketEvent('close', {
code: code,
Expand All @@ -279,6 +283,7 @@ export class WebSocket implements WebSocketPolyfill {
// _websocketPong(pongPayload: NSData) {}
_websocketFailed(error: string) {
this.readyState = this.CLOSED;
openWebsockets.delete(this);
this.dispatchEvent(
new WebSocketEvent('error', {
message: error,
Expand Down

0 comments on commit a5e4c51

Please sign in to comment.