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 {
@@ -519,8 +503,7 @@ export default class RealtimeClient {
519503 if ( this . conn ) {
520504 this . conn . binaryType = 'arraybuffer'
521505 this . conn . onopen = ( ) => this . _onConnOpen ( )
522- this . conn . onerror = ( error : WebSocketLikeError ) =>
523- this . _onConnError ( error as WebSocketLikeError )
506+ this . conn . onerror = ( error : Event ) => this . _onConnError ( error )
524507 this . conn . onmessage = ( event : any ) => this . _onConnMessage ( event )
525508 this . conn . onclose = ( event : any ) => this . _onConnClose ( event )
526509 }
@@ -541,8 +524,7 @@ export default class RealtimeClient {
541524
542525 this . log (
543526 'receive' ,
544- `${ payload . status || '' } ${ topic } ${ event } ${
545- ( ref && '(' + ref + ')' ) || ''
527+ `${ payload . status || '' } ${ topic } ${ event } ${ ( ref && '(' + ref + ')' ) || ''
546528 } `,
547529 payload
548530 )
@@ -603,8 +585,8 @@ export default class RealtimeClient {
603585 }
604586
605587 /** @internal */
606- private _onConnError ( error : WebSocketLikeError ) {
607- this . log ( 'transport' , error . message )
588+ private _onConnError ( error : Event ) {
589+ this . log ( 'transport' , ` ${ error } ` )
608590 this . _triggerChanError ( )
609591 this . stateChangeCallbacks . error . forEach ( ( callback ) => callback ( error ) )
610592 }
@@ -639,25 +621,4 @@ export default class RealtimeClient {
639621 }
640622 return result_url
641623 }
642- }
643-
644- class WSWebSocketDummy {
645- binaryType : string = 'arraybuffer'
646- close : Function
647- onclose : Function = ( ) => { }
648- onerror : Function = ( ) => { }
649- onmessage : Function = ( ) => { }
650- onopen : Function = ( ) => { }
651- readyState : number = SOCKET_STATES . connecting
652- send : Function = ( ) => { }
653- url : string | URL | null = null
654-
655- constructor (
656- address : string ,
657- _protocols : undefined ,
658- options : { close : Function }
659- ) {
660- this . url = address
661- this . close = options . close
662- }
663- }
624+ }
0 commit comments