Skip to content

Commit bb3d7fb

Browse files
committed
add leavenamepsace method, expose joinNamespace
1 parent f54153f commit bb3d7fb

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ Methods
118118
8. `connect(#timeoutAfter:Int, withTimeoutHandler handler:(() -> Void)?)` - Connect to the server. If it isn't connected after timeoutAfter seconds, the handler is called.
119119
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.
120120
10. `reconnect()` - Causes the client to reconnect to the server.
121+
11. `joinNamespace()` - Causes the client to join nsp. Shouldn't need to be called unless you change nsp manually.
122+
12. `leaveNamespace()` - Causes the client to leave the nsp and go back to /
121123

122124
Client Events
123125
------

SocketIOClientSwift/SocketEngine.swift

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
3434
private typealias Probe = (msg:String, type:PacketType, data:ContiguousArray<NSData>?)
3535
private typealias ProbeWaitQueue = [Probe]
3636

37+
private let allowedCharacterSet = NSCharacterSet(charactersInString: "!*'();:@&=+$,/?%#[]\" {}").invertedSet
3738
private let workQueue = NSOperationQueue()
3839
private let emitQueue = dispatch_queue_create("engineEmitQueue", DISPATCH_QUEUE_SERIAL)
3940
private let parseQueue = dispatch_queue_create("engineParseQueue", DISPATCH_QUEUE_SERIAL)
@@ -167,17 +168,16 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
167168
}
168169

169170
if params != nil {
170-
let allowedCharacterSet = NSCharacterSet(charactersInString: "!*'();:@&=+$,/?%#[]\" {}").invertedSet
171171

172172
for (key, value) in params! {
173173
let keyEsc = key.stringByAddingPercentEncodingWithAllowedCharacters(
174-
allowedCharacterSet)!
174+
self.allowedCharacterSet)!
175175
urlPolling += "&\(keyEsc)="
176176
urlWebSocket += "&\(keyEsc)="
177177

178178
if value is String {
179179
let valueEsc = (value as! String).stringByAddingPercentEncodingWithAllowedCharacters(
180-
allowedCharacterSet)!
180+
self.allowedCharacterSet)!
181181
urlPolling += "\(valueEsc)"
182182
urlWebSocket += "\(valueEsc)"
183183
} else {
@@ -219,7 +219,7 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
219219
if self.websocket || self.waitingForPoll || !self.connected {
220220
return
221221
}
222-
222+
223223
self.waitingForPoll = true
224224
let req = NSMutableURLRequest(URL: NSURL(string: self.urlPolling! + "&sid=\(self.sid)&b64=1")!)
225225

@@ -246,7 +246,7 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
246246
if this.polling {
247247
this.handlePollingFailed(err.localizedDescription)
248248
} else {
249-
NSLog(err.localizedDescription)
249+
SocketLogger.err(err.localizedDescription, client: this)
250250
}
251251
return
252252
}
@@ -263,7 +263,6 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
263263

264264
if this.fastUpgrade {
265265
this.doFastUpgrade()
266-
return
267266
} else if !this.closed && this.polling {
268267
this.doPoll()
269268
}

SocketIOClientSwift/SocketIOClient.swift

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,20 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketLogClient
404404
}
405405
}
406406

407-
func joinNamespace() {
407+
/**
408+
Leaves nsp and goes back to /
409+
*/
410+
public func leaveNamespace() {
411+
if self.nsp != "/" {
412+
self.engine?.send("1/\(self.nsp)", withData: nil)
413+
self.nsp = "/"
414+
}
415+
}
416+
417+
/**
418+
Joins nsp if it is not /
419+
*/
420+
public func joinNamespace() {
408421
SocketLogger.log("Joining namespace", client: self)
409422

410423
if self.nsp != "/" {

0 commit comments

Comments
 (0)