Skip to content

Commit f6fa5b7

Browse files
committed
don't lose ping precision
1 parent d8104f4 commit f6fa5b7

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

Source/SocketIO/Engine/SocketEngine.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,10 @@ public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePoll
123123

124124
private let url: URL
125125

126-
private var pingInterval: Double?
127-
private var pingTimeout = 0.0 {
126+
private var pingInterval: Int?
127+
private var pingTimeout = 0 {
128128
didSet {
129-
pongsMissedMax = Int(pingTimeout / (pingInterval ?? 25))
129+
pongsMissedMax = Int(pingTimeout / (pingInterval ?? 25000))
130130
}
131131
}
132132

@@ -459,9 +459,9 @@ public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePoll
459459
upgradeWs = false
460460
}
461461

462-
if let pingInterval = json["pingInterval"] as? Double, let pingTimeout = json["pingTimeout"] as? Double {
463-
self.pingInterval = pingInterval / 1000.0
464-
self.pingTimeout = pingTimeout / 1000.0
462+
if let pingInterval = json["pingInterval"] as? Int, let pingTimeout = json["pingTimeout"] as? Int {
463+
self.pingInterval = pingInterval
464+
self.pingTimeout = pingTimeout
465465
}
466466

467467
if !forcePolling && !forceWebsockets && upgradeWs {
@@ -550,7 +550,7 @@ public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePoll
550550
}
551551

552552
private func sendPing() {
553-
guard connected else { return }
553+
guard connected, let pingInterval = pingInterval else { return }
554554

555555
// Server is not responding
556556
if pongsMissed > pongsMissedMax {
@@ -559,12 +559,12 @@ public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePoll
559559
return
560560
}
561561

562-
guard let pingInterval = pingInterval else { return }
563-
564562
pongsMissed += 1
565563
write("", withType: .ping, withData: [])
566564

567-
engineQueue.asyncAfter(deadline: DispatchTime.now() + Double(pingInterval)) {[weak self] in self?.sendPing() }
565+
engineQueue.asyncAfter(deadline: DispatchTime.now() + .milliseconds(pingInterval)) {[weak self] in
566+
self?.sendPing()
567+
}
568568
}
569569

570570
// Moves from long-polling to websockets

0 commit comments

Comments
 (0)