From e2bf84f576d809ef8d5ffa7d6630ef52d7866035 Mon Sep 17 00:00:00 2001 From: Erik Date: Sat, 13 Aug 2016 14:43:42 -0400 Subject: [PATCH 1/2] Add SocketIOClientConfiguration --- README.md | 6 +- .../project.pbxproj | 12 ++ .../SocketIOClientConfigurationTest.swift | 46 ++++++++ SocketIO-MacTests/SocketObjectiveCTest.m | 2 +- Source/SocketEngine.swift | 4 +- Source/SocketExtensions.swift | 14 +-- Source/SocketIOClient.swift | 18 +-- Source/SocketIOClientConfiguration.swift | 107 ++++++++++++++++++ Source/SocketIOClientOption.swift | 6 +- 9 files changed, 184 insertions(+), 31 deletions(-) create mode 100644 SocketIO-MacTests/SocketIOClientConfigurationTest.swift create mode 100644 Source/SocketIOClientConfiguration.swift diff --git a/README.md b/README.md index 7549222..f08ab7a 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ Socket.IO-client for iOS/OS X. ##Example ```swift -let socket = SocketIOClient(socketURL: NSURL(string: "http://localhost:8080")!, options: [.Log(true), .ForcePolling(true)]) +let socket = SocketIOClient(socketURL: NSURL(string: "http://localhost:8080")!, config: [.Log(true), .ForcePolling(true)]) socket.on("connect") {data, ack in print("socket connected") @@ -27,7 +27,7 @@ socket.connect() ##Objective-C Example ```objective-c NSURL* url = [[NSURL alloc] initWithString:@"http://localhost:8080"]; -SocketIOClient* socket = [[SocketIOClient alloc] initWithSocketURL:url options:@{@"log": @YES, @"forcePolling": @YES}]; +SocketIOClient* socket = [[SocketIOClient alloc] initWithSocketURL:url config:@{@"log": @YES, @"forcePolling": @YES}]; [socket on:@"connect" callback:^(NSArray* data, SocketAckEmitter* ack) { NSLog(@"socket connected"); @@ -139,7 +139,7 @@ Run `seed install`. ##API Constructors ----------- -`init(var socketURL: NSURL, options: Set = [])` - Creates a new SocketIOClient. options is a Set of SocketIOClientOption. If your socket.io server is secure, you need to specify `https` in your socketURL. +`init(var socketURL: NSURL, config: SocketIOClientConfiguration = [])` - Creates a new SocketIOClient. options is a Set of SocketIOClientOption. If your socket.io server is secure, you need to specify `https` in your socketURL. `convenience init(socketURL: NSURL, options: NSDictionary?)` - Same as above, but meant for Objective-C. See Options on how convert between SocketIOClientOptions and dictionary keys. diff --git a/Socket.IO-Client-Swift.xcodeproj/project.pbxproj b/Socket.IO-Client-Swift.xcodeproj/project.pbxproj index d005db9..98a0f57 100644 --- a/Socket.IO-Client-Swift.xcodeproj/project.pbxproj +++ b/Socket.IO-Client-Swift.xcodeproj/project.pbxproj @@ -115,6 +115,10 @@ 7472C65D1BCAB53E003CA70D /* SocketNamespacePacketTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7472C65B1BCAB53E003CA70D /* SocketNamespacePacketTest.swift */; }; 7472C65F1BCAC46E003CA70D /* SocketSideEffectTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7472C65E1BCAC46E003CA70D /* SocketSideEffectTest.swift */; }; 7472C6601BCAC46E003CA70D /* SocketSideEffectTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7472C65E1BCAC46E003CA70D /* SocketSideEffectTest.swift */; }; + 747BC5991D5F943500CA5FA4 /* SocketIOClientConfiguration.swift in Sources */ = {isa = PBXBuildFile; fileRef = 747BC5981D5F943500CA5FA4 /* SocketIOClientConfiguration.swift */; }; + 747BC59A1D5F943500CA5FA4 /* SocketIOClientConfiguration.swift in Sources */ = {isa = PBXBuildFile; fileRef = 747BC5981D5F943500CA5FA4 /* SocketIOClientConfiguration.swift */; }; + 747BC59B1D5F943500CA5FA4 /* SocketIOClientConfiguration.swift in Sources */ = {isa = PBXBuildFile; fileRef = 747BC5981D5F943500CA5FA4 /* SocketIOClientConfiguration.swift */; }; + 747BC59F1D5F9BA200CA5FA4 /* SocketIOClientConfigurationTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 747BC59E1D5F9BA200CA5FA4 /* SocketIOClientConfigurationTest.swift */; }; 749642B51D3FCE5500DD32D1 /* SSLSecurity.swift in Sources */ = {isa = PBXBuildFile; fileRef = 749642B31D3FCE5500DD32D1 /* SSLSecurity.swift */; }; 749642B61D3FCE5500DD32D1 /* SSLSecurity.swift in Sources */ = {isa = PBXBuildFile; fileRef = 749642B31D3FCE5500DD32D1 /* SSLSecurity.swift */; }; 749642B71D3FCE5500DD32D1 /* SSLSecurity.swift in Sources */ = {isa = PBXBuildFile; fileRef = 749642B31D3FCE5500DD32D1 /* SSLSecurity.swift */; }; @@ -192,6 +196,8 @@ 74321DCA1C2D939A00CF6F43 /* SocketParserTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SocketParserTest.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 = ""; }; + 747BC5981D5F943500CA5FA4 /* SocketIOClientConfiguration.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SocketIOClientConfiguration.swift; path = Source/SocketIOClientConfiguration.swift; sourceTree = ""; }; + 747BC59E1D5F9BA200CA5FA4 /* SocketIOClientConfigurationTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SocketIOClientConfigurationTest.swift; sourceTree = ""; }; 749642B31D3FCE5500DD32D1 /* SSLSecurity.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SSLSecurity.swift; path = Source/SSLSecurity.swift; sourceTree = ""; }; 749642B41D3FCE5500DD32D1 /* WebSocket.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = WebSocket.swift; path = Source/WebSocket.swift; sourceTree = ""; }; 74ABF7761C3991C10078C657 /* SocketIOClientSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SocketIOClientSpec.swift; path = Source/SocketIOClientSpec.swift; sourceTree = ""; }; @@ -314,6 +320,7 @@ 74F124EF1BC574CF002966F4 /* SocketBasicPacketTest.swift */, 741F39ED1BD025D80026C9CC /* SocketEngineTest.swift */, 7472C65B1BCAB53E003CA70D /* SocketNamespacePacketTest.swift */, + 747BC59E1D5F9BA200CA5FA4 /* SocketIOClientConfigurationTest.swift */, 742D150B1CA5794B00BD987D /* SocketObjectiveCTest.m */, 74321DCA1C2D939A00CF6F43 /* SocketParserTest.swift */, 7472C65E1BCAC46E003CA70D /* SocketSideEffectTest.swift */, @@ -346,6 +353,7 @@ 74171E571C10CD240062D398 /* SocketEventHandler.swift */, CEBA56991CDA0B8200BA0389 /* SocketExtensions.swift */, 74171E591C10CD240062D398 /* SocketIOClient.swift */, + 747BC5981D5F943500CA5FA4 /* SocketIOClientConfiguration.swift */, 74171E5A1C10CD240062D398 /* SocketIOClientOption.swift */, 74ABF7761C3991C10078C657 /* SocketIOClientSpec.swift */, 74171E5B1C10CD240062D398 /* SocketIOClientStatus.swift */, @@ -622,6 +630,7 @@ 74171EB71C10CD240062D398 /* SocketParsable.swift in Sources */, 74171E811C10CD240062D398 /* SocketEnginePacketType.swift in Sources */, 74171E6F1C10CD240062D398 /* SocketAnyEvent.swift in Sources */, + 747BC5991D5F943500CA5FA4 /* SocketIOClientConfiguration.swift in Sources */, 74171E9F1C10CD240062D398 /* SocketIOClientOption.swift in Sources */, 74BC45AB1D0C6675008CC431 /* SocketClientManager.swift in Sources */, ); @@ -679,6 +688,7 @@ 74171EB91C10CD240062D398 /* SocketParsable.swift in Sources */, 74171E831C10CD240062D398 /* SocketEnginePacketType.swift in Sources */, 74171E711C10CD240062D398 /* SocketAnyEvent.swift in Sources */, + 747BC59A1D5F943500CA5FA4 /* SocketIOClientConfiguration.swift in Sources */, 74171EA11C10CD240062D398 /* SocketIOClientOption.swift in Sources */, 74BC45AC1D0C6675008CC431 /* SocketClientManager.swift in Sources */, ); @@ -691,6 +701,7 @@ 742D150C1CA5794B00BD987D /* SocketObjectiveCTest.m in Sources */, 74321DCB1C2D939A00CF6F43 /* SocketAckManagerTest.swift in Sources */, 74321DCC1C2D939A00CF6F43 /* SocketParserTest.swift in Sources */, + 747BC59F1D5F9BA200CA5FA4 /* SocketIOClientConfigurationTest.swift in Sources */, 7472C6601BCAC46E003CA70D /* SocketSideEffectTest.swift in Sources */, 74171EA81C10CD240062D398 /* SocketIOClientStatus.swift in Sources */, 741F39EF1BD025D80026C9CC /* SocketEngineTest.swift in Sources */, @@ -724,6 +735,7 @@ 74171EBB1C10CD240062D398 /* SocketParsable.swift in Sources */, 74171E851C10CD240062D398 /* SocketEnginePacketType.swift in Sources */, 74171E731C10CD240062D398 /* SocketAnyEvent.swift in Sources */, + 747BC59B1D5F943500CA5FA4 /* SocketIOClientConfiguration.swift in Sources */, 74171EA31C10CD240062D398 /* SocketIOClientOption.swift in Sources */, 74BC45AD1D0C6675008CC431 /* SocketClientManager.swift in Sources */, ); diff --git a/SocketIO-MacTests/SocketIOClientConfigurationTest.swift b/SocketIO-MacTests/SocketIOClientConfigurationTest.swift new file mode 100644 index 0000000..7c78261 --- /dev/null +++ b/SocketIO-MacTests/SocketIOClientConfigurationTest.swift @@ -0,0 +1,46 @@ +// +// TestSocketIOClientConfiguration.swift +// Socket.IO-Client-Swift +// +// Created by Erik Little on 8/13/16. +// +// + +import XCTest +import SocketIOClientSwift + +class TestSocketIOClientConfiguration: XCTestCase { + var config = [] as SocketIOClientConfiguration + + override func setUp() { + super.setUp() + + config = [.Log(false), .ForceNew(true)] as SocketIOClientConfiguration + } + + func testReplaceSameOption() { + config.insert(.Log(true)) + + XCTAssertEqual(config.count, 2) + + switch config[0] { + case let .Log(log): + XCTAssertTrue(log) + default: + XCTFail() + } + } + + func testIgnoreIfExisting() { + config.insert(.ForceNew(false), replacing: false) + + XCTAssertEqual(config.count, 2) + + switch config[1] { + case let .ForceNew(new): + XCTAssertTrue(new) + default: + XCTFail() + } + } +} diff --git a/SocketIO-MacTests/SocketObjectiveCTest.m b/SocketIO-MacTests/SocketObjectiveCTest.m index b2f7832..cafeff0 100644 --- a/SocketIO-MacTests/SocketObjectiveCTest.m +++ b/SocketIO-MacTests/SocketObjectiveCTest.m @@ -21,7 +21,7 @@ - (void)setUp { [super setUp]; NSURL* url = [[NSURL alloc] initWithString:@"http://localhost"]; - self.socket = [[SocketIOClient alloc] initWithSocketURL:url options:nil]; + self.socket = [[SocketIOClient alloc] initWithSocketURL:url config:nil]; } - (void)testOnSyntax { diff --git a/Source/SocketEngine.swift b/Source/SocketEngine.swift index 0a848a2..b86f42a 100644 --- a/Source/SocketEngine.swift +++ b/Source/SocketEngine.swift @@ -80,7 +80,7 @@ public final class SocketEngine : NSObject, NSURLSessionDelegate, SocketEnginePo private var selfSigned = false private var voipEnabled = false - public init(client: SocketEngineClient, url: NSURL, options: Set) { + public init(client: SocketEngineClient, url: NSURL, options: SocketIOClientConfiguration) { self.client = client self.url = url @@ -127,7 +127,7 @@ public final class SocketEngine : NSObject, NSURLSessionDelegate, SocketEnginePo } public convenience init(client: SocketEngineClient, url: NSURL, options: NSDictionary?) { - self.init(client: client, url: url, options: options?.toSocketOptionsSet() ?? []) + self.init(client: client, url: url, options: options?.toSocketConfiguration() ?? []) } deinit { diff --git a/Source/SocketExtensions.swift b/Source/SocketExtensions.swift index 0cf05b3..f15d3be 100644 --- a/Source/SocketExtensions.swift +++ b/Source/SocketExtensions.swift @@ -89,12 +89,12 @@ extension NSDictionary { } } - func toSocketOptionsSet() -> Set { - var options = Set() + func toSocketConfiguration() -> SocketIOClientConfiguration { + var options = [] as SocketIOClientConfiguration for (rawKey, value) in self { if let key = rawKey as? String, opt = NSDictionary.keyValueToSocketIOClientOption(key, value: value) { - options.insertIgnore(opt) + options.insert(opt) } } @@ -102,14 +102,6 @@ extension NSDictionary { } } -extension Set where Element : ClientOption { - mutating func insertIgnore(element: Element) { - if !contains(element) { - insert(element) - } - } -} - extension String { func toArray() throws -> [AnyObject] { guard let stringData = dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false) else { return [] } diff --git a/Source/SocketIOClient.swift b/Source/SocketIOClient.swift index a9d0e47..0adf61c 100644 --- a/Source/SocketIOClient.swift +++ b/Source/SocketIOClient.swift @@ -42,7 +42,7 @@ public final class SocketIOClient : NSObject, SocketEngineClient, SocketParsable public var forceNew = false public var nsp = "/" - public var options: Set + public var config: SocketIOClientConfiguration public var reconnects = true public var reconnectWait = 10 public var sid: String? { @@ -67,15 +67,15 @@ public final class SocketIOClient : NSObject, SocketEngineClient, SocketParsable var waitingPackets = [SocketPacket]() /// Type safe way to create a new SocketIOClient. opts can be omitted - public init(socketURL: NSURL, options: Set = []) { - self.options = options + public init(socketURL: NSURL, config: SocketIOClientConfiguration = []) { + self.config = config self.socketURL = socketURL if socketURL.absoluteString.hasPrefix("https://") { - self.options.insertIgnore(.Secure(true)) + self.config.insert(.Secure(true)) } - for option in options { + for option in config { switch option { case let .Reconnects(reconnects): self.reconnects = reconnects @@ -98,15 +98,15 @@ public final class SocketIOClient : NSObject, SocketEngineClient, SocketParsable } } - self.options.insertIgnore(.Path("/socket.io/")) + self.config.insert(.Path("/socket.io/"), replacing: false) 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(socketURL: NSURL, options: Set)` - public convenience init(socketURL: NSURL, options: NSDictionary?) { - self.init(socketURL: socketURL, options: options?.toSocketOptionsSet() ?? []) + public convenience init(socketURL: NSURL, config: NSDictionary?) { + self.init(socketURL: socketURL, config: config?.toSocketConfiguration() ?? []) } deinit { @@ -117,7 +117,7 @@ public final class SocketIOClient : NSObject, SocketEngineClient, SocketParsable private func addEngine() -> SocketEngineSpec { DefaultSocketLogger.Logger.log("Adding engine", type: logType) - engine = SocketEngine(client: self, url: socketURL, options: options) + engine = SocketEngine(client: self, url: socketURL, options: config) return engine! } diff --git a/Source/SocketIOClientConfiguration.swift b/Source/SocketIOClientConfiguration.swift new file mode 100644 index 0000000..c743991 --- /dev/null +++ b/Source/SocketIOClientConfiguration.swift @@ -0,0 +1,107 @@ +// +// SocketIOClientConfiguration.swift +// Socket.IO-Client-Swift +// +// Created by Erik Little on 8/13/16. +// +// 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. + +public struct SocketIOClientConfiguration : ArrayLiteralConvertible, CollectionType, MutableCollectionType { + public typealias Element = SocketIOClientOption + public typealias Index = Array.Index + public typealias Generator = Array.Generator + public typealias SubSequence = Array.SubSequence + + private var backingArray = [SocketIOClientOption]() + + public var startIndex: Index { + return backingArray.startIndex + } + + public var endIndex: Index { + return backingArray.endIndex + } + + public var isEmpty: Bool { + return backingArray.isEmpty + } + + public var count: Index.Distance { + return backingArray.count + } + + public var first: Generator.Element? { + return backingArray.first + } + + public subscript(position: Index) -> Generator.Element { + get { + return backingArray[position] + } + + set { + backingArray[position] = newValue + } + } + + public subscript(bounds: Range) -> SubSequence { + get { + return backingArray[bounds] + } + + set { + backingArray[bounds] = newValue + } + } + + public init(arrayLiteral elements: Element...) { + backingArray = elements + } + + public func generate() -> Generator { + return backingArray.generate() + } + + public mutating func insert(element: Element, replacing replace: Bool = true) { + for i in 0.. SubSequence { + return backingArray.prefixUpTo(end) + } + + @warn_unused_result + public func prefixThrough(position: Index) -> SubSequence { + return prefixThrough(position) + } + + @warn_unused_result + public func suffixFrom(start: Index) -> SubSequence { + return backingArray.suffixFrom(start) + } +} diff --git a/Source/SocketIOClientOption.swift b/Source/SocketIOClientOption.swift index bf616ff..512cd05 100644 --- a/Source/SocketIOClientOption.swift +++ b/Source/SocketIOClientOption.swift @@ -24,7 +24,7 @@ import Foundation -protocol ClientOption : CustomStringConvertible, Hashable { +protocol ClientOption : CustomStringConvertible, Equatable { func getSocketIOOptionValue() -> AnyObject } @@ -99,10 +99,6 @@ public enum SocketIOClientOption : ClientOption { return description } - public var hashValue: Int { - return description.hashValue - } - func getSocketIOOptionValue() -> AnyObject { let value: AnyObject From c364f6a83233d3b4fd6616140c3e334d2d740c88 Mon Sep 17 00:00:00 2001 From: Erik Date: Sun, 14 Aug 2016 09:58:21 -0400 Subject: [PATCH 2/2] tweaks --- .../SocketIOClientConfigurationTest.swift | 2 +- Source/SocketExtensions.swift | 2 +- Source/SocketIOClient.swift | 7 ++-- Source/SocketPacket.swift | 38 ++++++------------- 4 files changed, 18 insertions(+), 31 deletions(-) diff --git a/SocketIO-MacTests/SocketIOClientConfigurationTest.swift b/SocketIO-MacTests/SocketIOClientConfigurationTest.swift index 7c78261..ee5dbdf 100644 --- a/SocketIO-MacTests/SocketIOClientConfigurationTest.swift +++ b/SocketIO-MacTests/SocketIOClientConfigurationTest.swift @@ -15,7 +15,7 @@ class TestSocketIOClientConfiguration: XCTestCase { override func setUp() { super.setUp() - config = [.Log(false), .ForceNew(true)] as SocketIOClientConfiguration + config = [.Log(false), .ForceNew(true)] } func testReplaceSameOption() { diff --git a/Source/SocketExtensions.swift b/Source/SocketExtensions.swift index f15d3be..71a8bd8 100644 --- a/Source/SocketExtensions.swift +++ b/Source/SocketExtensions.swift @@ -36,7 +36,7 @@ extension Array where Element: AnyObject { } extension NSCharacterSet { - class var allowedURLCharacterSet: NSCharacterSet { + static var allowedURLCharacterSet: NSCharacterSet { return NSCharacterSet(charactersInString: "!*'();:@&=+$,/?%#[]\" {}").invertedSet } } diff --git a/Source/SocketIOClient.swift b/Source/SocketIOClient.swift index 0adf61c..7912615 100644 --- a/Source/SocketIOClient.swift +++ b/Source/SocketIOClient.swift @@ -45,9 +45,6 @@ public final class SocketIOClient : NSObject, SocketEngineClient, SocketParsable public var config: SocketIOClientConfiguration public var reconnects = true public var reconnectWait = 10 - public var sid: String? { - return nsp + "#" + (engine?.sid ?? "") - } private let ackQueue = dispatch_queue_create("com.socketio.ackQueue", DISPATCH_QUEUE_SERIAL) private let emitQueue = dispatch_queue_create("com.socketio.emitQueue", DISPATCH_QUEUE_SERIAL) @@ -66,6 +63,10 @@ public final class SocketIOClient : NSObject, SocketEngineClient, SocketParsable var waitingPackets = [SocketPacket]() + public var sid: String? { + return nsp + "#" + (engine?.sid ?? "") + } + /// Type safe way to create a new SocketIOClient. opts can be omitted public init(socketURL: NSURL, config: SocketIOClientConfiguration = []) { self.config = config diff --git a/Source/SocketPacket.swift b/Source/SocketPacket.swift index 6326453..a34691e 100644 --- a/Source/SocketPacket.swift +++ b/Source/SocketPacket.swift @@ -26,6 +26,10 @@ import Foundation struct SocketPacket { + enum PacketType: Int { + case Connect, Disconnect, Event, Ack, Error, BinaryEvent, BinaryAck + } + private let placeholders: Int private static let logType = "SocketPacket" @@ -34,9 +38,8 @@ struct SocketPacket { let id: Int let type: PacketType - enum PacketType: Int { - case Connect, Disconnect, Event, Ack, Error, BinaryEvent, BinaryAck - } + var binary: [NSData] + var data: [AnyObject] var args: [AnyObject] { if type == .Event || type == .BinaryEvent && data.count != 0 { @@ -46,8 +49,6 @@ struct SocketPacket { } } - var binary: [NSData] - var data: [AnyObject] var description: String { return "SocketPacket {type: \(String(type.rawValue)); data: " + "\(String(data)); id: \(id); placeholders: \(placeholders); nsp: \(nsp)}" @@ -110,27 +111,12 @@ struct SocketPacket { private func createPacketString() -> String { let typeString = String(type.rawValue) - let binaryCountString: String - let nspString: String - let idString: String - - if type == .BinaryEvent || type == .BinaryAck { - binaryCountString = typeString + String(binary.count) + "-" - } else { - binaryCountString = typeString - } - - if nsp != "/" { - nspString = binaryCountString + nsp + "," - } else { - nspString = binaryCountString - } - - if id != -1 { - idString = nspString + String(id) - } else { - idString = nspString - } + // Binary count? + let binaryCountString = typeString + (type == .BinaryEvent || type == .BinaryAck ? String(binary.count) + "-" : "") + // Namespace? + let nspString = binaryCountString + (nsp != "/" ? nsp + "," : "") + // Ack number? + let idString = nspString + (id != -1 ? String(id) : "") return completeMessage(idString) }