Skip to content

Commit e8dfe58

Browse files
committed
redo pingtimeouts
1 parent fcebc87 commit e8dfe58

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

SocketIOClientSwift/SocketEngine.swift

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,13 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
4040
private var fastUpgrade = false
4141
private var forcePolling = false
4242
private var forceWebsockets = false
43-
private var gotPong = true
43+
private var pingInterval:Int?
4444
private var pingTimer:NSTimer?
45+
private var pingTimeout = 0
46+
private var pongsMissed = 0
47+
private var pongsMissedMax:Int {
48+
return pingTimeout / (pingInterval ?? 25)
49+
}
4550
private var postWait = [String]()
4651
private var _polling = true
4752
private var probing = false
@@ -59,7 +64,6 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
5964
weak var client:SocketEngineClient?
6065
var cookies:[NSHTTPCookie]?
6166
var log = false
62-
var pingInterval:Int?
6367
var polling:Bool {
6468
return _polling
6569
}
@@ -357,16 +361,17 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
357361

358362
if let json = NSJSONSerialization.JSONObjectWithData(mesData,
359363
options: NSJSONReadingOptions.AllowFragments,
360-
error: &err) as? NSDictionary, let sid = json["sid"] as? String {
364+
error: &err) as? NSDictionary, sid = json["sid"] as? String {
361365
self.sid = sid
362366
_connected = true
363367

364368
if !forcePolling && !forceWebsockets {
365369
createWebsocket(andConnect: true)
366370
}
367371

368-
if let pingInterval = json["pingInterval"] as? Int {
372+
if let pingInterval = json["pingInterval"] as? Int, pingTimeout = json["pingTimeout"] as? Int {
369373
self.pingInterval = pingInterval / 1000
374+
self.pingTimeout = pingTimeout / 1000
370375
}
371376
} else {
372377
client?.didError("Engine failed to handshake")
@@ -517,7 +522,7 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
517522
} else if type == PacketType.NOOP {
518523
doPoll()
519524
} else if type == PacketType.PONG {
520-
gotPong = true
525+
pongsMissed = 0
521526

522527
// We should upgrade
523528
if message == "3probe" {
@@ -560,15 +565,15 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
560565
}
561566
}
562567

563-
func sendPing() {
568+
@objc private func sendPing() {
564569
//Server is not responding
565-
if !gotPong {
570+
if pongsMissed > pongsMissedMax {
566571
pingTimer?.invalidate()
567572
client?.engineDidClose("Ping timeout")
568573
return
569574
}
570575

571-
gotPong = false
576+
++pongsMissed
572577
write("", withType: PacketType.PING, withData: nil)
573578
}
574579

0 commit comments

Comments
 (0)