From 40ac53844d908e65cfb870ca27cfa2356c3034e1 Mon Sep 17 00:00:00 2001 From: Erik Date: Sat, 17 Oct 2015 14:49:41 -0400 Subject: [PATCH 01/16] mess around with a more type safe way to set options --- .../project.pbxproj | 10 +++ SocketIOClientSwift/SocketIOClient.swift | 28 ++++---- .../SocketIOClientOptions .swift | 64 +++++++++++++++++++ SocketIOClientSwift/SocketTypes.swift | 1 + 4 files changed, 92 insertions(+), 11 deletions(-) create mode 100644 SocketIOClientSwift/SocketIOClientOptions .swift diff --git a/Socket.IO-Client-Swift.xcodeproj/project.pbxproj b/Socket.IO-Client-Swift.xcodeproj/project.pbxproj index 41ade57..afb73e1 100644 --- a/Socket.IO-Client-Swift.xcodeproj/project.pbxproj +++ b/Socket.IO-Client-Swift.xcodeproj/project.pbxproj @@ -37,6 +37,10 @@ 5764DFA01B51F254004FF46E /* SwiftRegex.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5764DF871B51F254004FF46E /* SwiftRegex.swift */; }; 5764DFA11B51F254004FF46E /* WebSocket.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5764DF881B51F254004FF46E /* WebSocket.swift */; }; 5764DFA21B51F254004FF46E /* WebSocket.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5764DF881B51F254004FF46E /* WebSocket.swift */; }; + 740E10441BD2C4680064FC4A /* SocketIOClientOptions .swift in Sources */ = {isa = PBXBuildFile; fileRef = 740E10431BD2C4680064FC4A /* SocketIOClientOptions .swift */; settings = {ASSET_TAGS = (); }; }; + 740E10451BD2C4680064FC4A /* SocketIOClientOptions .swift in Sources */ = {isa = PBXBuildFile; fileRef = 740E10431BD2C4680064FC4A /* SocketIOClientOptions .swift */; settings = {ASSET_TAGS = (); }; }; + 740E10461BD2C4680064FC4A /* SocketIOClientOptions .swift in Sources */ = {isa = PBXBuildFile; fileRef = 740E10431BD2C4680064FC4A /* SocketIOClientOptions .swift */; settings = {ASSET_TAGS = (); }; }; + 740E10471BD2C4680064FC4A /* SocketIOClientOptions .swift in Sources */ = {isa = PBXBuildFile; fileRef = 740E10431BD2C4680064FC4A /* SocketIOClientOptions .swift */; settings = {ASSET_TAGS = (); }; }; 741F39EE1BD025D80026C9CC /* SocketEngineTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 741F39ED1BD025D80026C9CC /* SocketEngineTest.swift */; settings = {ASSET_TAGS = (); }; }; 741F39EF1BD025D80026C9CC /* SocketEngineTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 741F39ED1BD025D80026C9CC /* SocketEngineTest.swift */; settings = {ASSET_TAGS = (); }; }; 745895381BB59A0A0050ACC8 /* SocketAckManagerTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 94A20D601B99E22F00BF9E44 /* SocketAckManagerTest.swift */; }; @@ -138,6 +142,7 @@ 5764DF861B51F254004FF46E /* SocketTypes.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SocketTypes.swift; path = SocketIOClientSwift/SocketTypes.swift; sourceTree = ""; }; 5764DF871B51F254004FF46E /* SwiftRegex.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SwiftRegex.swift; path = SocketIOClientSwift/SwiftRegex.swift; sourceTree = ""; }; 5764DF881B51F254004FF46E /* WebSocket.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = WebSocket.swift; path = SocketIOClientSwift/WebSocket.swift; sourceTree = ""; }; + 740E10431BD2C4680064FC4A /* SocketIOClientOptions .swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "SocketIOClientOptions .swift"; path = "SocketIOClientSwift/SocketIOClientOptions .swift"; sourceTree = ""; }; 741F39ED1BD025D80026C9CC /* SocketEngineTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SocketEngineTest.swift; sourceTree = ""; }; 7472C65B1BCAB53E003CA70D /* SocketNamespacePacketTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SocketNamespacePacketTest.swift; sourceTree = ""; }; 7472C65E1BCAC46E003CA70D /* SocketSideEffectTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SocketSideEffectTest.swift; sourceTree = ""; }; @@ -293,6 +298,7 @@ 5764DF801B51F254004FF46E /* SocketEventHandler.swift */, 5764DF811B51F254004FF46E /* SocketFixUTF8.swift */, 5764DF821B51F254004FF46E /* SocketIOClient.swift */, + 740E10431BD2C4680064FC4A /* SocketIOClientOptions .swift */, 74781D591B7E83930042CACA /* SocketIOClientStatus.swift */, 5764DF831B51F254004FF46E /* SocketLogger.swift */, 5764DF841B51F254004FF46E /* SocketPacket.swift */, @@ -492,6 +498,7 @@ 74781D5A1B7E83930042CACA /* SocketIOClientStatus.swift in Sources */, 5764DFA11B51F254004FF46E /* WebSocket.swift in Sources */, 5764DF991B51F254004FF46E /* SocketPacket.swift in Sources */, + 740E10441BD2C4680064FC4A /* SocketIOClientOptions .swift in Sources */, 74F124E31BC5697B002966F4 /* SocketEngineSpec.swift in Sources */, 5764DF891B51F254004FF46E /* SocketAckManager.swift in Sources */, 74F124E81BC56BFC002966F4 /* SocketEnginePacketType.swift in Sources */, @@ -523,6 +530,7 @@ 74781D5B1B7E83930042CACA /* SocketIOClientStatus.swift in Sources */, 945B653B1B5FCEEA0081E995 /* SocketIOClient.swift in Sources */, 74F124F01BC574CF002966F4 /* SocketBasicPacketTest.swift in Sources */, + 740E10451BD2C4680064FC4A /* SocketIOClientOptions .swift in Sources */, 949FAE8D1B9B94E600073BE9 /* SocketParserTest.swift in Sources */, 7472C65C1BCAB53E003CA70D /* SocketNamespacePacketTest.swift in Sources */, 945B65411B5FCEEA0081E995 /* WebSocket.swift in Sources */, @@ -548,6 +556,7 @@ 74781D5C1B7E83930042CACA /* SocketIOClientStatus.swift in Sources */, 5764DFA21B51F254004FF46E /* WebSocket.swift in Sources */, 5764DF9A1B51F254004FF46E /* SocketPacket.swift in Sources */, + 740E10461BD2C4680064FC4A /* SocketIOClientOptions .swift in Sources */, 74F124E51BC5697B002966F4 /* SocketEngineSpec.swift in Sources */, 5764DF8A1B51F254004FF46E /* SocketAckManager.swift in Sources */, 74F124EA1BC56BFC002966F4 /* SocketEnginePacketType.swift in Sources */, @@ -579,6 +588,7 @@ 745895381BB59A0A0050ACC8 /* SocketAckManagerTest.swift in Sources */, 7458953D1BB59A0A0050ACC8 /* SocketParserTest.swift in Sources */, 74F124F11BC574CF002966F4 /* SocketBasicPacketTest.swift in Sources */, + 740E10471BD2C4680064FC4A /* SocketIOClientOptions .swift in Sources */, 74F124EB1BC56BFC002966F4 /* SocketEnginePacketType.swift in Sources */, 7472C65D1BCAB53E003CA70D /* SocketNamespacePacketTest.swift in Sources */, 749A7F901BA9D42D00782993 /* SocketAckEmitter.swift in Sources */, diff --git a/SocketIOClientSwift/SocketIOClient.swift b/SocketIOClientSwift/SocketIOClient.swift index dc00c5e..68b7115 100644 --- a/SocketIOClientSwift/SocketIOClient.swift +++ b/SocketIOClientSwift/SocketIOClient.swift @@ -32,7 +32,7 @@ public final class SocketIOClient: NSObject, SocketEngineClient { public private(set) var status = SocketIOClientStatus.NotConnected public var nsp = "/" - public var opts: [String: AnyObject]? + public var opts: SocketOptionsDictionary? public var reconnects = true public var reconnectWait = 10 public var sid: String? { @@ -57,7 +57,7 @@ public final class SocketIOClient: NSObject, SocketEngineClient { /** Create a new SocketIOClient. opts can be omitted */ - public init(var socketURL: String, opts: [String: AnyObject]? = nil) { + public init(var socketURL: String, opts: SocketOptionsDictionary? = nil) { if socketURL["https://"].matches().count != 0 { self.secure = true } @@ -68,37 +68,37 @@ public final class SocketIOClient: NSObject, SocketEngineClient { self.socketURL = socketURL self.opts = opts - if let connectParams = opts?["connectParams"] as? [String: AnyObject] { + if let connectParams = opts?[.ConnectParams] as? [String: AnyObject] { self.connectParams = connectParams } - if let logger = opts?["logger"] as? SocketLogger { + if let logger = opts?[.Logger] as? SocketLogger { Logger = logger } - if let log = opts?["log"] as? Bool { + if let log = opts?[.Log] as? Bool { Logger.log = log } - if let nsp = opts?["nsp"] as? String { + if let nsp = opts?[.Nsp] as? String { self.nsp = nsp } - if let reconnects = opts?["reconnects"] as? Bool { + if let reconnects = opts?[.Reconnects] as? Bool { self.reconnects = reconnects } - if let reconnectAttempts = opts?["reconnectAttempts"] as? Int { + if let reconnectAttempts = opts?[.ReconnectAttempts] as? Int { self.reconnectAttempts = reconnectAttempts } else { self.reconnectAttempts = -1 } - if let reconnectWait = opts?["reconnectWait"] as? Int { + if let reconnectWait = opts?[.ReconnectWait] as? Int { self.reconnectWait = abs(reconnectWait) } - if let handleQueue = opts?["handleQueue"] as? dispatch_queue_t { + if let handleQueue = opts?[.HandleQueue] as? dispatch_queue_t { self.handleQueue = handleQueue } else { self.handleQueue = dispatch_get_main_queue() @@ -107,6 +107,11 @@ public final class SocketIOClient: NSObject, SocketEngineClient { super.init() } + public convenience init(socketURL: String, opts: NSDictionary?) { + self.init(socketURL: socketURL, + opts: SocketIOClientOptions.NSDictionaryToSocketOptionsDictionary(opts ?? [:])) + } + deinit { Logger.log("Client is being deinit", type: logType) } @@ -114,7 +119,8 @@ public final class SocketIOClient: NSObject, SocketEngineClient { private func addEngine() -> SocketEngine { Logger.log("Adding engine", type: logType) - let newEngine = SocketEngine(client: self, opts: opts) + let newEngine = SocketEngine(client: self, opts: + SocketIOClientOptions.SocketOptionsDictionaryToNSDictionary(opts ?? [:])) engine = newEngine return newEngine diff --git a/SocketIOClientSwift/SocketIOClientOptions .swift b/SocketIOClientSwift/SocketIOClientOptions .swift new file mode 100644 index 0000000..7ff317f --- /dev/null +++ b/SocketIOClientSwift/SocketIOClientOptions .swift @@ -0,0 +1,64 @@ +// +// SocketIOClientOptions .swift +// Socket.IO-Client-Swift +// +// Created by Erik Little on 10/17/15. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +import Foundation + +public enum SocketIOClientOptions: String { + case ConnectParams = "connectParams" + case Reconnects = "reconnects" + case ReconnectAttempts = "reconnectAttempts" + case ReconnectWait = "reconnectWait" + case ForcePolling = "forcePolling" + case ForceWebsockets = "forceWebsockets" + case Nsp = "nsp" + case Cookies = "cookies" + case Log = "log" + case Logger = "logger" + case SessionDelegate = "sessionDelegate" + case Path = "path" + case ExtraHeaders = "extraHeaders" + case HandleQueue = "handleQueue" + + static func NSDictionaryToSocketOptionsDictionary(dict: NSDictionary) -> SocketOptionsDictionary { + var options = [SocketIOClientOptions: AnyObject]() + + for (rawKey, value) in dict { + if let key = rawKey as? String, opt = SocketIOClientOptions(rawValue: key) { + options[opt] = value + } + } + + return options + } + + static func SocketOptionsDictionaryToNSDictionary(dict: SocketOptionsDictionary) -> NSDictionary { + let options = NSMutableDictionary() + + for (key, value) in dict { + options[key.rawValue] = value + } + + return options + } +} \ No newline at end of file diff --git a/SocketIOClientSwift/SocketTypes.swift b/SocketIOClientSwift/SocketTypes.swift index ed091f9..2b50db4 100644 --- a/SocketIOClientSwift/SocketTypes.swift +++ b/SocketIOClientSwift/SocketTypes.swift @@ -27,6 +27,7 @@ import Foundation public typealias AckCallback = ([AnyObject]) -> Void public typealias NormalCallback = ([AnyObject], SocketAckEmitter?) -> Void public typealias OnAckCallback = (timeoutAfter: UInt64, callback: AckCallback) -> Void +public typealias SocketOptionsDictionary = [SocketIOClientOptions: AnyObject] enum Either { case Left(E) From 7382030dfff453e4b626a64710b537f8292f518c Mon Sep 17 00:00:00 2001 From: Erik Date: Sat, 17 Oct 2015 14:54:09 -0400 Subject: [PATCH 02/16] there's a typealias for a reason --- SocketIOClientSwift/SocketIOClientOptions .swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SocketIOClientSwift/SocketIOClientOptions .swift b/SocketIOClientSwift/SocketIOClientOptions .swift index 7ff317f..19c32e3 100644 --- a/SocketIOClientSwift/SocketIOClientOptions .swift +++ b/SocketIOClientSwift/SocketIOClientOptions .swift @@ -41,7 +41,7 @@ public enum SocketIOClientOptions: String { case HandleQueue = "handleQueue" static func NSDictionaryToSocketOptionsDictionary(dict: NSDictionary) -> SocketOptionsDictionary { - var options = [SocketIOClientOptions: AnyObject]() + var options = SocketOptionsDictionary() for (rawKey, value) in dict { if let key = rawKey as? String, opt = SocketIOClientOptions(rawValue: key) { From a5db43b8348b397c34308d856dde2bfca4faebb8 Mon Sep 17 00:00:00 2001 From: Erik Date: Sat, 17 Oct 2015 14:58:47 -0400 Subject: [PATCH 03/16] add comments --- SocketIOClientSwift/SocketIOClient.swift | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/SocketIOClientSwift/SocketIOClient.swift b/SocketIOClientSwift/SocketIOClient.swift index 68b7115..8f68dad 100644 --- a/SocketIOClientSwift/SocketIOClient.swift +++ b/SocketIOClientSwift/SocketIOClient.swift @@ -55,7 +55,7 @@ public final class SocketIOClient: NSObject, SocketEngineClient { var waitingData = [SocketPacket]() /** - Create a new SocketIOClient. opts can be omitted + Type safe way to create a new SocketIOClient. opts can be omitted */ public init(var socketURL: String, opts: SocketOptionsDictionary? = nil) { if socketURL["https://"].matches().count != 0 { @@ -107,6 +107,10 @@ public final class SocketIOClient: NSObject, SocketEngineClient { super.init() } + /** + Not so type safe way to create a SocketIOClient, meant for Objective-C compatiblity. + If using Swift it's recommended to use `init(var socketURL: String, opts: SocketOptionsDictionary? = nil)` + */ public convenience init(socketURL: String, opts: NSDictionary?) { self.init(socketURL: socketURL, opts: SocketIOClientOptions.NSDictionaryToSocketOptionsDictionary(opts ?? [:])) From b9e05286a2d4599f96eb8523e63584ca92267e97 Mon Sep 17 00:00:00 2001 From: Erik Date: Sun, 18 Oct 2015 14:52:46 -0400 Subject: [PATCH 04/16] even more type safe solution, idea from @lightsprint09 --- SocketIOClientSwift/SocketIOClient.swift | 67 ++++----- .../SocketIOClientOptions .swift | 132 ++++++++++++++---- SocketIOClientSwift/SocketLogger.swift | 4 +- SocketIOClientSwift/SocketTypes.swift | 2 +- 4 files changed, 138 insertions(+), 67 deletions(-) diff --git a/SocketIOClientSwift/SocketIOClient.swift b/SocketIOClientSwift/SocketIOClient.swift index 8f68dad..ea0b562 100644 --- a/SocketIOClientSwift/SocketIOClient.swift +++ b/SocketIOClientSwift/SocketIOClient.swift @@ -32,7 +32,7 @@ public final class SocketIOClient: NSObject, SocketEngineClient { public private(set) var status = SocketIOClientStatus.NotConnected public var nsp = "/" - public var opts: SocketOptionsDictionary? + public var opts: SocketOptionsSet? public var reconnects = true public var reconnectWait = 10 public var sid: String? { @@ -40,9 +40,7 @@ public final class SocketIOClient: NSObject, SocketEngineClient { } private let emitQueue = dispatch_queue_create("com.socketio.emitQueue", DISPATCH_QUEUE_SERIAL) - private let handleQueue: dispatch_queue_t! private let logType = "SocketIOClient" - private let reconnectAttempts: Int! private var anyHandler: ((SocketAnyEvent) -> Void)? private var currentReconnectAttempt = 0 @@ -52,12 +50,14 @@ public final class SocketIOClient: NSObject, SocketEngineClient { private var ackHandlers = SocketAckManager() private(set) var currentAck = -1 + private(set) var handleQueue = dispatch_get_main_queue() + private(set) var reconnectAttempts = -1 var waitingData = [SocketPacket]() /** Type safe way to create a new SocketIOClient. opts can be omitted */ - public init(var socketURL: String, opts: SocketOptionsDictionary? = nil) { + public init(var socketURL: String, opts: SocketOptionsSet? = nil) { if socketURL["https://"].matches().count != 0 { self.secure = true } @@ -68,40 +68,27 @@ public final class SocketIOClient: NSObject, SocketEngineClient { self.socketURL = socketURL self.opts = opts - if let connectParams = opts?[.ConnectParams] as? [String: AnyObject] { - self.connectParams = connectParams - } - - if let logger = opts?[.Logger] as? SocketLogger { - Logger = logger - } - - if let log = opts?[.Log] as? Bool { - Logger.log = log - } - - if let nsp = opts?[.Nsp] as? String { - self.nsp = nsp - } - - if let reconnects = opts?[.Reconnects] as? Bool { - self.reconnects = reconnects - } - - if let reconnectAttempts = opts?[.ReconnectAttempts] as? Int { - self.reconnectAttempts = reconnectAttempts - } else { - self.reconnectAttempts = -1 - } - - if let reconnectWait = opts?[.ReconnectWait] as? Int { - self.reconnectWait = abs(reconnectWait) - } - - if let handleQueue = opts?[.HandleQueue] as? dispatch_queue_t { - self.handleQueue = handleQueue - } else { - self.handleQueue = dispatch_get_main_queue() + for option in opts ?? [] { + switch option { + case .ConnectParams(let params): + connectParams = params + case .Reconnects(let reconnects): + self.reconnects = reconnects + case .ReconnectAttempts(let attempts): + reconnectAttempts = attempts + case .ReconnectWait(let wait): + reconnectWait = wait + case .Nsp(let nsp): + self.nsp = nsp + case .Log(let log): + Logger.log = log + case .Logger(let logger): + Logger = logger + case .HandleQueue(let queue): + handleQueue = queue + default: + continue + } } super.init() @@ -113,7 +100,7 @@ public final class SocketIOClient: NSObject, SocketEngineClient { */ public convenience init(socketURL: String, opts: NSDictionary?) { self.init(socketURL: socketURL, - opts: SocketIOClientOptions.NSDictionaryToSocketOptionsDictionary(opts ?? [:])) + opts: SocketIOClientOption.NSDictionaryToSocketOptionsSet(opts ?? [:])) } deinit { @@ -124,7 +111,7 @@ public final class SocketIOClient: NSObject, SocketEngineClient { Logger.log("Adding engine", type: logType) let newEngine = SocketEngine(client: self, opts: - SocketIOClientOptions.SocketOptionsDictionaryToNSDictionary(opts ?? [:])) + SocketIOClientOption.SocketOptionsSetToNSDictionary(opts ?? [])) engine = newEngine return newEngine diff --git a/SocketIOClientSwift/SocketIOClientOptions .swift b/SocketIOClientSwift/SocketIOClientOptions .swift index 19c32e3..36739e1 100644 --- a/SocketIOClientSwift/SocketIOClientOptions .swift +++ b/SocketIOClientSwift/SocketIOClientOptions .swift @@ -1,5 +1,5 @@ // -// SocketIOClientOptions .swift +// SocketIOClientOption .swift // Socket.IO-Client-Swift // // Created by Erik Little on 10/17/15. @@ -24,41 +24,125 @@ import Foundation -public enum SocketIOClientOptions: String { - case ConnectParams = "connectParams" - case Reconnects = "reconnects" - case ReconnectAttempts = "reconnectAttempts" - case ReconnectWait = "reconnectWait" - case ForcePolling = "forcePolling" - case ForceWebsockets = "forceWebsockets" - case Nsp = "nsp" - case Cookies = "cookies" - case Log = "log" - case Logger = "logger" - case SessionDelegate = "sessionDelegate" - case Path = "path" - case ExtraHeaders = "extraHeaders" - case HandleQueue = "handleQueue" +public enum SocketIOClientOption: CustomStringConvertible, Hashable { + case ConnectParams([String: AnyObject]) + case Reconnects(Bool) + case ReconnectAttempts(Int) + case ReconnectWait(Int) + case ForcePolling(Bool) + case ForceWebsockets(Bool) + case Nsp(String) + case Cookies([NSHTTPCookie]) + case Log(Bool) + case Logger(SocketLogger) + case SessionDelegate(NSURLSessionDelegate) + case Path(String) + case ExtraHeaders([String: String]) + case HandleQueue(dispatch_queue_t) - static func NSDictionaryToSocketOptionsDictionary(dict: NSDictionary) -> SocketOptionsDictionary { - var options = SocketOptionsDictionary() + public var description: String { + switch self { + case .ConnectParams: + return "connectParams" + case .Reconnects: + return "reconnects" + case .ReconnectAttempts: + return "reconnectAttempts" + case .ReconnectWait: + return "reconnectWait" + case .ForcePolling: + return "forcePolling" + case .ForceWebsockets: + return "forceWebsockets" + case .Nsp: + return "nsp" + case .Cookies: + return "cookies" + case .Log: + return "log" + case .Logger: + return "logger" + case .SessionDelegate: + return "sessionDelegate" + case .Path: + return "path" + case .ExtraHeaders: + return "extraHeaders" + case .HandleQueue: + return "handleQueue" + } + } + + public var hashValue: Int { + return description.hashValue + } + + static func keyValueToSocketIOClientOption(key: String, value: AnyObject) -> SocketIOClientOption? { + switch key { + case "connectParams" where value is [String: AnyObject]: + return .ConnectParams(value as! [String: AnyObject]) + case "reconnects" where value is Bool: + return .Reconnects(value as! Bool) + case "reconnectAttempts" where value is Int: + return .ReconnectAttempts(value as! Int) + case "reconnectWait" where value is Int: + return .ReconnectWait(value as! Int) + case "forcePolling" where value is Bool: + return .ForcePolling(value as! Bool) + case "forceWebsockets" where value is Bool: + return .ForceWebsockets(value as! Bool) + case "nsp" where value is String: + return .Nsp(value as! String) + case "cookies" where value is [NSHTTPCookie]: + return .Cookies(value as! [NSHTTPCookie]) + case "log" where value is Bool: + return .Log(value as! Bool) + case "logger" where value is SocketLogger: + return .Logger(value as! SocketLogger) + case "sessionDelegate" where value is NSURLSessionDelegate: + return .SessionDelegate(value as! NSURLSessionDelegate) + case "path" where value is String: + return .Path(value as! String) + case "extraHeaders" where value is [String: String]: + return .ExtraHeaders(value as! [String: String]) + case "handleQueue" where value is dispatch_queue_t: + return .HandleQueue(value as! dispatch_queue_t) + default: + return nil + } + } + + static func getSocketIOOptionValue(option: SocketIOClientOption) -> AnyObject? { + if let value = Mirror(reflecting: option).children.first!.value as? AnyObject { + return value + } else { + return nil + } + } + + static func NSDictionaryToSocketOptionsSet(dict: NSDictionary) -> SocketOptionsSet { + var options = SocketOptionsSet() for (rawKey, value) in dict { - if let key = rawKey as? String, opt = SocketIOClientOptions(rawValue: key) { - options[opt] = value + if let key = rawKey as? String, opt = keyValueToSocketIOClientOption(key, value: value) { + options.insert(opt) } } return options } - static func SocketOptionsDictionaryToNSDictionary(dict: SocketOptionsDictionary) -> NSDictionary { + static func SocketOptionsSetToNSDictionary(set: SocketOptionsSet) -> NSDictionary { let options = NSMutableDictionary() - for (key, value) in dict { - options[key.rawValue] = value + for option in set { + options[option.description] = getSocketIOOptionValue(option) } return options } -} \ No newline at end of file +} + +public func ==(lhs: SocketIOClientOption, rhs: SocketIOClientOption) -> Bool { + return lhs.description == rhs.description +} diff --git a/SocketIOClientSwift/SocketLogger.swift b/SocketIOClientSwift/SocketLogger.swift index 6c62aa9..b168820 100644 --- a/SocketIOClientSwift/SocketLogger.swift +++ b/SocketIOClientSwift/SocketLogger.swift @@ -26,7 +26,7 @@ import Foundation var Logger: SocketLogger = DefaultSocketLogger() -public protocol SocketLogger { +public protocol SocketLogger: class { /// Whether to log or not var log: Bool {get set} @@ -56,6 +56,6 @@ public extension SocketLogger { } } -struct DefaultSocketLogger: SocketLogger { +class DefaultSocketLogger: SocketLogger { var log = false } diff --git a/SocketIOClientSwift/SocketTypes.swift b/SocketIOClientSwift/SocketTypes.swift index 2b50db4..d992c75 100644 --- a/SocketIOClientSwift/SocketTypes.swift +++ b/SocketIOClientSwift/SocketTypes.swift @@ -27,7 +27,7 @@ import Foundation public typealias AckCallback = ([AnyObject]) -> Void public typealias NormalCallback = ([AnyObject], SocketAckEmitter?) -> Void public typealias OnAckCallback = (timeoutAfter: UInt64, callback: AckCallback) -> Void -public typealias SocketOptionsDictionary = [SocketIOClientOptions: AnyObject] +public typealias SocketOptionsSet = Set enum Either { case Left(E) From 7bc8cd01e8d0d75962e55b882daadd5c6f2dfbbd Mon Sep 17 00:00:00 2001 From: Erik Date: Sun, 18 Oct 2015 14:58:52 -0400 Subject: [PATCH 05/16] remove unsafe --- SocketIOClientSwift/SocketIOClientOptions .swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SocketIOClientSwift/SocketIOClientOptions .swift b/SocketIOClientSwift/SocketIOClientOptions .swift index 36739e1..a7358d2 100644 --- a/SocketIOClientSwift/SocketIOClientOptions .swift +++ b/SocketIOClientSwift/SocketIOClientOptions .swift @@ -113,7 +113,7 @@ public enum SocketIOClientOption: CustomStringConvertible, Hashable { } static func getSocketIOOptionValue(option: SocketIOClientOption) -> AnyObject? { - if let value = Mirror(reflecting: option).children.first!.value as? AnyObject { + if let value = Mirror(reflecting: option).children.first?.value as? AnyObject { return value } else { return nil From ac228ccede3c648527447d064e4cf63bb3e69e74 Mon Sep 17 00:00:00 2001 From: Erik Date: Sun, 18 Oct 2015 15:16:44 -0400 Subject: [PATCH 06/16] update readme --- README.md | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 3d69a8a..98daebe 100644 --- a/README.md +++ b/README.md @@ -120,25 +120,29 @@ Run `seed install`. ##API Constructors ----------- -`init(socketURL: String, opts: NSDictionary? = nil)` - Constructs a new client for the given URL. opts can be omitted (will use default values) note: If your socket.io server is secure, you need to specify `https` in your socketURL. +`init(var socketURL: String, opts: SocketOptionsSet? = nil)` - Creates a new SocketIOClient. opts is a Set of SocketIOClientOption. If your socket.io server is secure, you need to specify `https` in your socketURL. +`convenience init(socketURL: String, opts: NSDictionary?)` - Same as above, but meant for Objective-C. See Options on how convert between SocketIOClientOptions and dictionary keys. Options ------- -- `connectParams: [String: AnyObject]?` - Dictionary whose contents will be passed with the connection. -- `reconnects: Bool` Default is `true` -- `reconnectAttempts: Int` Default is `-1` (infinite tries) -- `reconnectWait: Int` Default is `10` -- `forcePolling: Bool` Default is `false`. `true` forces the client to use xhr-polling. -- `forceWebsockets: Bool` Default is `false`. `true` forces the client to use WebSockets. -- `nsp: String` Default is `"/"`. Connects to a namespace. -- `cookies: [NSHTTPCookie]?` An array of NSHTTPCookies. Passed during the handshake. Default is nil. -- `log: Bool` If `true` socket will log debug messages. Default is false. -- `logger: SocketLogger` If you wish to implement your own logger that conforms to SocketLogger you can pass it in here. Will use the default logging defined under the protocol otherwise. -- `sessionDelegate: NSURLSessionDelegate` Sets an NSURLSessionDelegate for the underlying engine. Useful if you need to handle self-signed certs. Default is nil. -- `path: String` - If the server uses a custom path. ex: `"/swift"`. Default is `""` -- `extraHeaders: [String: String]?` - Adds custom headers to the initial request. Default is nil. -- `handleQueue: dispatch_queue_t` - The dispatch queue that handlers are run on. Default is the main queue. +All options are a case of SocketIOClientOption. To get the Objective-C Option, convert the name to lowerCamelCase. +```swift +case ConnectParams([String: AnyObject]) // Dictionary whose contents will be passed with the connection. +case Reconnects(Bool) // Whether to reconnect on server lose. Default is `true` +case ReconnectAttempts(Int) // How many times to reconnect. Default is `-1` (infinite tries) +case ReconnectWait(Int) // Amount of time to wait between reconnects. Default is `10` +case ForcePolling(Bool) // `true` forces the client to use xhr-polling. Default is `false` +case ForceWebsockets(Bool) // `true` forces the client to use WebSockets. Default is `false` +case Nsp(String) // The namespace to connect to. Must begin with /. Default is `/` +case Cookies([NSHTTPCookie]) // An array of NSHTTPCookies. Passed during the handshake. Default is nil. +case Log(Bool) // If `true` socket will log debug messages. Default is false. +case Logger(SocketLogger) // Custom logger that conforms to SocketLogger. Will use the default logging otherwise. +case SessionDelegate(NSURLSessionDelegate) // Sets an NSURLSessionDelegate for the underlying engine. Useful if you need to handle self-signed certs. Default is nil. +case Path(String) // If the server uses a custom path. ex: `"/swift"`. Default is `""` +case ExtraHeaders([String: String]) // Adds custom headers to the initial request. Default is nil. +case HandleQueue(dispatch_queue_t) // The dispatch queue that handlers are run on. Default is the main queue. +``` Methods ------- 1. `on(event: String, callback: NormalCallback)` - Adds a handler for an event. Items are passed by an array. `ack` can be used to send an ack when one is requested. See example. From 885965c5bc708c0476d3458042eddaa2f605149a Mon Sep 17 00:00:00 2001 From: Erik Date: Sun, 18 Oct 2015 15:18:25 -0400 Subject: [PATCH 07/16] update examples --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 98daebe..60ba181 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ Socket.IO-client for iOS/OS X. ##Example ```swift -let socket = SocketIOClient(socketURL: "localhost:8080") +let socket = SocketIOClient(socketURL: "localhost:8080", opts: [.Log(true), .ForcePolling(true)]) socket.on("connect") {data, ack in print("socket connected") @@ -26,7 +26,7 @@ socket.connect() ##Objective-C Example ```objective-c -SocketIOClient* socket = [[SocketIOClient alloc] initWithSocketURL:@"localhost:8080" opts:nil]; +SocketIOClient* socket = [[SocketIOClient alloc] initWithSocketURL:@"localhost:8080" opts:@{@"log": @YES, @"forcePolling": @YES}]; [socket on:@"connect" callback:^(NSArray* data, SocketAckEmitter* ack) { NSLog(@"socket connected"); From 6c6084705e306523e4cdb264f75a1a5a93bfedf5 Mon Sep 17 00:00:00 2001 From: Erik Date: Sun, 18 Oct 2015 15:36:06 -0400 Subject: [PATCH 08/16] rename file --- .../project.pbxproj | 20 +++++++++---------- ...ions .swift => SocketIOClientOption.swift} | 0 SocketIOClientSwift/SocketTypes.swift | 2 +- 3 files changed, 11 insertions(+), 11 deletions(-) rename SocketIOClientSwift/{SocketIOClientOptions .swift => SocketIOClientOption.swift} (100%) diff --git a/Socket.IO-Client-Swift.xcodeproj/project.pbxproj b/Socket.IO-Client-Swift.xcodeproj/project.pbxproj index afb73e1..44c4fa9 100644 --- a/Socket.IO-Client-Swift.xcodeproj/project.pbxproj +++ b/Socket.IO-Client-Swift.xcodeproj/project.pbxproj @@ -37,10 +37,10 @@ 5764DFA01B51F254004FF46E /* SwiftRegex.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5764DF871B51F254004FF46E /* SwiftRegex.swift */; }; 5764DFA11B51F254004FF46E /* WebSocket.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5764DF881B51F254004FF46E /* WebSocket.swift */; }; 5764DFA21B51F254004FF46E /* WebSocket.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5764DF881B51F254004FF46E /* WebSocket.swift */; }; - 740E10441BD2C4680064FC4A /* SocketIOClientOptions .swift in Sources */ = {isa = PBXBuildFile; fileRef = 740E10431BD2C4680064FC4A /* SocketIOClientOptions .swift */; settings = {ASSET_TAGS = (); }; }; - 740E10451BD2C4680064FC4A /* SocketIOClientOptions .swift in Sources */ = {isa = PBXBuildFile; fileRef = 740E10431BD2C4680064FC4A /* SocketIOClientOptions .swift */; settings = {ASSET_TAGS = (); }; }; - 740E10461BD2C4680064FC4A /* SocketIOClientOptions .swift in Sources */ = {isa = PBXBuildFile; fileRef = 740E10431BD2C4680064FC4A /* SocketIOClientOptions .swift */; settings = {ASSET_TAGS = (); }; }; - 740E10471BD2C4680064FC4A /* SocketIOClientOptions .swift in Sources */ = {isa = PBXBuildFile; fileRef = 740E10431BD2C4680064FC4A /* SocketIOClientOptions .swift */; settings = {ASSET_TAGS = (); }; }; + 740E10441BD2C4680064FC4A /* SocketIOClientOption.swift in Sources */ = {isa = PBXBuildFile; fileRef = 740E10431BD2C4680064FC4A /* SocketIOClientOption.swift */; settings = {ASSET_TAGS = (); }; }; + 740E10451BD2C4680064FC4A /* SocketIOClientOption.swift in Sources */ = {isa = PBXBuildFile; fileRef = 740E10431BD2C4680064FC4A /* SocketIOClientOption.swift */; settings = {ASSET_TAGS = (); }; }; + 740E10461BD2C4680064FC4A /* SocketIOClientOption.swift in Sources */ = {isa = PBXBuildFile; fileRef = 740E10431BD2C4680064FC4A /* SocketIOClientOption.swift */; settings = {ASSET_TAGS = (); }; }; + 740E10471BD2C4680064FC4A /* SocketIOClientOption.swift in Sources */ = {isa = PBXBuildFile; fileRef = 740E10431BD2C4680064FC4A /* SocketIOClientOption.swift */; settings = {ASSET_TAGS = (); }; }; 741F39EE1BD025D80026C9CC /* SocketEngineTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 741F39ED1BD025D80026C9CC /* SocketEngineTest.swift */; settings = {ASSET_TAGS = (); }; }; 741F39EF1BD025D80026C9CC /* SocketEngineTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 741F39ED1BD025D80026C9CC /* SocketEngineTest.swift */; settings = {ASSET_TAGS = (); }; }; 745895381BB59A0A0050ACC8 /* SocketAckManagerTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 94A20D601B99E22F00BF9E44 /* SocketAckManagerTest.swift */; }; @@ -142,7 +142,7 @@ 5764DF861B51F254004FF46E /* SocketTypes.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SocketTypes.swift; path = SocketIOClientSwift/SocketTypes.swift; sourceTree = ""; }; 5764DF871B51F254004FF46E /* SwiftRegex.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SwiftRegex.swift; path = SocketIOClientSwift/SwiftRegex.swift; sourceTree = ""; }; 5764DF881B51F254004FF46E /* WebSocket.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = WebSocket.swift; path = SocketIOClientSwift/WebSocket.swift; sourceTree = ""; }; - 740E10431BD2C4680064FC4A /* SocketIOClientOptions .swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "SocketIOClientOptions .swift"; path = "SocketIOClientSwift/SocketIOClientOptions .swift"; sourceTree = ""; }; + 740E10431BD2C4680064FC4A /* SocketIOClientOption.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SocketIOClientOption.swift; path = SocketIOClientSwift/SocketIOClientOption.swift; sourceTree = ""; }; 741F39ED1BD025D80026C9CC /* SocketEngineTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SocketEngineTest.swift; sourceTree = ""; }; 7472C65B1BCAB53E003CA70D /* SocketNamespacePacketTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SocketNamespacePacketTest.swift; sourceTree = ""; }; 7472C65E1BCAC46E003CA70D /* SocketSideEffectTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SocketSideEffectTest.swift; sourceTree = ""; }; @@ -298,7 +298,7 @@ 5764DF801B51F254004FF46E /* SocketEventHandler.swift */, 5764DF811B51F254004FF46E /* SocketFixUTF8.swift */, 5764DF821B51F254004FF46E /* SocketIOClient.swift */, - 740E10431BD2C4680064FC4A /* SocketIOClientOptions .swift */, + 740E10431BD2C4680064FC4A /* SocketIOClientOption.swift */, 74781D591B7E83930042CACA /* SocketIOClientStatus.swift */, 5764DF831B51F254004FF46E /* SocketLogger.swift */, 5764DF841B51F254004FF46E /* SocketPacket.swift */, @@ -498,7 +498,7 @@ 74781D5A1B7E83930042CACA /* SocketIOClientStatus.swift in Sources */, 5764DFA11B51F254004FF46E /* WebSocket.swift in Sources */, 5764DF991B51F254004FF46E /* SocketPacket.swift in Sources */, - 740E10441BD2C4680064FC4A /* SocketIOClientOptions .swift in Sources */, + 740E10441BD2C4680064FC4A /* SocketIOClientOption.swift in Sources */, 74F124E31BC5697B002966F4 /* SocketEngineSpec.swift in Sources */, 5764DF891B51F254004FF46E /* SocketAckManager.swift in Sources */, 74F124E81BC56BFC002966F4 /* SocketEnginePacketType.swift in Sources */, @@ -530,7 +530,7 @@ 74781D5B1B7E83930042CACA /* SocketIOClientStatus.swift in Sources */, 945B653B1B5FCEEA0081E995 /* SocketIOClient.swift in Sources */, 74F124F01BC574CF002966F4 /* SocketBasicPacketTest.swift in Sources */, - 740E10451BD2C4680064FC4A /* SocketIOClientOptions .swift in Sources */, + 740E10451BD2C4680064FC4A /* SocketIOClientOption.swift in Sources */, 949FAE8D1B9B94E600073BE9 /* SocketParserTest.swift in Sources */, 7472C65C1BCAB53E003CA70D /* SocketNamespacePacketTest.swift in Sources */, 945B65411B5FCEEA0081E995 /* WebSocket.swift in Sources */, @@ -556,7 +556,7 @@ 74781D5C1B7E83930042CACA /* SocketIOClientStatus.swift in Sources */, 5764DFA21B51F254004FF46E /* WebSocket.swift in Sources */, 5764DF9A1B51F254004FF46E /* SocketPacket.swift in Sources */, - 740E10461BD2C4680064FC4A /* SocketIOClientOptions .swift in Sources */, + 740E10461BD2C4680064FC4A /* SocketIOClientOption.swift in Sources */, 74F124E51BC5697B002966F4 /* SocketEngineSpec.swift in Sources */, 5764DF8A1B51F254004FF46E /* SocketAckManager.swift in Sources */, 74F124EA1BC56BFC002966F4 /* SocketEnginePacketType.swift in Sources */, @@ -588,7 +588,7 @@ 745895381BB59A0A0050ACC8 /* SocketAckManagerTest.swift in Sources */, 7458953D1BB59A0A0050ACC8 /* SocketParserTest.swift in Sources */, 74F124F11BC574CF002966F4 /* SocketBasicPacketTest.swift in Sources */, - 740E10471BD2C4680064FC4A /* SocketIOClientOptions .swift in Sources */, + 740E10471BD2C4680064FC4A /* SocketIOClientOption.swift in Sources */, 74F124EB1BC56BFC002966F4 /* SocketEnginePacketType.swift in Sources */, 7472C65D1BCAB53E003CA70D /* SocketNamespacePacketTest.swift in Sources */, 749A7F901BA9D42D00782993 /* SocketAckEmitter.swift in Sources */, diff --git a/SocketIOClientSwift/SocketIOClientOptions .swift b/SocketIOClientSwift/SocketIOClientOption.swift similarity index 100% rename from SocketIOClientSwift/SocketIOClientOptions .swift rename to SocketIOClientSwift/SocketIOClientOption.swift diff --git a/SocketIOClientSwift/SocketTypes.swift b/SocketIOClientSwift/SocketTypes.swift index d992c75..aa07698 100644 --- a/SocketIOClientSwift/SocketTypes.swift +++ b/SocketIOClientSwift/SocketTypes.swift @@ -32,4 +32,4 @@ public typealias SocketOptionsSet = Set enum Either { case Left(E) case Right(V) -} \ No newline at end of file +} From c608bf8286877eb93c0bfe78c9ea8fce1e4fee2c Mon Sep 17 00:00:00 2001 From: Erik Date: Sun, 18 Oct 2015 15:48:31 -0400 Subject: [PATCH 09/16] Do some black magic to turn case into lower camel case --- .../SocketIOClientOption.swift | 33 +++---------------- 1 file changed, 4 insertions(+), 29 deletions(-) diff --git a/SocketIOClientSwift/SocketIOClientOption.swift b/SocketIOClientSwift/SocketIOClientOption.swift index a7358d2..a704c32 100644 --- a/SocketIOClientSwift/SocketIOClientOption.swift +++ b/SocketIOClientSwift/SocketIOClientOption.swift @@ -41,35 +41,10 @@ public enum SocketIOClientOption: CustomStringConvertible, Hashable { case HandleQueue(dispatch_queue_t) public var description: String { - switch self { - case .ConnectParams: - return "connectParams" - case .Reconnects: - return "reconnects" - case .ReconnectAttempts: - return "reconnectAttempts" - case .ReconnectWait: - return "reconnectWait" - case .ForcePolling: - return "forcePolling" - case .ForceWebsockets: - return "forceWebsockets" - case .Nsp: - return "nsp" - case .Cookies: - return "cookies" - case .Log: - return "log" - case .Logger: - return "logger" - case .SessionDelegate: - return "sessionDelegate" - case .Path: - return "path" - case .ExtraHeaders: - return "extraHeaders" - case .HandleQueue: - return "handleQueue" + if let label = Mirror(reflecting: self).children.first?.label { + return String(label[label.startIndex]).lowercaseString + String(label.characters.dropFirst()) + } else { + return "" } } From 45b5852e50993066978b412de7e831d9415563b3 Mon Sep 17 00:00:00 2001 From: Erik Date: Sun, 18 Oct 2015 16:31:46 -0400 Subject: [PATCH 10/16] refactor --- SocketIOClientSwift/SocketIOClientOption.swift | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/SocketIOClientSwift/SocketIOClientOption.swift b/SocketIOClientSwift/SocketIOClientOption.swift index a704c32..309f14a 100644 --- a/SocketIOClientSwift/SocketIOClientOption.swift +++ b/SocketIOClientSwift/SocketIOClientOption.swift @@ -88,11 +88,7 @@ public enum SocketIOClientOption: CustomStringConvertible, Hashable { } static func getSocketIOOptionValue(option: SocketIOClientOption) -> AnyObject? { - if let value = Mirror(reflecting: option).children.first?.value as? AnyObject { - return value - } else { - return nil - } + return Mirror(reflecting: option).children.first?.value as? AnyObject } static func NSDictionaryToSocketOptionsSet(dict: NSDictionary) -> SocketOptionsSet { From c99f9a6b97c68ec97ffd27a02516c47355d757ad Mon Sep 17 00:00:00 2001 From: Erik Date: Sun, 18 Oct 2015 16:33:31 -0400 Subject: [PATCH 11/16] make sure wait is positive --- SocketIOClientSwift/SocketIOClient.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SocketIOClientSwift/SocketIOClient.swift b/SocketIOClientSwift/SocketIOClient.swift index ea0b562..4a08b63 100644 --- a/SocketIOClientSwift/SocketIOClient.swift +++ b/SocketIOClientSwift/SocketIOClient.swift @@ -77,7 +77,7 @@ public final class SocketIOClient: NSObject, SocketEngineClient { case .ReconnectAttempts(let attempts): reconnectAttempts = attempts case .ReconnectWait(let wait): - reconnectWait = wait + reconnectWait = abs(wait) case .Nsp(let nsp): self.nsp = nsp case .Log(let log): From 8e2f4789890294b11bd96d11cb4df7f6644f798f Mon Sep 17 00:00:00 2001 From: Erik Date: Sun, 18 Oct 2015 16:36:01 -0400 Subject: [PATCH 12/16] Make static method an instance method --- SocketIOClientSwift/SocketIOClientOption.swift | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/SocketIOClientSwift/SocketIOClientOption.swift b/SocketIOClientSwift/SocketIOClientOption.swift index 309f14a..cc3a16d 100644 --- a/SocketIOClientSwift/SocketIOClientOption.swift +++ b/SocketIOClientSwift/SocketIOClientOption.swift @@ -87,8 +87,8 @@ public enum SocketIOClientOption: CustomStringConvertible, Hashable { } } - static func getSocketIOOptionValue(option: SocketIOClientOption) -> AnyObject? { - return Mirror(reflecting: option).children.first?.value as? AnyObject + func getSocketIOOptionValue() -> AnyObject? { + return Mirror(reflecting: self).children.first?.value as? AnyObject } static func NSDictionaryToSocketOptionsSet(dict: NSDictionary) -> SocketOptionsSet { @@ -107,7 +107,7 @@ public enum SocketIOClientOption: CustomStringConvertible, Hashable { let options = NSMutableDictionary() for option in set { - options[option.description] = getSocketIOOptionValue(option) + options[option.description] = option.getSocketIOOptionValue() } return options From ab56b58b2d05f02812bf0604d4302e2e1a9f857d Mon Sep 17 00:00:00 2001 From: Erik Date: Sun, 18 Oct 2015 16:42:34 -0400 Subject: [PATCH 13/16] rename external variable --- README.md | 8 ++++---- SocketIOClientSwift/SocketIOClient.swift | 14 +++++++------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 60ba181..e9f3ea3 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ Socket.IO-client for iOS/OS X. ##Example ```swift -let socket = SocketIOClient(socketURL: "localhost:8080", opts: [.Log(true), .ForcePolling(true)]) +let socket = SocketIOClient(socketURL: "localhost:8080", options: [.Log(true), .ForcePolling(true)]) socket.on("connect") {data, ack in print("socket connected") @@ -26,7 +26,7 @@ socket.connect() ##Objective-C Example ```objective-c -SocketIOClient* socket = [[SocketIOClient alloc] initWithSocketURL:@"localhost:8080" opts:@{@"log": @YES, @"forcePolling": @YES}]; +SocketIOClient* socket = [[SocketIOClient alloc] initWithSocketURL:@"localhost:8080" options:@{@"log": @YES, @"forcePolling": @YES}]; [socket on:@"connect" callback:^(NSArray* data, SocketAckEmitter* ack) { NSLog(@"socket connected"); @@ -120,8 +120,8 @@ Run `seed install`. ##API Constructors ----------- -`init(var socketURL: String, opts: SocketOptionsSet? = nil)` - Creates a new SocketIOClient. opts is a Set of SocketIOClientOption. If your socket.io server is secure, you need to specify `https` in your socketURL. -`convenience init(socketURL: String, opts: NSDictionary?)` - Same as above, but meant for Objective-C. See Options on how convert between SocketIOClientOptions and dictionary keys. +`init(var socketURL: String, options: SocketOptionsSet? = nil)` - Creates a new SocketIOClient. opts is a Set of SocketIOClientOption. If your socket.io server is secure, you need to specify `https` in your socketURL. +`convenience init(socketURL: String, options: NSDictionary?)` - Same as above, but meant for Objective-C. See Options on how convert between SocketIOClientOptions and dictionary keys. Options ------- diff --git a/SocketIOClientSwift/SocketIOClient.swift b/SocketIOClientSwift/SocketIOClient.swift index 4a08b63..2a9cf7d 100644 --- a/SocketIOClientSwift/SocketIOClient.swift +++ b/SocketIOClientSwift/SocketIOClient.swift @@ -32,7 +32,7 @@ public final class SocketIOClient: NSObject, SocketEngineClient { public private(set) var status = SocketIOClientStatus.NotConnected public var nsp = "/" - public var opts: SocketOptionsSet? + public var options: SocketOptionsSet? public var reconnects = true public var reconnectWait = 10 public var sid: String? { @@ -57,7 +57,7 @@ public final class SocketIOClient: NSObject, SocketEngineClient { /** Type safe way to create a new SocketIOClient. opts can be omitted */ - public init(var socketURL: String, opts: SocketOptionsSet? = nil) { + public init(var socketURL: String, options: SocketOptionsSet? = nil) { if socketURL["https://"].matches().count != 0 { self.secure = true } @@ -66,9 +66,9 @@ public final class SocketIOClient: NSObject, SocketEngineClient { socketURL = socketURL["https://"] ~= "" self.socketURL = socketURL - self.opts = opts + self.options = options - for option in opts ?? [] { + for option in options ?? [] { switch option { case .ConnectParams(let params): connectParams = params @@ -98,9 +98,9 @@ public final class SocketIOClient: NSObject, SocketEngineClient { Not so type safe way to create a SocketIOClient, meant for Objective-C compatiblity. If using Swift it's recommended to use `init(var socketURL: String, opts: SocketOptionsDictionary? = nil)` */ - public convenience init(socketURL: String, opts: NSDictionary?) { + public convenience init(socketURL: String, options: NSDictionary?) { self.init(socketURL: socketURL, - opts: SocketIOClientOption.NSDictionaryToSocketOptionsSet(opts ?? [:])) + options: SocketIOClientOption.NSDictionaryToSocketOptionsSet(options ?? [:])) } deinit { @@ -111,7 +111,7 @@ public final class SocketIOClient: NSObject, SocketEngineClient { Logger.log("Adding engine", type: logType) let newEngine = SocketEngine(client: self, opts: - SocketIOClientOption.SocketOptionsSetToNSDictionary(opts ?? [])) + SocketIOClientOption.SocketOptionsSetToNSDictionary(options ?? [])) engine = newEngine return newEngine From b97574e28a26837f7c406e20f95f11aec56c56ca Mon Sep 17 00:00:00 2001 From: Erik Date: Sun, 18 Oct 2015 16:55:33 -0400 Subject: [PATCH 14/16] don't typealias --- README.md | 2 +- SocketIOClientSwift/SocketIOClient.swift | 4 ++-- SocketIOClientSwift/SocketIOClientOption.swift | 6 +++--- SocketIOClientSwift/SocketTypes.swift | 1 - 4 files changed, 6 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index e9f3ea3..87e60d6 100644 --- a/README.md +++ b/README.md @@ -120,7 +120,7 @@ Run `seed install`. ##API Constructors ----------- -`init(var socketURL: String, options: SocketOptionsSet? = nil)` - Creates a new SocketIOClient. opts is a Set of SocketIOClientOption. If your socket.io server is secure, you need to specify `https` in your socketURL. +`init(var socketURL: String, options: Set? = nil)` - Creates a new SocketIOClient. opts is a Set of SocketIOClientOption. If your socket.io server is secure, you need to specify `https` in your socketURL. `convenience init(socketURL: String, options: NSDictionary?)` - Same as above, but meant for Objective-C. See Options on how convert between SocketIOClientOptions and dictionary keys. Options diff --git a/SocketIOClientSwift/SocketIOClient.swift b/SocketIOClientSwift/SocketIOClient.swift index 2a9cf7d..2a2bada 100644 --- a/SocketIOClientSwift/SocketIOClient.swift +++ b/SocketIOClientSwift/SocketIOClient.swift @@ -32,7 +32,7 @@ public final class SocketIOClient: NSObject, SocketEngineClient { public private(set) var status = SocketIOClientStatus.NotConnected public var nsp = "/" - public var options: SocketOptionsSet? + public var options: Set? public var reconnects = true public var reconnectWait = 10 public var sid: String? { @@ -57,7 +57,7 @@ public final class SocketIOClient: NSObject, SocketEngineClient { /** Type safe way to create a new SocketIOClient. opts can be omitted */ - public init(var socketURL: String, options: SocketOptionsSet? = nil) { + public init(var socketURL: String, options: Set? = nil) { if socketURL["https://"].matches().count != 0 { self.secure = true } diff --git a/SocketIOClientSwift/SocketIOClientOption.swift b/SocketIOClientSwift/SocketIOClientOption.swift index cc3a16d..11a5b01 100644 --- a/SocketIOClientSwift/SocketIOClientOption.swift +++ b/SocketIOClientSwift/SocketIOClientOption.swift @@ -91,8 +91,8 @@ public enum SocketIOClientOption: CustomStringConvertible, Hashable { return Mirror(reflecting: self).children.first?.value as? AnyObject } - static func NSDictionaryToSocketOptionsSet(dict: NSDictionary) -> SocketOptionsSet { - var options = SocketOptionsSet() + static func NSDictionaryToSocketOptionsSet(dict: NSDictionary) -> Set { + var options = Set() for (rawKey, value) in dict { if let key = rawKey as? String, opt = keyValueToSocketIOClientOption(key, value: value) { @@ -103,7 +103,7 @@ public enum SocketIOClientOption: CustomStringConvertible, Hashable { return options } - static func SocketOptionsSetToNSDictionary(set: SocketOptionsSet) -> NSDictionary { + static func SocketOptionsSetToNSDictionary(set: Set) -> NSDictionary { let options = NSMutableDictionary() for option in set { diff --git a/SocketIOClientSwift/SocketTypes.swift b/SocketIOClientSwift/SocketTypes.swift index aa07698..86bdeee 100644 --- a/SocketIOClientSwift/SocketTypes.swift +++ b/SocketIOClientSwift/SocketTypes.swift @@ -27,7 +27,6 @@ import Foundation public typealias AckCallback = ([AnyObject]) -> Void public typealias NormalCallback = ([AnyObject], SocketAckEmitter?) -> Void public typealias OnAckCallback = (timeoutAfter: UInt64, callback: AckCallback) -> Void -public typealias SocketOptionsSet = Set enum Either { case Left(E) From 6e31f54e88992090887e1dbefa959e310446c437 Mon Sep 17 00:00:00 2001 From: Erik Date: Sun, 18 Oct 2015 18:27:12 -0400 Subject: [PATCH 15/16] fix socketio/socket.io-client-swift#216 --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 87e60d6..18e626d 100644 --- a/README.md +++ b/README.md @@ -158,6 +158,8 @@ Methods 11. `reconnect()` - Causes the client to reconnect to the server. 12. `joinNamespace()` - Causes the client to join nsp. Shouldn't need to be called unless you change nsp manually. 13. `leaveNamespace()` - Causes the client to leave the nsp and go back to / +14. `off(event: String)` - Removes all event handlers for event. +15. `removeAllHandlers()` - Removes all handlers.s Client Events ------ From 86bd271a9f7e0787edb96f70749f6cffe68b351d Mon Sep 17 00:00:00 2001 From: Erik Date: Sun, 18 Oct 2015 18:27:52 -0400 Subject: [PATCH 16/16] remove extra letter --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 18e626d..b33d9a4 100644 --- a/README.md +++ b/README.md @@ -159,7 +159,7 @@ Methods 12. `joinNamespace()` - Causes the client to join nsp. Shouldn't need to be called unless you change nsp manually. 13. `leaveNamespace()` - Causes the client to leave the nsp and go back to / 14. `off(event: String)` - Removes all event handlers for event. -15. `removeAllHandlers()` - Removes all handlers.s +15. `removeAllHandlers()` - Removes all handlers. Client Events ------