diff --git a/README.md b/README.md index 5f21917..de43c9d 100644 --- a/README.md +++ b/README.md @@ -77,7 +77,7 @@ import PackageDescription let package = Package( name: "YourSocketIOProject", dependencies: [ - .Package(url: "https://github.com/socketio/socket.io-client-swift", majorVersion: 5) + .Package(url: "https://github.com/socketio/socket.io-client-swift", majorVersion: 6) ] ) ``` @@ -88,7 +88,7 @@ Carthage ----------------- Add this line to your `Cartfile`: ``` -github "socketio/socket.io-client-swift" ~> 6.1.0 # Or latest version +github "socketio/socket.io-client-swift" ~> 6.1.1 # Or latest version ``` Run `carthage update --platform ios,macosx`. @@ -102,7 +102,7 @@ source 'https://github.com/CocoaPods/Specs.git' platform :ios, '8.0' use_frameworks! -pod 'Socket.IO-Client-Swift', '~> 6.1.0' # Or latest version +pod 'Socket.IO-Client-Swift', '~> 6.1.1' # Or latest version ``` Install pods: @@ -130,7 +130,7 @@ CocoaSeeds Add this line to your `Seedfile`: ``` -github "socketio/socket.io-client-swift", "v6.1.0", :files => "Source/*.swift" # Or latest version +github "socketio/socket.io-client-swift", "v6.1.1", :files => "Source/*.swift" # Or latest version ``` Run `seed install`. diff --git a/Socket.IO-Client-Swift.podspec b/Socket.IO-Client-Swift.podspec index 52ae33e..248adc5 100644 --- a/Socket.IO-Client-Swift.podspec +++ b/Socket.IO-Client-Swift.podspec @@ -1,7 +1,7 @@ Pod::Spec.new do |s| s.name = "Socket.IO-Client-Swift" s.module_name = "SocketIOClientSwift" - s.version = "6.1.0" + s.version = "6.1.1" s.summary = "Socket.IO-client for iOS and OS X" s.description = <<-DESC Socket.IO-client for iOS and OS X. @@ -14,7 +14,7 @@ Pod::Spec.new do |s| s.ios.deployment_target = '8.0' s.osx.deployment_target = '10.10' s.tvos.deployment_target = '9.0' - s.source = { :git => "https://github.com/socketio/socket.io-client-swift.git", :tag => 'v6.1.0' } + s.source = { :git => "https://github.com/socketio/socket.io-client-swift.git", :tag => 'v6.1.1' } s.source_files = "Source/**/*.swift" s.requires_arc = true # s.dependency 'Starscream', '~> 0.9' # currently this repo includes Starscream swift files diff --git a/Socket.IO-Client-Swift.xcodeproj/project.pbxproj b/Socket.IO-Client-Swift.xcodeproj/project.pbxproj index e513ca8..97318d8 100644 --- a/Socket.IO-Client-Swift.xcodeproj/project.pbxproj +++ b/Socket.IO-Client-Swift.xcodeproj/project.pbxproj @@ -119,15 +119,21 @@ 742D150C1CA5794B00BD987D /* SocketObjectiveCTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 742D150B1CA5794B00BD987D /* SocketObjectiveCTest.m */; }; 74321DCB1C2D939A00CF6F43 /* SocketAckManagerTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74321DC91C2D939A00CF6F43 /* SocketAckManagerTest.swift */; }; 74321DCC1C2D939A00CF6F43 /* SocketParserTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74321DCA1C2D939A00CF6F43 /* SocketParserTest.swift */; }; - 7471CCEA1C39926300364B59 /* SocketClientSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74ABF7761C3991C10078C657 /* SocketClientSpec.swift */; }; - 7471CCEB1C39926C00364B59 /* SocketClientSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74ABF7761C3991C10078C657 /* SocketClientSpec.swift */; }; + 7471CCEA1C39926300364B59 /* SocketIOClientSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74ABF7761C3991C10078C657 /* SocketIOClientSpec.swift */; }; + 7471CCEB1C39926C00364B59 /* SocketIOClientSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74ABF7761C3991C10078C657 /* SocketIOClientSpec.swift */; }; 7472C65C1BCAB53E003CA70D /* SocketNamespacePacketTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7472C65B1BCAB53E003CA70D /* SocketNamespacePacketTest.swift */; }; 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 */; }; - 74ABF7771C3991C10078C657 /* SocketClientSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74ABF7761C3991C10078C657 /* SocketClientSpec.swift */; }; + 74ABF7771C3991C10078C657 /* SocketIOClientSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74ABF7761C3991C10078C657 /* SocketIOClientSpec.swift */; }; 74F124F01BC574CF002966F4 /* SocketBasicPacketTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74F124EF1BC574CF002966F4 /* SocketBasicPacketTest.swift */; }; 74F124F11BC574CF002966F4 /* SocketBasicPacketTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74F124EF1BC574CF002966F4 /* SocketBasicPacketTest.swift */; }; + CEBA56961CDA0B7700BA0389 /* NSCharacterSet.swift in Sources */ = {isa = PBXBuildFile; fileRef = CEBA56951CDA0B7700BA0389 /* NSCharacterSet.swift */; }; + CEBA56971CDA0B7700BA0389 /* NSCharacterSet.swift in Sources */ = {isa = PBXBuildFile; fileRef = CEBA56951CDA0B7700BA0389 /* NSCharacterSet.swift */; }; + CEBA56981CDA0B7700BA0389 /* NSCharacterSet.swift in Sources */ = {isa = PBXBuildFile; fileRef = CEBA56951CDA0B7700BA0389 /* NSCharacterSet.swift */; }; + CEBA569A1CDA0B8200BA0389 /* String.swift in Sources */ = {isa = PBXBuildFile; fileRef = CEBA56991CDA0B8200BA0389 /* String.swift */; }; + CEBA569B1CDA0B8200BA0389 /* String.swift in Sources */ = {isa = PBXBuildFile; fileRef = CEBA56991CDA0B8200BA0389 /* String.swift */; }; + CEBA569C1CDA0B8200BA0389 /* String.swift in Sources */ = {isa = PBXBuildFile; fileRef = CEBA56991CDA0B8200BA0389 /* String.swift */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -193,8 +199,10 @@ 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 = ""; }; - 74ABF7761C3991C10078C657 /* SocketClientSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SocketClientSpec.swift; path = Source/SocketClientSpec.swift; sourceTree = ""; }; + 74ABF7761C3991C10078C657 /* SocketIOClientSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SocketIOClientSpec.swift; path = Source/SocketIOClientSpec.swift; sourceTree = ""; }; 74F124EF1BC574CF002966F4 /* SocketBasicPacketTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SocketBasicPacketTest.swift; sourceTree = ""; }; + CEBA56951CDA0B7700BA0389 /* NSCharacterSet.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = NSCharacterSet.swift; path = Source/NSCharacterSet.swift; sourceTree = ""; }; + CEBA56991CDA0B8200BA0389 /* String.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = String.swift; path = Source/String.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -347,10 +355,10 @@ 5764DF7B1B51F24A004FF46E /* Source */ = { isa = PBXGroup; children = ( + CEBA569E1CDA0C0C00BA0389 /* utils */, 74171E501C10CD240062D398 /* SocketAckEmitter.swift */, 74171E511C10CD240062D398 /* SocketAckManager.swift */, 74171E521C10CD240062D398 /* SocketAnyEvent.swift */, - 74ABF7761C3991C10078C657 /* SocketClientSpec.swift */, 74171E531C10CD240062D398 /* SocketEngine.swift */, 74171E541C10CD240062D398 /* SocketEngineClient.swift */, 74171E551C10CD240062D398 /* SocketEnginePacketType.swift */, @@ -360,6 +368,7 @@ 74171E571C10CD240062D398 /* SocketEventHandler.swift */, 74171E591C10CD240062D398 /* SocketIOClient.swift */, 74171E5A1C10CD240062D398 /* SocketIOClientOption.swift */, + 74ABF7761C3991C10078C657 /* SocketIOClientSpec.swift */, 74171E5B1C10CD240062D398 /* SocketIOClientStatus.swift */, 74171E5C1C10CD240062D398 /* SocketLogger.swift */, 74171E5D1C10CD240062D398 /* SocketPacket.swift */, @@ -372,6 +381,15 @@ name = Source; sourceTree = ""; }; + CEBA569E1CDA0C0C00BA0389 /* utils */ = { + isa = PBXGroup; + children = ( + CEBA56951CDA0B7700BA0389 /* NSCharacterSet.swift */, + CEBA56991CDA0B8200BA0389 /* String.swift */, + ); + name = utils; + sourceTree = ""; + }; /* End PBXGroup section */ /* Begin PBXHeadersBuildPhase section */ @@ -607,10 +625,12 @@ files = ( 740CA1221C496EF700CB98F4 /* SocketEngineWebsocket.swift in Sources */, 74171EA51C10CD240062D398 /* SocketIOClientStatus.swift in Sources */, + CEBA569A1CDA0B8200BA0389 /* String.swift in Sources */, 74171E751C10CD240062D398 /* SocketEngine.swift in Sources */, 74171E691C10CD240062D398 /* SocketAckManager.swift in Sources */, 7420CB791C49629E00956AA4 /* SocketEnginePollable.swift in Sources */, - 74ABF7771C3991C10078C657 /* SocketClientSpec.swift in Sources */, + CEBA56961CDA0B7700BA0389 /* NSCharacterSet.swift in Sources */, + 74ABF7771C3991C10078C657 /* SocketIOClientSpec.swift in Sources */, 74171E871C10CD240062D398 /* SocketEngineSpec.swift in Sources */, 74171E631C10CD240062D398 /* SocketAckEmitter.swift in Sources */, 74171EBD1C10CD240062D398 /* SocketStringReader.swift in Sources */, @@ -663,10 +683,12 @@ buildActionMask = 2147483647; files = ( 740CA1211C496EF200CB98F4 /* SocketEngineWebsocket.swift in Sources */, - 7471CCEA1C39926300364B59 /* SocketClientSpec.swift in Sources */, + 7471CCEA1C39926300364B59 /* SocketIOClientSpec.swift in Sources */, + CEBA569B1CDA0B8200BA0389 /* String.swift in Sources */, 74171EA71C10CD240062D398 /* SocketIOClientStatus.swift in Sources */, 74171E771C10CD240062D398 /* SocketEngine.swift in Sources */, 7420CB7A1C49629E00956AA4 /* SocketEnginePollable.swift in Sources */, + CEBA56971CDA0B7700BA0389 /* NSCharacterSet.swift in Sources */, 74171E6B1C10CD240062D398 /* SocketAckManager.swift in Sources */, 74171E891C10CD240062D398 /* SocketEngineSpec.swift in Sources */, 74171E651C10CD240062D398 /* SocketAckEmitter.swift in Sources */, @@ -706,10 +728,12 @@ buildActionMask = 2147483647; files = ( 740CA1201C496EEB00CB98F4 /* SocketEngineWebsocket.swift in Sources */, - 7471CCEB1C39926C00364B59 /* SocketClientSpec.swift in Sources */, + 7471CCEB1C39926C00364B59 /* SocketIOClientSpec.swift in Sources */, + CEBA569C1CDA0B8200BA0389 /* String.swift in Sources */, 74171EA91C10CD240062D398 /* SocketIOClientStatus.swift in Sources */, 74171E791C10CD240062D398 /* SocketEngine.swift in Sources */, 7420CB7B1C49629E00956AA4 /* SocketEnginePollable.swift in Sources */, + CEBA56981CDA0B7700BA0389 /* NSCharacterSet.swift in Sources */, 74171E6D1C10CD240062D398 /* SocketAckManager.swift in Sources */, 74171E8B1C10CD240062D398 /* SocketEngineSpec.swift in Sources */, 74171E671C10CD240062D398 /* SocketAckEmitter.swift in Sources */, diff --git a/SocketIO-MacTests/SocketEngineTest.swift b/SocketIO-MacTests/SocketEngineTest.swift index 7b2388c..ec543a1 100644 --- a/SocketIO-MacTests/SocketEngineTest.swift +++ b/SocketIO-MacTests/SocketEngineTest.swift @@ -86,4 +86,20 @@ class SocketEngineTest: XCTestCase { engine.parsePollingMessage("41:42[\"stringTest\",\"lïne one\\nlīne \\rtwo\"]") waitForExpectationsWithTimeout(3, handler: nil) } + + func testEncodeURLProperly() { + engine.connectParams = [ + "created": "2016-05-04T18:31:15+0200" + ] + + XCTAssertEqual(engine.urlPolling.query, "transport=polling&b64=1&created=2016-05-04T18%3A31%3A15%2B0200") + XCTAssertEqual(engine.urlWebSocket.query, "transport=websocket&created=2016-05-04T18%3A31%3A15%2B0200") + + engine.connectParams = [ + "forbidden": "!*'();:@&=+$,/?%#[]\" {}" + ] + + XCTAssertEqual(engine.urlPolling.query, "transport=polling&b64=1&forbidden=%21%2A%27%28%29%3B%3A%40%26%3D%2B%24%2C%2F%3F%25%23%5B%5D%22%20%7B%7D") + XCTAssertEqual(engine.urlWebSocket.query, "transport=websocket&forbidden=%21%2A%27%28%29%3B%3A%40%26%3D%2B%24%2C%2F%3F%25%23%5B%5D%22%20%7B%7D") + } } diff --git a/SocketIO-MacTests/SocketObjectiveCTest.m b/SocketIO-MacTests/SocketObjectiveCTest.m index 8372112..f3f70e3 100644 --- a/SocketIO-MacTests/SocketObjectiveCTest.m +++ b/SocketIO-MacTests/SocketObjectiveCTest.m @@ -35,7 +35,9 @@ } - (void)testEmitWithAckSyntax { - [self.socket emitWithAck:@"testAckEmit" withItems:@[@YES]]; + [self.socket emitWithAck:@"testAckEmit" withItems:@[@YES]](0, ^(NSArray* data) { + + }); } - (void)testOffSyntax { diff --git a/Source/NSCharacterSet.swift b/Source/NSCharacterSet.swift new file mode 100644 index 0000000..3ccde2c --- /dev/null +++ b/Source/NSCharacterSet.swift @@ -0,0 +1,31 @@ +// +// NSCharacterSet.swift +// Socket.IO-Client-Swift +// +// Created by Yannick Loriot on 5/4/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. + +import Foundation + +extension NSCharacterSet { + class var allowedURLCharacterSet: NSCharacterSet { + return NSCharacterSet(charactersInString: "!*'();:@&=+$,/?%#[]\" {}").invertedSet + } +} diff --git a/Source/SocketEngine.swift b/Source/SocketEngine.swift index e886cad..e06a811 100644 --- a/Source/SocketEngine.swift +++ b/Source/SocketEngine.swift @@ -34,6 +34,7 @@ public final class SocketEngine : NSObject, SocketEnginePollable, SocketEngineWe (urlPolling, urlWebSocket) = createURLs() } } + public var postWait = [String]() public var waitingForPoll = false public var waitingForPost = false @@ -133,23 +134,16 @@ public final class SocketEngine : NSObject, SocketEnginePollable, SocketEngineWe allowLossyConversion: false) else { return } do { - if let dict = try NSJSONSerialization.JSONObjectWithData(stringData, - options: NSJSONReadingOptions.MutableContainers) as? NSDictionary { - guard let code = dict["code"] as? Int else { return } - guard let error = dict["message"] as? String else { return } - - switch code { - case 0: // Unknown transport - didError(error) - case 1: // Unknown sid. - didError(error) - case 2: // Bad handshake request - didError(error) - case 3: // Bad request - didError(error) - default: - didError(error) - } + if let dict = try NSJSONSerialization.JSONObjectWithData(stringData, options: .MutableContainers) as? NSDictionary { + guard let error = dict["message"] as? String else { return } + + /* + 0: Unknown transport + 1: Unknown sid + 2: Bad handshake request + 3: Bad request + */ + didError(error) } } catch { didError("Got unknown error from server \(msg)") @@ -204,7 +198,9 @@ public final class SocketEngine : NSObject, SocketEnginePollable, SocketEngineWe } } - doLongPoll(reqPolling) + dispatch_async(emitQueue) { + self.doLongPoll(reqPolling) + } } private func createURLs() -> (NSURL, NSURL) { @@ -218,8 +214,6 @@ public final class SocketEngine : NSObject, SocketEnginePollable, SocketEngineWe urlWebSocket.path = socketPath urlPolling.path = socketPath - urlWebSocket.query = "transport=websocket" - urlPolling.query = "transport=polling&b64=1" if secure { urlPolling.scheme = "https" @@ -231,12 +225,15 @@ public final class SocketEngine : NSObject, SocketEnginePollable, SocketEngineWe if connectParams != nil { for (key, value) in connectParams! { - queryString += "&\(key)=\(value)" + let keyEsc = key.urlEncode()! + let valueEsc = "\(value)".urlEncode()! + + queryString += "&\(keyEsc)=\(valueEsc)" } } - urlWebSocket.query = urlWebSocket.query! + queryString - urlPolling.query = urlPolling.query! + queryString + urlWebSocket.percentEncodedQuery = "transport=websocket" + queryString + urlPolling.percentEncodedQuery = "transport=polling&b64=1" + queryString return (urlPolling.URL!, urlWebSocket.URL!) } @@ -286,7 +283,8 @@ public final class SocketEngine : NSObject, SocketEnginePollable, SocketEngineWe DefaultSocketLogger.Logger.log("Engine is being closed.", type: logType) if closed { - return postSendClose(nil, nil, nil) + client?.engineDidClose(reason) + return } if websocket { @@ -393,7 +391,6 @@ public final class SocketEngine : NSObject, SocketEnginePollable, SocketEngineWe } } catch { didError("Error parsing open packet") - return } } diff --git a/Source/SocketEnginePollable.swift b/Source/SocketEnginePollable.swift index 86291cd..c419e51 100644 --- a/Source/SocketEnginePollable.swift +++ b/Source/SocketEnginePollable.swift @@ -181,9 +181,7 @@ extension SocketEnginePollable { } func parsePollingMessage(str: String) { - guard str.characters.count != 1 else { - return - } + guard str.characters.count != 1 else { return } var reader = SocketStringReader(message: str) @@ -210,7 +208,7 @@ extension SocketEnginePollable { let fixedMessage: String if doubleEncodeUTF8 { - fixedMessage = doubleEncodeUTF8(message) + fixedMessage = doubleEncodeUTF8(message) } else { fixedMessage = message } diff --git a/Source/SocketEngineSpec.swift b/Source/SocketEngineSpec.swift index 785fc6f..7fdd779 100644 --- a/Source/SocketEngineSpec.swift +++ b/Source/SocketEngineSpec.swift @@ -46,6 +46,7 @@ import Foundation var urlPolling: NSURL { get } var urlWebSocket: NSURL { get } var websocket: Bool { get } + var ws: WebSocket? { get } init(client: SocketEngineClient, url: NSURL, options: NSDictionary?) @@ -62,14 +63,14 @@ import Foundation extension SocketEngineSpec { var urlPollingWithSid: NSURL { let com = NSURLComponents(URL: urlPolling, resolvingAgainstBaseURL: false)! - com.query = com.query! + "&sid=\(sid)" + com.percentEncodedQuery = com.percentEncodedQuery! + "&sid=\(sid.urlEncode()!)" return com.URL! } var urlWebSocketWithSid: NSURL { let com = NSURLComponents(URL: urlWebSocket, resolvingAgainstBaseURL: false)! - com.query = com.query! + (sid == "" ? "" : "&sid=\(sid)") + com.percentEncodedQuery = com.percentEncodedQuery! + (sid == "" ? "" : "&sid=\(sid.urlEncode()!)") return com.URL! } diff --git a/Source/SocketEngineWebsocket.swift b/Source/SocketEngineWebsocket.swift index a173250..e1b0ba8 100644 --- a/Source/SocketEngineWebsocket.swift +++ b/Source/SocketEngineWebsocket.swift @@ -27,8 +27,6 @@ import Foundation /// Protocol that is used to implement socket.io WebSocket support public protocol SocketEngineWebsocket : SocketEngineSpec, WebSocketDelegate { - var ws: WebSocket? { get } - func sendWebSocketMessage(str: String, withType type: SocketEnginePacketType, withData datas: [NSData]) } diff --git a/Source/SocketIOClient.swift b/Source/SocketIOClient.swift index 99fa5f8..02cda38 100644 --- a/Source/SocketIOClient.swift +++ b/Source/SocketIOClient.swift @@ -131,8 +131,7 @@ public final class SocketIOClient : NSObject, SocketEngineClient, SocketParsable assert(timeoutAfter >= 0, "Invalid timeout: \(timeoutAfter)") guard status != .Connected else { - DefaultSocketLogger.Logger.log("Tried connecting on an already connected socket", - type: logType) + DefaultSocketLogger.Logger.log("Tried connecting on an already connected socket", type: logType) return } @@ -149,8 +148,8 @@ public final class SocketIOClient : NSObject, SocketEngineClient, SocketParsable let time = dispatch_time(DISPATCH_TIME_NOW, Int64(timeoutAfter) * Int64(NSEC_PER_SEC)) dispatch_after(time, handleQueue) {[weak self] in - if let this = self where this.status != .Connected && this.status != .Closed { - this.status = .Closed + if let this = self where this.status != .Connected && this.status != .Disconnected { + this.status = .Disconnected this.engine?.disconnect("Connect timeout") handler?() @@ -187,11 +186,11 @@ public final class SocketIOClient : NSObject, SocketEngineClient, SocketParsable } func didDisconnect(reason: String) { - guard status != .Closed else { return } + guard status != .Disconnected else { return } DefaultSocketLogger.Logger.log("Disconnected: %@", type: logType, args: reason) - status = .Closed + status = .Disconnected reconnects = false // Make sure the engine is actually dead. @@ -202,6 +201,8 @@ public final class SocketIOClient : NSObject, SocketEngineClient, SocketParsable /// Disconnects the socket. Only reconnect the same socket if you know what you're doing. /// Will turn off automatic reconnects. public func disconnect() { + assert(status != .NotConnected, "Tried closing a NotConnected client") + DefaultSocketLogger.Logger.log("Closing socket", type: logType) reconnects = false @@ -267,11 +268,11 @@ public final class SocketIOClient : NSObject, SocketEngineClient, SocketParsable public func engineDidClose(reason: String) { waitingPackets.removeAll() - if status != .Closed { + if status != .Disconnected { status = .NotConnected } - if status == .Closed || !reconnects { + if status == .Disconnected || !reconnects { didDisconnect(reason) } else if !reconnecting { reconnecting = true diff --git a/Source/SocketClientSpec.swift b/Source/SocketIOClientSpec.swift similarity index 94% rename from Source/SocketClientSpec.swift rename to Source/SocketIOClientSpec.swift index 48b3b17..8b33cf9 100644 --- a/Source/SocketClientSpec.swift +++ b/Source/SocketIOClientSpec.swift @@ -1,5 +1,5 @@ // -// SocketClientSpec.swift +// SocketIOClientSpec.swift // Socket.IO-Client-Swift // // Created by Erik Little on 1/3/16. @@ -22,7 +22,7 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. -protocol SocketClientSpec : class { +protocol SocketIOClientSpec : class { var nsp: String { get set } var waitingPackets: [SocketPacket] { get set } @@ -34,7 +34,7 @@ protocol SocketClientSpec : class { func joinNamespace(namespace: String) } -extension SocketClientSpec { +extension SocketIOClientSpec { func didError(reason: String) { DefaultSocketLogger.Logger.error("%@", type: "SocketIOClient", args: reason) diff --git a/Source/SocketIOClientStatus.swift b/Source/SocketIOClientStatus.swift index 09626b8..0a34c2f 100644 --- a/Source/SocketIOClientStatus.swift +++ b/Source/SocketIOClientStatus.swift @@ -24,19 +24,9 @@ import Foundation -@objc public enum SocketIOClientStatus : Int, CustomStringConvertible { - case NotConnected, Closed, Connecting, Connected - - public var description: String { - switch self { - case NotConnected: - return "Not Connected" - case Closed: - return "Closed" - case Connecting: - return "Connecting" - case Connected: - return "Connected" - } - } -} \ No newline at end of file +/// **NotConnected**: initial state +/// +/// **Disconnected**: connected before +@objc public enum SocketIOClientStatus : Int { + case NotConnected, Disconnected, Connecting, Connected +} diff --git a/Source/SocketLogger.swift b/Source/SocketLogger.swift index 9ba7d34..bff9d4e 100644 --- a/Source/SocketLogger.swift +++ b/Source/SocketLogger.swift @@ -26,7 +26,7 @@ import Foundation public protocol SocketLogger : class { /// Whether to log or not - var log: Bool {get set} + var log: Bool { get set } /// Normal log messages func log(message: String, type: String, args: AnyObject...) diff --git a/Source/SocketParsable.swift b/Source/SocketParsable.swift index 1c4084a..c74b160 100644 --- a/Source/SocketParsable.swift +++ b/Source/SocketParsable.swift @@ -22,7 +22,7 @@ import Foundation -protocol SocketParsable : SocketClientSpec { +protocol SocketParsable : SocketIOClientSpec { func parseBinaryData(data: NSData) func parseSocketMessage(message: String) } @@ -45,8 +45,7 @@ extension SocketParsable { private func handlePacket(pack: SocketPacket) { switch pack.type { case .Event where isCorrectNamespace(pack.nsp): - handleEvent(pack.event, data: pack.args, - isInternalMessage: false, withAck: pack.id) + handleEvent(pack.event, data: pack.args, isInternalMessage: false, withAck: pack.id) case .Ack where isCorrectNamespace(pack.nsp): handleAck(pack.id, data: pack.data) case .BinaryEvent where isCorrectNamespace(pack.nsp): diff --git a/Source/String.swift b/Source/String.swift new file mode 100644 index 0000000..0e30e8c --- /dev/null +++ b/Source/String.swift @@ -0,0 +1,31 @@ +// +// String.swift +// Socket.IO-Client-Swift +// +// Created by Yannick Loriot on 5/4/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. + +import Foundation + +extension String { + func urlEncode() -> String? { + return stringByAddingPercentEncodingWithAllowedCharacters(.allowedURLCharacterSet) + } +}