1- import WebSocket from './WebSocket '
1+ import { WebSocket } from 'isows '
22
33import {
44 CHANNEL_EVENTS ,
@@ -44,18 +44,16 @@ export type HeartbeatStatus =
4444 | 'timeout'
4545 | 'disconnected'
4646
47- const noop = ( ) => { }
47+ const noop = ( ) => { }
4848
4949export interface WebSocketLikeConstructor {
50- new (
50+ new (
5151 address : string | URL ,
5252 _ignored ?: any ,
5353 options ?: { headers : Object | undefined }
54- ) : WebSocketLike
54+ ) : WebSocket
5555}
5656
57- export type WebSocketLike = WebSocket | WSWebSocketDummy
58-
5957export interface WebSocketLikeError {
6058 error : any
6159 message : string
@@ -109,7 +107,7 @@ export default class RealtimeClient {
109107 encode : Function
110108 decode : Function
111109 reconnectAfterMs : Function
112- conn : WebSocketLike | null = null
110+ conn : WebSocket | null = null
113111 sendBuffer : Function [ ] = [ ]
114112 serializer : Serializer = new Serializer ( )
115113 stateChangeCallbacks : {
@@ -118,11 +116,11 @@ export default class RealtimeClient {
118116 error : Function [ ]
119117 message : Function [ ]
120118 } = {
121- open : [ ] ,
122- close : [ ] ,
123- error : [ ] ,
124- message : [ ] ,
125- }
119+ open : [ ] ,
120+ close : [ ] ,
121+ error : [ ] ,
122+ message : [ ] ,
123+ }
126124 fetch : Fetch
127125 accessToken : ( ( ) => Promise < string | null > ) | null = null
128126 worker ?: boolean
@@ -176,13 +174,13 @@ export default class RealtimeClient {
176174 this . reconnectAfterMs = options ?. reconnectAfterMs
177175 ? options . reconnectAfterMs
178176 : ( tries : number ) => {
179- return [ 1000 , 2000 , 5000 , 10000 ] [ tries - 1 ] || 10000
180- }
177+ return [ 1000 , 2000 , 5000 , 10000 ] [ tries - 1 ] || 10000
178+ }
181179 this . encode = options ?. encode
182180 ? options . encode
183181 : ( payload : JSON , callback : Function ) => {
184- return callback ( JSON . stringify ( payload ) )
185- }
182+ return callback ( JSON . stringify ( payload ) )
183+ }
186184 this . decode = options ?. decode
187185 ? options . decode
188186 : this . serializer . decode . bind ( this . serializer )
@@ -212,25 +210,11 @@ export default class RealtimeClient {
212210 if ( ! this . transport ) {
213211 this . transport = WebSocket
214212 }
215- if ( this . transport ) {
216- // Detect if using the native browser WebSocket
217- const isBrowser =
218- typeof window !== 'undefined' && this . transport === window . WebSocket
219- if ( isBrowser ) {
220- this . conn = new this . transport ( this . endpointURL ( ) )
221- } else {
222- this . conn = new this . transport ( this . endpointURL ( ) , undefined , {
223- headers : this . headers ,
224- } )
225- }
226- this . setupConnection ( )
227- return
228- }
229- this . conn = new WSWebSocketDummy ( this . endpointURL ( ) , undefined , {
230- close : ( ) => {
231- this . conn = null
232- } ,
233- } )
213+
214+ this . conn = new this . transport ( this . endpointURL ( ) , undefined , {
215+ headers : this . headers ,
216+ } ) as WebSocket
217+ this . setupConnection ( )
234218 }
235219
236220 /**
@@ -252,7 +236,7 @@ export default class RealtimeClient {
252236 */
253237 disconnect ( code ?: number , reason ?: string ) : void {
254238 if ( this . conn ) {
255- this . conn . onclose = function ( ) { } // noop
239+ this . conn . onclose = function ( ) { } // noop
256240 if ( code ) {
257241 this . conn . close ( code , reason ?? '' )
258242 } else {
@@ -518,8 +502,7 @@ export default class RealtimeClient {
518502 if ( this . conn ) {
519503 this . conn . binaryType = 'arraybuffer'
520504 this . conn . onopen = ( ) => this . _onConnOpen ( )
521- this . conn . onerror = ( error : WebSocketLikeError ) =>
522- this . _onConnError ( error as WebSocketLikeError )
505+ this . conn . onerror = ( error : Event ) => this . _onConnError ( error )
523506 this . conn . onmessage = ( event : any ) => this . _onConnMessage ( event )
524507 this . conn . onclose = ( event : any ) => this . _onConnClose ( event )
525508 }
@@ -540,8 +523,7 @@ export default class RealtimeClient {
540523
541524 this . log (
542525 'receive' ,
543- `${ payload . status || '' } ${ topic } ${ event } ${
544- ( ref && '(' + ref + ')' ) || ''
526+ `${ payload . status || '' } ${ topic } ${ event } ${ ( ref && '(' + ref + ')' ) || ''
545527 } `,
546528 payload
547529 )
@@ -613,8 +595,8 @@ export default class RealtimeClient {
613595 }
614596
615597 /** @internal */
616- private _onConnError ( error : WebSocketLikeError ) {
617- this . log ( 'transport' , error . message )
598+ private _onConnError ( error : Event ) {
599+ this . log ( 'transport' , ` ${ error } ` )
618600 this . _triggerChanError ( )
619601 this . stateChangeCallbacks . error . forEach ( ( callback ) => callback ( error ) )
620602 }
@@ -649,25 +631,4 @@ export default class RealtimeClient {
649631 }
650632 return result_url
651633 }
652- }
653-
654- class WSWebSocketDummy {
655- binaryType : string = 'arraybuffer'
656- close : Function
657- onclose : Function = ( ) => { }
658- onerror : Function = ( ) => { }
659- onmessage : Function = ( ) => { }
660- onopen : Function = ( ) => { }
661- readyState : number = SOCKET_STATES . connecting
662- send : Function = ( ) => { }
663- url : string | URL | null = null
664-
665- constructor (
666- address : string ,
667- _protocols : undefined ,
668- options : { close : Function }
669- ) {
670- this . url = address
671- this . close = options . close
672- }
673- }
634+ }
0 commit comments