@@ -7,7 +7,7 @@ export interface SocketTransportOptions {
77 socketOptions : Partial < ManagerOptions & SocketOptions >
88 onConnect ?: ( ) => void | Promise < void >
99 onDisconnect ?: ( reason : string ) => void
10- onReconnect ?: ( attemptNumber : number ) => void | Promise < void >
10+ onReconnect ?: ( ) => void | Promise < void >
1111 logger ?: {
1212 log : ( message : string , ...args : unknown [ ] ) => void
1313 error : ( message : string , ...args : unknown [ ] ) => void
@@ -23,7 +23,7 @@ export class SocketTransport {
2323 private socket : Socket | null = null
2424 private connectionState : ConnectionState = ConnectionState . DISCONNECTED
2525 private retryTimeout : NodeJS . Timeout | null = null
26- private hasConnectedOnce : boolean = false
26+ private isPreviouslyConnected : boolean = false
2727
2828 private readonly retryConfig : RetryConfig = {
2929 maxInitialAttempts : Infinity ,
@@ -48,31 +48,24 @@ export class SocketTransport {
4848 // kicks in after a successful initial connection.
4949 public async connect ( ) : Promise < void > {
5050 if ( this . connectionState === ConnectionState . CONNECTED ) {
51- console . log ( `[SocketTransport] Already connected` )
51+ console . log ( `[SocketTransport#connect ] Already connected` )
5252 return
5353 }
5454
5555 if ( this . connectionState === ConnectionState . CONNECTING || this . connectionState === ConnectionState . RETRYING ) {
56- console . log ( `[SocketTransport] Connection attempt already in progress` )
56+ console . log ( `[SocketTransport#connect] Already in progress` )
5757 return
5858 }
5959
6060 let attempt = 0
6161 let delay = this . retryConfig . initialDelay
6262
6363 while ( attempt < this . retryConfig . maxInitialAttempts ) {
64- console . log ( `[SocketTransport] Initial connect attempt ${ attempt + 1 } ` )
64+ console . log ( `[SocketTransport#connect] attempt = ${ attempt + 1 } , delay = ${ delay } ms ` )
6565 this . connectionState = attempt === 0 ? ConnectionState . CONNECTING : ConnectionState . RETRYING
6666
6767 try {
6868 await this . _connect ( )
69- console . log ( `[SocketTransport] Connected to ${ this . options . url } ` )
70- this . connectionState = ConnectionState . CONNECTED
71-
72- if ( this . options . onConnect ) {
73- await this . options . onConnect ( )
74- }
75-
7669 break
7770 } catch ( _error ) {
7871 attempt ++
@@ -82,8 +75,6 @@ export class SocketTransport {
8275 this . socket = null
8376 }
8477
85- console . log ( `[SocketTransport] Waiting ${ delay } ms before retry...` )
86-
8778 const promise = new Promise ( ( resolve ) => {
8879 this . retryTimeout = setTimeout ( resolve , delay )
8980 } )
@@ -99,11 +90,12 @@ export class SocketTransport {
9990 this . retryTimeout = null
10091 }
10192
102- if ( this . connectionState === ConnectionState . CONNECTED ) {
103- console . log ( `[SocketTransport] Connected to ${ this . options . url } ` )
93+ if ( this . socket ?. connected ) {
94+ console . log ( `[SocketTransport#connect] connected - ${ this . options . url } ` )
10495 } else {
96+ // Since we have infinite retries this should never happen.
10597 this . connectionState = ConnectionState . FAILED
106- console . error ( `[SocketTransport] Failed to connect to ${ this . options . url } , giving up` )
98+ console . error ( `[SocketTransport#connect] Giving up` )
10799 }
108100 }
109101
@@ -112,7 +104,7 @@ export class SocketTransport {
112104 this . socket = io ( this . options . url , this . options . socketOptions )
113105
114106 let connectionTimeout : NodeJS . Timeout | null = setTimeout ( ( ) => {
115- console . error ( `[SocketTransport] failed to connect after ${ this . CONNECTION_TIMEOUT } ms` )
107+ console . error ( `[SocketTransport#_connect ] failed to connect after ${ this . CONNECTION_TIMEOUT } ms` )
116108
117109 if ( this . connectionState !== ConnectionState . CONNECTED ) {
118110 this . socket ?. disconnect ( )
@@ -122,22 +114,28 @@ export class SocketTransport {
122114
123115 // https://socket.io/docs/v4/client-api/#event-connect
124116 this . socket . on ( "connect" , async ( ) => {
125- console . log ( `[SocketTransport] on(connect)` )
117+ console . log (
118+ `[SocketTransport#_connect] on(connect): isPreviouslyConnected = ${ this . isPreviouslyConnected } ` ,
119+ )
126120
127121 if ( connectionTimeout ) {
128122 clearTimeout ( connectionTimeout )
129123 connectionTimeout = null
130124 }
131125
132- if ( this . hasConnectedOnce ) {
133- this . connectionState = ConnectionState . CONNECTED
126+ this . connectionState = ConnectionState . CONNECTED
134127
128+ if ( this . isPreviouslyConnected ) {
135129 if ( this . options . onReconnect ) {
136- await this . options . onReconnect ( 0 )
130+ await this . options . onReconnect ( )
131+ }
132+ } else {
133+ if ( this . options . onConnect ) {
134+ await this . options . onConnect ( )
137135 }
138136 }
139137
140- this . hasConnectedOnce = true
138+ this . isPreviouslyConnected = true
141139 resolve ( )
142140 } )
143141
@@ -153,7 +151,9 @@ export class SocketTransport {
153151
154152 // https://socket.io/docs/v4/client-api/#event-disconnect
155153 this . socket . on ( "disconnect" , ( reason , details ) => {
156- console . log ( `[SocketTransport] on(disconnect) (reason: ${ reason } , details: ${ JSON . stringify ( details ) } )` )
154+ console . log (
155+ `[SocketTransport#_connect] on(disconnect) (reason: ${ reason } , details: ${ JSON . stringify ( details ) } )` ,
156+ )
157157 this . connectionState = ConnectionState . DISCONNECTED
158158
159159 if ( this . options . onDisconnect ) {
@@ -163,12 +163,12 @@ export class SocketTransport {
163163 // Don't attempt to reconnect if we're manually disconnecting.
164164 const isManualDisconnect = reason === "io client disconnect"
165165
166- if ( ! isManualDisconnect && this . hasConnectedOnce ) {
166+ if ( ! isManualDisconnect && this . isPreviouslyConnected ) {
167167 // After successful initial connection, rely entirely on
168168 // Socket.IO's reconnection logic.
169- console . log ( "[SocketTransport] will attempt to reconnect" )
169+ console . log ( "[SocketTransport#_connect ] will attempt to reconnect" )
170170 } else {
171- console . log ( "[SocketTransport] will *NOT* attempt to reconnect" )
171+ console . log ( "[SocketTransport#_connect ] will *NOT* attempt to reconnect" )
172172 }
173173 } )
174174
@@ -177,53 +177,53 @@ export class SocketTransport {
177177 this . socket . io . on ( "error" , ( error ) => {
178178 // Connection error.
179179 if ( connectionTimeout && this . connectionState !== ConnectionState . CONNECTED ) {
180- console . error ( `[SocketTransport] on(error): ${ error . message } ` )
180+ console . error ( `[SocketTransport#_connect ] on(error): ${ error . message } ` )
181181 clearTimeout ( connectionTimeout )
182182 connectionTimeout = null
183183 reject ( error )
184184 }
185185
186186 // Post-connection error.
187187 if ( this . connectionState === ConnectionState . CONNECTED ) {
188- console . error ( `[SocketTransport] on(error): ${ error . message } ` )
188+ console . error ( `[SocketTransport#_connect ] on(error): ${ error . message } ` )
189189 }
190190 } )
191191
192192 // https://socket.io/docs/v4/client-api/#event-reconnect
193193 // Fired upon a successful reconnection.
194194 this . socket . io . on ( "reconnect" , ( attempt ) => {
195- console . log ( `[SocketTransport] on(reconnect) - ${ attempt } ` )
195+ console . log ( `[SocketTransport#_connect ] on(reconnect) - ${ attempt } ` )
196196 this . connectionState = ConnectionState . CONNECTED
197197
198198 if ( this . options . onReconnect ) {
199- this . options . onReconnect ( attempt )
199+ this . options . onReconnect ( )
200200 }
201201 } )
202202
203203 // https://socket.io/docs/v4/client-api/#event-reconnect_attempt
204204 // Fired upon an attempt to reconnect.
205205 this . socket . io . on ( "reconnect_attempt" , ( attempt ) => {
206- console . log ( `[SocketTransport] on(reconnect_attempt) - ${ attempt } ` )
206+ console . log ( `[SocketTransport#_connect ] on(reconnect_attempt) - ${ attempt } ` )
207207 } )
208208
209209 // https://socket.io/docs/v4/client-api/#event-reconnect_error
210210 // Fired upon a reconnection attempt error.
211211 this . socket . io . on ( "reconnect_error" , ( error ) => {
212- console . error ( `[SocketTransport] on(reconnect_error): ${ error . message } ` )
212+ console . error ( `[SocketTransport#_connect ] on(reconnect_error): ${ error . message } ` )
213213 } )
214214
215215 // https://socket.io/docs/v4/client-api/#event-reconnect_failed
216216 // Fired when couldn't reconnect within `reconnectionAttempts`.
217217 // Since we use infinite retries, this should never fire.
218218 this . socket . io . on ( "reconnect_failed" , ( ) => {
219- console . error ( `[SocketTransport] on(reconnect_failed) - giving up` )
219+ console . error ( `[SocketTransport#_connect ] on(reconnect_failed) - giving up` )
220220 this . connectionState = ConnectionState . FAILED
221221 } )
222222
223223 // This is a custom event fired by the server.
224224 this . socket . on ( "auth_error" , ( error ) => {
225225 console . error (
226- `[SocketTransport] on(auth_error): ${ error instanceof Error ? error . message : String ( error ) } ` ,
226+ `[SocketTransport#_connect ] on(auth_error): ${ error instanceof Error ? error . message : String ( error ) } ` ,
227227 )
228228
229229 if ( connectionTimeout && this . connectionState !== ConnectionState . CONNECTED ) {
@@ -236,7 +236,7 @@ export class SocketTransport {
236236 }
237237
238238 public async disconnect ( ) : Promise < void > {
239- console . log ( `[SocketTransport] Disconnecting...` )
239+ console . log ( `[SocketTransport#disconnect ] Disconnecting...` )
240240
241241 if ( this . retryTimeout ) {
242242 clearTimeout ( this . retryTimeout )
@@ -251,7 +251,7 @@ export class SocketTransport {
251251 }
252252
253253 this . connectionState = ConnectionState . DISCONNECTED
254- console . log ( `[SocketTransport] Disconnected` )
254+ console . log ( `[SocketTransport#disconnect ] Disconnected` )
255255 }
256256
257257 public getSocket ( ) : Socket | null {
@@ -267,14 +267,14 @@ export class SocketTransport {
267267 }
268268
269269 public async reconnect ( ) : Promise < void > {
270- console . log ( `[SocketTransport] Manually reconnecting...` )
270+ console . log ( `[SocketTransport#reconnect ] Manually reconnecting...` )
271271
272272 if ( this . connectionState === ConnectionState . CONNECTED ) {
273- console . log ( `[SocketTransport] Already connected` )
273+ console . log ( `[SocketTransport#reconnect ] Already connected` )
274274 return
275275 }
276276
277- this . hasConnectedOnce = false
277+ this . isPreviouslyConnected = false
278278 await this . disconnect ( )
279279 await this . connect ( )
280280 }
0 commit comments