@@ -7,7 +7,7 @@ export interface SocketTransportOptions {
7
7
socketOptions : Partial < ManagerOptions & SocketOptions >
8
8
onConnect ?: ( ) => void | Promise < void >
9
9
onDisconnect ?: ( reason : string ) => void
10
- onReconnect ?: ( attemptNumber : number ) => void | Promise < void >
10
+ onReconnect ?: ( ) => void | Promise < void >
11
11
logger ?: {
12
12
log : ( message : string , ...args : unknown [ ] ) => void
13
13
error : ( message : string , ...args : unknown [ ] ) => void
@@ -23,7 +23,7 @@ export class SocketTransport {
23
23
private socket : Socket | null = null
24
24
private connectionState : ConnectionState = ConnectionState . DISCONNECTED
25
25
private retryTimeout : NodeJS . Timeout | null = null
26
- private hasConnectedOnce : boolean = false
26
+ private isPreviouslyConnected : boolean = false
27
27
28
28
private readonly retryConfig : RetryConfig = {
29
29
maxInitialAttempts : Infinity ,
@@ -48,31 +48,24 @@ export class SocketTransport {
48
48
// kicks in after a successful initial connection.
49
49
public async connect ( ) : Promise < void > {
50
50
if ( this . connectionState === ConnectionState . CONNECTED ) {
51
- console . log ( `[SocketTransport] Already connected` )
51
+ console . log ( `[SocketTransport#connect ] Already connected` )
52
52
return
53
53
}
54
54
55
55
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` )
57
57
return
58
58
}
59
59
60
60
let attempt = 0
61
61
let delay = this . retryConfig . initialDelay
62
62
63
63
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 ` )
65
65
this . connectionState = attempt === 0 ? ConnectionState . CONNECTING : ConnectionState . RETRYING
66
66
67
67
try {
68
68
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
-
76
69
break
77
70
} catch ( _error ) {
78
71
attempt ++
@@ -82,8 +75,6 @@ export class SocketTransport {
82
75
this . socket = null
83
76
}
84
77
85
- console . log ( `[SocketTransport] Waiting ${ delay } ms before retry...` )
86
-
87
78
const promise = new Promise ( ( resolve ) => {
88
79
this . retryTimeout = setTimeout ( resolve , delay )
89
80
} )
@@ -99,11 +90,12 @@ export class SocketTransport {
99
90
this . retryTimeout = null
100
91
}
101
92
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 } ` )
104
95
} else {
96
+ // Since we have infinite retries this should never happen.
105
97
this . connectionState = ConnectionState . FAILED
106
- console . error ( `[SocketTransport] Failed to connect to ${ this . options . url } , giving up` )
98
+ console . error ( `[SocketTransport#connect] Giving up` )
107
99
}
108
100
}
109
101
@@ -112,7 +104,7 @@ export class SocketTransport {
112
104
this . socket = io ( this . options . url , this . options . socketOptions )
113
105
114
106
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` )
116
108
117
109
if ( this . connectionState !== ConnectionState . CONNECTED ) {
118
110
this . socket ?. disconnect ( )
@@ -122,22 +114,28 @@ export class SocketTransport {
122
114
123
115
// https://socket.io/docs/v4/client-api/#event-connect
124
116
this . socket . on ( "connect" , async ( ) => {
125
- console . log ( `[SocketTransport] on(connect)` )
117
+ console . log (
118
+ `[SocketTransport#_connect] on(connect): isPreviouslyConnected = ${ this . isPreviouslyConnected } ` ,
119
+ )
126
120
127
121
if ( connectionTimeout ) {
128
122
clearTimeout ( connectionTimeout )
129
123
connectionTimeout = null
130
124
}
131
125
132
- if ( this . hasConnectedOnce ) {
133
- this . connectionState = ConnectionState . CONNECTED
126
+ this . connectionState = ConnectionState . CONNECTED
134
127
128
+ if ( this . isPreviouslyConnected ) {
135
129
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 ( )
137
135
}
138
136
}
139
137
140
- this . hasConnectedOnce = true
138
+ this . isPreviouslyConnected = true
141
139
resolve ( )
142
140
} )
143
141
@@ -153,7 +151,9 @@ export class SocketTransport {
153
151
154
152
// https://socket.io/docs/v4/client-api/#event-disconnect
155
153
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
+ )
157
157
this . connectionState = ConnectionState . DISCONNECTED
158
158
159
159
if ( this . options . onDisconnect ) {
@@ -163,12 +163,12 @@ export class SocketTransport {
163
163
// Don't attempt to reconnect if we're manually disconnecting.
164
164
const isManualDisconnect = reason === "io client disconnect"
165
165
166
- if ( ! isManualDisconnect && this . hasConnectedOnce ) {
166
+ if ( ! isManualDisconnect && this . isPreviouslyConnected ) {
167
167
// After successful initial connection, rely entirely on
168
168
// Socket.IO's reconnection logic.
169
- console . log ( "[SocketTransport] will attempt to reconnect" )
169
+ console . log ( "[SocketTransport#_connect ] will attempt to reconnect" )
170
170
} else {
171
- console . log ( "[SocketTransport] will *NOT* attempt to reconnect" )
171
+ console . log ( "[SocketTransport#_connect ] will *NOT* attempt to reconnect" )
172
172
}
173
173
} )
174
174
@@ -177,53 +177,53 @@ export class SocketTransport {
177
177
this . socket . io . on ( "error" , ( error ) => {
178
178
// Connection error.
179
179
if ( connectionTimeout && this . connectionState !== ConnectionState . CONNECTED ) {
180
- console . error ( `[SocketTransport] on(error): ${ error . message } ` )
180
+ console . error ( `[SocketTransport#_connect ] on(error): ${ error . message } ` )
181
181
clearTimeout ( connectionTimeout )
182
182
connectionTimeout = null
183
183
reject ( error )
184
184
}
185
185
186
186
// Post-connection error.
187
187
if ( this . connectionState === ConnectionState . CONNECTED ) {
188
- console . error ( `[SocketTransport] on(error): ${ error . message } ` )
188
+ console . error ( `[SocketTransport#_connect ] on(error): ${ error . message } ` )
189
189
}
190
190
} )
191
191
192
192
// https://socket.io/docs/v4/client-api/#event-reconnect
193
193
// Fired upon a successful reconnection.
194
194
this . socket . io . on ( "reconnect" , ( attempt ) => {
195
- console . log ( `[SocketTransport] on(reconnect) - ${ attempt } ` )
195
+ console . log ( `[SocketTransport#_connect ] on(reconnect) - ${ attempt } ` )
196
196
this . connectionState = ConnectionState . CONNECTED
197
197
198
198
if ( this . options . onReconnect ) {
199
- this . options . onReconnect ( attempt )
199
+ this . options . onReconnect ( )
200
200
}
201
201
} )
202
202
203
203
// https://socket.io/docs/v4/client-api/#event-reconnect_attempt
204
204
// Fired upon an attempt to reconnect.
205
205
this . socket . io . on ( "reconnect_attempt" , ( attempt ) => {
206
- console . log ( `[SocketTransport] on(reconnect_attempt) - ${ attempt } ` )
206
+ console . log ( `[SocketTransport#_connect ] on(reconnect_attempt) - ${ attempt } ` )
207
207
} )
208
208
209
209
// https://socket.io/docs/v4/client-api/#event-reconnect_error
210
210
// Fired upon a reconnection attempt error.
211
211
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 } ` )
213
213
} )
214
214
215
215
// https://socket.io/docs/v4/client-api/#event-reconnect_failed
216
216
// Fired when couldn't reconnect within `reconnectionAttempts`.
217
217
// Since we use infinite retries, this should never fire.
218
218
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` )
220
220
this . connectionState = ConnectionState . FAILED
221
221
} )
222
222
223
223
// This is a custom event fired by the server.
224
224
this . socket . on ( "auth_error" , ( error ) => {
225
225
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 ) } ` ,
227
227
)
228
228
229
229
if ( connectionTimeout && this . connectionState !== ConnectionState . CONNECTED ) {
@@ -236,7 +236,7 @@ export class SocketTransport {
236
236
}
237
237
238
238
public async disconnect ( ) : Promise < void > {
239
- console . log ( `[SocketTransport] Disconnecting...` )
239
+ console . log ( `[SocketTransport#disconnect ] Disconnecting...` )
240
240
241
241
if ( this . retryTimeout ) {
242
242
clearTimeout ( this . retryTimeout )
@@ -251,7 +251,7 @@ export class SocketTransport {
251
251
}
252
252
253
253
this . connectionState = ConnectionState . DISCONNECTED
254
- console . log ( `[SocketTransport] Disconnected` )
254
+ console . log ( `[SocketTransport#disconnect ] Disconnected` )
255
255
}
256
256
257
257
public getSocket ( ) : Socket | null {
@@ -267,14 +267,14 @@ export class SocketTransport {
267
267
}
268
268
269
269
public async reconnect ( ) : Promise < void > {
270
- console . log ( `[SocketTransport] Manually reconnecting...` )
270
+ console . log ( `[SocketTransport#reconnect ] Manually reconnecting...` )
271
271
272
272
if ( this . connectionState === ConnectionState . CONNECTED ) {
273
- console . log ( `[SocketTransport] Already connected` )
273
+ console . log ( `[SocketTransport#reconnect ] Already connected` )
274
274
return
275
275
}
276
276
277
- this . hasConnectedOnce = false
277
+ this . isPreviouslyConnected = false
278
278
await this . disconnect ( )
279
279
await this . connect ( )
280
280
}
0 commit comments