From 16fd0436328003a73d266c7cdc6fd313cbd1e10a Mon Sep 17 00:00:00 2001 From: Erik Date: Tue, 3 Mar 2015 22:39:37 -0500 Subject: [PATCH] fix reconnect --- SwiftIO/SocketEngine.swift | 11 +++++++---- SwiftIO/SocketIOClient.swift | 6 +----- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/SwiftIO/SocketEngine.swift b/SwiftIO/SocketEngine.swift index 58bdc3d..a3c3a90 100644 --- a/SwiftIO/SocketEngine.swift +++ b/SwiftIO/SocketEngine.swift @@ -103,18 +103,21 @@ class SocketEngine: NSObject, SRWebSocketDelegate { NSURLConnection.sendAsynchronousRequest(reqPolling, queue: self.pollingQueue) {[weak self] res, data, err in var err:NSError? - - if self == nil || err != nil || data == nil { + if self == nil { + return + } else if err != nil || data == nil { println("Error") println(err) - exit(1) + if !self!.client.reconnecting { + self?.client.tryReconnect(triesLeft: self!.client.reconnectAttempts) + } + return } let sub = data.subdataWithRange(NSMakeRange(5, data.length - 5)) if let json = NSJSONSerialization.JSONObjectWithData(sub, options: NSJSONReadingOptions.AllowFragments, error: &err) as? NSDictionary { - println(json) if let sid = json["sid"] as? String { self?.sid = sid diff --git a/SwiftIO/SocketIOClient.swift b/SwiftIO/SocketIOClient.swift index 4f38b9e..ff3f622 100644 --- a/SwiftIO/SocketIOClient.swift +++ b/SwiftIO/SocketIOClient.swift @@ -750,7 +750,7 @@ class SocketIOClient: NSObject { } // We lost connection and should attempt to reestablish - private func tryReconnect(var #triesLeft:Int) { + func tryReconnect(var #triesLeft:Int) { if triesLeft != -1 && triesLeft <= 0 { self.connecting = false self.reconnects = false @@ -846,8 +846,4 @@ class SocketIOClient: NSObject { self.tryReconnect(triesLeft: self.reconnectAttempts) } } - - func testEngine() { - self.engine.open() - } } \ No newline at end of file