diff --git a/README.md b/README.md index feeacfc..6fece28 100644 --- a/README.md +++ b/README.md @@ -118,6 +118,8 @@ Methods 8. `connect(#timeoutAfter:Int, withTimeoutHandler handler:(() -> Void)?)` - Connect to the server. If it isn't connected after timeoutAfter seconds, the handler is called. 9. `close(#fast:Bool)` - Closes the socket. Once a socket is closed it should not be reopened. Pass true to fast if you're closing from a background task. 10. `reconnect()` - Causes the client to reconnect to the server. +11. `joinNamespace()` - Causes the client to join nsp. Shouldn't need to be called unless you change nsp manually. +12. `leaveNamespace()` - Causes the client to leave the nsp and go back to / Client Events ------ diff --git a/SocketIOClientSwift/SocketEngine.swift b/SocketIOClientSwift/SocketEngine.swift index 629e8fc..caec186 100644 --- a/SocketIOClientSwift/SocketEngine.swift +++ b/SocketIOClientSwift/SocketEngine.swift @@ -34,6 +34,7 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient { private typealias Probe = (msg:String, type:PacketType, data:ContiguousArray?) private typealias ProbeWaitQueue = [Probe] + private let allowedCharacterSet = NSCharacterSet(charactersInString: "!*'();:@&=+$,/?%#[]\" {}").invertedSet private let workQueue = NSOperationQueue() private let emitQueue = dispatch_queue_create("engineEmitQueue", DISPATCH_QUEUE_SERIAL) private let parseQueue = dispatch_queue_create("engineParseQueue", DISPATCH_QUEUE_SERIAL) @@ -167,17 +168,16 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient { } if params != nil { - let allowedCharacterSet = NSCharacterSet(charactersInString: "!*'();:@&=+$,/?%#[]\" {}").invertedSet for (key, value) in params! { let keyEsc = key.stringByAddingPercentEncodingWithAllowedCharacters( - allowedCharacterSet)! + self.allowedCharacterSet)! urlPolling += "&\(keyEsc)=" urlWebSocket += "&\(keyEsc)=" if value is String { let valueEsc = (value as! String).stringByAddingPercentEncodingWithAllowedCharacters( - allowedCharacterSet)! + self.allowedCharacterSet)! urlPolling += "\(valueEsc)" urlWebSocket += "\(valueEsc)" } else { @@ -219,7 +219,7 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient { if self.websocket || self.waitingForPoll || !self.connected { return } - + self.waitingForPoll = true let req = NSMutableURLRequest(URL: NSURL(string: self.urlPolling! + "&sid=\(self.sid)&b64=1")!) @@ -246,7 +246,7 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient { if this.polling { this.handlePollingFailed(err.localizedDescription) } else { - NSLog(err.localizedDescription) + SocketLogger.err(err.localizedDescription, client: this) } return } @@ -263,7 +263,6 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient { if this.fastUpgrade { this.doFastUpgrade() - return } else if !this.closed && this.polling { this.doPoll() } diff --git a/SocketIOClientSwift/SocketIOClient.swift b/SocketIOClientSwift/SocketIOClient.swift index 5e4028f..55dab45 100644 --- a/SocketIOClientSwift/SocketIOClient.swift +++ b/SocketIOClientSwift/SocketIOClient.swift @@ -404,7 +404,20 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketLogClient } } - func joinNamespace() { + /** + Leaves nsp and goes back to / + */ + public func leaveNamespace() { + if self.nsp != "/" { + self.engine?.send("1/\(self.nsp)", withData: nil) + self.nsp = "/" + } + } + + /** + Joins nsp if it is not / + */ + public func joinNamespace() { SocketLogger.log("Joining namespace", client: self) if self.nsp != "/" {