From 4de7a48e8768a1a0ed9351e1bb8429f925d23068 Mon Sep 17 00:00:00 2001 From: Erik Date: Sat, 27 Aug 2016 11:37:13 -0400 Subject: [PATCH 1/2] Don't send whole packet to handleConnect --- Source/SocketParsable.swift | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Source/SocketParsable.swift b/Source/SocketParsable.swift index d5a1b9db..b12ba30f 100644 --- a/Source/SocketParsable.swift +++ b/Source/SocketParsable.swift @@ -32,8 +32,8 @@ extension SocketParsable { return nsp == self.nsp } - private func handleConnect(p: SocketPacket) { - if p.nsp == "/" && nsp != "/" { + private func handleConnect(packetNamespace: String) { + if packetNamespace == "/" && nsp != "/" { joinNamespace(nsp) } else { didConnect() @@ -51,7 +51,7 @@ extension SocketParsable { case .BinaryAck where isCorrectNamespace(pack.nsp): waitingPackets.append(pack) case .Connect: - handleConnect(pack) + handleConnect(pack.nsp) case .Disconnect: didDisconnect("Got Disconnect") case .Error: From bd7f94333d0e5f26387c72122fa9d7485c9ba4ef Mon Sep 17 00:00:00 2001 From: Erik Date: Sat, 27 Aug 2016 13:05:22 -0400 Subject: [PATCH 2/2] Refactor engine. Fix infinite recursion in configuration --- Source/SocketEngine.swift | 58 ++++++++++++------------ Source/SocketIOClientConfiguration.swift | 2 +- 2 files changed, 31 insertions(+), 29 deletions(-) diff --git a/Source/SocketEngine.swift b/Source/SocketEngine.swift index 9b3c5e14..cdc94ab0 100644 --- a/Source/SocketEngine.swift +++ b/Source/SocketEngine.swift @@ -359,36 +359,38 @@ public final class SocketEngine : NSObject, NSURLSessionDelegate, SocketEnginePo private func handleOpen(openData: String) { do { let json = try openData.toNSDictionary() + guard let sid = json[sid] as? String else { + client?.engineDidError("Open packet contained no sid") + return + } - if let sid = json["sid"] as? String { - let upgradeWs: Bool - - self.sid = sid - connected = true - - if let upgrades = json["upgrades"] as? [String] { - upgradeWs = upgrades.contains("websocket") - } else { - upgradeWs = false - } - - if let pingInterval = json["pingInterval"] as? Double, pingTimeout = json["pingTimeout"] as? Double { - self.pingInterval = pingInterval / 1000.0 - self.pingTimeout = pingTimeout / 1000.0 - } - - if !forcePolling && !forceWebsockets && upgradeWs { - createWebsocketAndConnect() - } - - sendPing() - - if !forceWebsockets { - doPoll() - } - - client?.engineDidOpen("Connect") + let upgradeWs: Bool + + self.sid = sid + connected = true + + if let upgrades = json["upgrades"] as? [String] { + upgradeWs = upgrades.contains("websocket") + } else { + upgradeWs = false + } + + if let pingInterval = json["pingInterval"] as? Double, pingTimeout = json["pingTimeout"] as? Double { + self.pingInterval = pingInterval / 1000.0 + self.pingTimeout = pingTimeout / 1000.0 } + + if !forcePolling && !forceWebsockets && upgradeWs { + createWebsocketAndConnect() + } + + sendPing() + + if !forceWebsockets { + doPoll() + } + + client?.engineDidOpen("Connect") } catch { didError("Error parsing open packet") } diff --git a/Source/SocketIOClientConfiguration.swift b/Source/SocketIOClientConfiguration.swift index c7439918..4823430d 100644 --- a/Source/SocketIOClientConfiguration.swift +++ b/Source/SocketIOClientConfiguration.swift @@ -97,7 +97,7 @@ public struct SocketIOClientConfiguration : ArrayLiteralConvertible, CollectionT @warn_unused_result public func prefixThrough(position: Index) -> SubSequence { - return prefixThrough(position) + return backingArray.prefixThrough(position) } @warn_unused_result