1- import type { WebSocket as WSWebSocket } from 'ws '
1+ import WebSocket from './WebSocket '
22
33import {
44 CHANNEL_EVENTS ,
@@ -54,7 +54,7 @@ export interface WebSocketLikeConstructor {
5454 ) : WebSocketLike
5555}
5656
57- export type WebSocketLike = WebSocket | WSWebSocket | WSWebSocketDummy
57+ export type WebSocketLike = WebSocket | WSWebSocketDummy
5858
5959export interface WebSocketLikeError {
6060 error : any
@@ -81,13 +81,13 @@ export type RealtimeClientOptions = {
8181 accessToken ?: ( ) => Promise < string | null >
8282}
8383
84- const NATIVE_WEBSOCKET_AVAILABLE = typeof WebSocket !== 'undefined'
8584const WORKER_SCRIPT = `
8685 addEventListener("message", (e) => {
8786 if (e.data.event === "start") {
8887 setInterval(() => postMessage({ event: "keepAlive" }), e.data.interval);
8988 }
9089 });`
90+
9191export default class RealtimeClient {
9292 accessTokenValue : string | null = null
9393 apiKey : string | null = null
@@ -209,33 +209,21 @@ export default class RealtimeClient {
209209 if ( this . conn ) {
210210 return
211211 }
212-
212+ if ( ! this . transport ) {
213+ this . transport = WebSocket
214+ }
213215 if ( this . transport ) {
214216 this . conn = new this . transport ( this . endpointURL ( ) , undefined , {
215217 headers : this . headers ,
216218 } )
217219 this . setupConnection ( )
218220 return
219221 }
220-
221- if ( NATIVE_WEBSOCKET_AVAILABLE ) {
222- this . conn = new WebSocket ( this . endpointURL ( ) )
223- this . setupConnection ( )
224- return
225- }
226-
227222 this . conn = new WSWebSocketDummy ( this . endpointURL ( ) , undefined , {
228223 close : ( ) => {
229224 this . conn = null
230225 } ,
231226 } )
232-
233- import ( 'ws' ) . then ( ( { default : WS } ) => {
234- this . conn = new WS ( this . endpointURL ( ) , undefined , {
235- headers : this . headers ,
236- } )
237- this . setupConnection ( )
238- } )
239227 }
240228
241229 /**
@@ -560,7 +548,7 @@ export default class RealtimeClient {
560548 }
561549
562550 /** @internal */
563- private async _onConnOpen ( ) {
551+ private _onConnOpen ( ) {
564552 this . log ( 'transport' , `connected to ${ this . endpointURL ( ) } ` )
565553 this . flushSendBuffer ( )
566554 this . reconnectTimer . reset ( )
@@ -576,11 +564,10 @@ export default class RealtimeClient {
576564 } else {
577565 this . log ( 'worker' , `starting default worker` )
578566 }
579-
580567 const objectUrl = this . _workerObjectUrl ( this . workerUrl ! )
581568 this . workerRef = new Worker ( objectUrl )
582569 this . workerRef . onerror = ( error ) => {
583- this . log ( 'worker' , 'worker error' , error . message )
570+ this . log ( 'worker' , 'worker error' , ( error as ErrorEvent ) . message )
584571 this . workerRef ! . terminate ( )
585572 }
586573 this . workerRef . onmessage = ( event ) => {
@@ -593,12 +580,10 @@ export default class RealtimeClient {
593580 interval : this . heartbeatIntervalMs ,
594581 } )
595582 }
596-
597- this . stateChangeCallbacks . open . forEach ( ( callback ) => callback ( ) ) !
583+ this . stateChangeCallbacks . open . forEach ( ( callback ) => callback ( ) )
598584 }
599585
600586 /** @internal */
601-
602587 private _onConnClose ( event : any ) {
603588 this . log ( 'transport' , 'close' , event )
604589 this . _triggerChanError ( )
@@ -631,7 +616,6 @@ export default class RealtimeClient {
631616 }
632617 const prefix = url . match ( / \? / ) ? '&' : '?'
633618 const query = new URLSearchParams ( params )
634-
635619 return `${ url } ${ prefix } ${ query } `
636620 }
637621
0 commit comments