@@ -131,15 +131,10 @@ open class SocketEngine:
131
131
132
132
private let url : URL
133
133
134
+ private var lastCommunication : Date ?
134
135
private var pingInterval : Int ?
135
- private var pingTimeout = 0 {
136
- didSet {
137
- pingsMissedMax = Int ( pingTimeout / ( pingInterval ?? 25000 ) )
138
- }
139
- }
136
+ private var pingTimeout = 0
140
137
141
- private var pingsMissed = 0
142
- private var pingsMissedMax = 0
143
138
private var probeWait = ProbeWaitQueue ( )
144
139
private var secure = false
145
140
private var certPinner : CertificatePinning ?
@@ -418,7 +413,6 @@ open class SocketEngine:
418
413
419
414
self . sid = sid
420
415
connected = true
421
- pingsMissed = 0
422
416
423
417
if let upgrades = json [ " upgrades " ] as? [ String ] {
424
418
upgradeWs = upgrades. contains ( " websocket " )
@@ -454,25 +448,23 @@ open class SocketEngine:
454
448
}
455
449
456
450
private func handlePing( with message: String ) {
457
- pingsMissed = 0
458
-
459
451
write ( " " , withType: . pong, withData: [ ] )
460
452
461
453
client? . engineDidReceivePing ( )
462
454
}
463
455
464
456
private func checkPings( ) {
465
457
let pingInterval = self . pingInterval ?? 25_000
458
+ let deadlineMs = Double ( pingInterval + pingTimeout) / 1000
459
+ let timeoutDeadline = DispatchTime . now ( ) + . milliseconds( pingInterval + pingTimeout)
466
460
467
- engineQueue. asyncAfter ( deadline: . now ( ) + . milliseconds ( pingInterval ) ) { [ weak self, id = self . sid] in
461
+ engineQueue. asyncAfter ( deadline: timeoutDeadline ) { [ weak self, id = self . sid] in
468
462
// Make sure not to ping old connections
469
463
guard let this = self , this. sid == id else { return }
470
464
471
- if this. pingsMissed > this . pingsMissedMax {
465
+ if abs ( this. lastCommunication ? . timeIntervalSinceNow ?? deadlineMs ) >= deadlineMs {
472
466
this. closeOutEngine ( reason: " Ping timeout " )
473
467
} else {
474
- this. pingsMissed += 1
475
-
476
468
this. checkPings ( )
477
469
}
478
470
}
@@ -484,13 +476,17 @@ open class SocketEngine:
484
476
open func parseEngineData( _ data: Data ) {
485
477
DefaultSocketLogger . Logger. log ( " Got binary data: \( data) " , type: SocketEngine . logType)
486
478
479
+ lastCommunication = Date ( )
480
+
487
481
client? . parseEngineBinaryData ( data)
488
482
}
489
483
490
484
/// Parses a raw engine.io packet.
491
485
///
492
486
/// - parameter message: The message to parse.
493
487
open func parseEngineMessage( _ message: String ) {
488
+ lastCommunication = Date ( )
489
+
494
490
DefaultSocketLogger . Logger. log ( " Got message: \( message) " , type: SocketEngine . logType)
495
491
496
492
let reader = SocketStringReader ( message: message)
@@ -540,28 +536,6 @@ open class SocketEngine:
540
536
waitingForPost = false
541
537
}
542
538
543
- private func sendPing( ) {
544
- guard connected, let pingInterval = pingInterval else { return }
545
-
546
- // Server is not responding
547
- if pingsMissed > pingsMissedMax {
548
- closeOutEngine ( reason: " Ping timeout " )
549
- return
550
- }
551
-
552
- pingsMissed += 1
553
- write ( " " , withType: . ping, withData: [ ] , completion: nil )
554
-
555
- engineQueue. asyncAfter ( deadline: . now( ) + . milliseconds( pingInterval) ) { [ weak self, id = self . sid] in
556
- // Make sure not to ping old connections
557
- guard let this = self , this. sid == id else { return }
558
-
559
- this. sendPing ( )
560
- }
561
-
562
- client? . engineDidSendPong ( )
563
- }
564
-
565
539
/// Called when the engine should set/update its configs from a given configuration.
566
540
///
567
541
/// parameter config: The `SocketIOClientConfiguration` that should be used to set/update configs.
@@ -713,8 +687,6 @@ extension SocketEngine {
713
687
wsConnected = true
714
688
client? . engineDidWebsocketUpgrade ( headers: headers)
715
689
websocketDidConnect ( )
716
- case let . error( err) :
717
- print ( err)
718
690
case . cancelled:
719
691
wsConnected = false
720
692
websocketDidDisconnect ( error: EngineError . canceled)
0 commit comments