From 2fdd1313a8aec9ee14942cc613e0c170c4eb30b1 Mon Sep 17 00:00:00 2001 From: Erik Date: Fri, 22 May 2015 07:26:52 -0400 Subject: [PATCH] move open logic to method --- SocketIOClientSwift/SocketEngine.swift | 56 ++++++++++++++------------ 1 file changed, 30 insertions(+), 26 deletions(-) diff --git a/SocketIOClientSwift/SocketEngine.swift b/SocketIOClientSwift/SocketEngine.swift index 3bfb5c3..e2bd320 100644 --- a/SocketIOClientSwift/SocketEngine.swift +++ b/SocketIOClientSwift/SocketEngine.swift @@ -356,6 +356,35 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient { postWait.removeAll(keepCapacity: true) } + private func handleOpen(openData:String) { + var err:NSError? + let mesData = openData.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)! + + if let json = NSJSONSerialization.JSONObjectWithData(mesData, + options: NSJSONReadingOptions.AllowFragments, + error: &err) as? NSDictionary, let sid = json["sid"] as? String { + self.sid = sid + _connected = true + + if !forcePolling && !forceWebsockets { + createWebsocket(andConnect: true) + } + + if let pingInterval = json["pingInterval"] as? Int { + self.pingInterval = pingInterval / 1000 + } + } else { + client?.didError("Engine failed to handshake") + return + } + + startPingTimer() + + if !forceWebsockets { + doPoll() + } + } + // A poll failed, tell the client about it private func handlePollingFailed(reason:String) { _connected = false @@ -498,34 +527,9 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient { upgradeTransport() } } else if type == PacketType.OPEN { - var err:NSError? - message.removeAtIndex(message.startIndex) - let mesData = message.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)! - if let json = NSJSONSerialization.JSONObjectWithData(mesData, - options: NSJSONReadingOptions.AllowFragments, - error: &err) as? NSDictionary, let sid = json["sid"] as? String { - self.sid = sid - _connected = true - - if !forcePolling && !forceWebsockets { - createWebsocket(andConnect: true) - } - - if let pingInterval = json["pingInterval"] as? Int { - self.pingInterval = pingInterval / 1000 - } - } else { - client?.didError("Engine failed to handshake") - return - } - - startPingTimer() - - if !forceWebsockets { - doPoll() - } + handleOpen(message) } else if type == PacketType.CLOSE { if polling { client?.engineDidClose("Disconnect")