Skip to content

Commit c6a6d95

Browse files
committed
Better timeouts
1 parent 6cd8f79 commit c6a6d95

File tree

1 file changed

+10
-38
lines changed

1 file changed

+10
-38
lines changed

Source/SocketIO/Engine/SocketEngine.swift

+10-38
Original file line numberDiff line numberDiff line change
@@ -131,15 +131,10 @@ open class SocketEngine:
131131

132132
private let url: URL
133133

134+
private var lastCommunication: Date?
134135
private var pingInterval: Int?
135-
private var pingTimeout = 0 {
136-
didSet {
137-
pingsMissedMax = Int(pingTimeout / (pingInterval ?? 25000))
138-
}
139-
}
136+
private var pingTimeout = 0
140137

141-
private var pingsMissed = 0
142-
private var pingsMissedMax = 0
143138
private var probeWait = ProbeWaitQueue()
144139
private var secure = false
145140
private var certPinner: CertificatePinning?
@@ -418,7 +413,6 @@ open class SocketEngine:
418413

419414
self.sid = sid
420415
connected = true
421-
pingsMissed = 0
422416

423417
if let upgrades = json["upgrades"] as? [String] {
424418
upgradeWs = upgrades.contains("websocket")
@@ -454,25 +448,23 @@ open class SocketEngine:
454448
}
455449

456450
private func handlePing(with message: String) {
457-
pingsMissed = 0
458-
459451
write("", withType: .pong, withData: [])
460452

461453
client?.engineDidReceivePing()
462454
}
463455

464456
private func checkPings() {
465457
let pingInterval = self.pingInterval ?? 25_000
458+
let deadlineMs = Double(pingInterval + pingTimeout) / 1000
459+
let timeoutDeadline = DispatchTime.now() + .milliseconds(pingInterval + pingTimeout)
466460

467-
engineQueue.asyncAfter(deadline: .now() + .milliseconds(pingInterval)) {[weak self, id = self.sid] in
461+
engineQueue.asyncAfter(deadline: timeoutDeadline) {[weak self, id = self.sid] in
468462
// Make sure not to ping old connections
469463
guard let this = self, this.sid == id else { return }
470464

471-
if this.pingsMissed > this.pingsMissedMax {
465+
if abs(this.lastCommunication?.timeIntervalSinceNow ?? deadlineMs) >= deadlineMs {
472466
this.closeOutEngine(reason: "Ping timeout")
473467
} else {
474-
this.pingsMissed += 1
475-
476468
this.checkPings()
477469
}
478470
}
@@ -484,13 +476,17 @@ open class SocketEngine:
484476
open func parseEngineData(_ data: Data) {
485477
DefaultSocketLogger.Logger.log("Got binary data: \(data)", type: SocketEngine.logType)
486478

479+
lastCommunication = Date()
480+
487481
client?.parseEngineBinaryData(data)
488482
}
489483

490484
/// Parses a raw engine.io packet.
491485
///
492486
/// - parameter message: The message to parse.
493487
open func parseEngineMessage(_ message: String) {
488+
lastCommunication = Date()
489+
494490
DefaultSocketLogger.Logger.log("Got message: \(message)", type: SocketEngine.logType)
495491

496492
let reader = SocketStringReader(message: message)
@@ -540,28 +536,6 @@ open class SocketEngine:
540536
waitingForPost = false
541537
}
542538

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-
565539
/// Called when the engine should set/update its configs from a given configuration.
566540
///
567541
/// parameter config: The `SocketIOClientConfiguration` that should be used to set/update configs.
@@ -713,8 +687,6 @@ extension SocketEngine {
713687
wsConnected = true
714688
client?.engineDidWebsocketUpgrade(headers: headers)
715689
websocketDidConnect()
716-
case let .error(err):
717-
print(err)
718690
case .cancelled:
719691
wsConnected = false
720692
websocketDidDisconnect(error: EngineError.canceled)

0 commit comments

Comments
 (0)