This commit is contained in:
Erik 2015-05-09 15:20:03 -04:00
parent c049f62028
commit 5a5dc70203
2 changed files with 32 additions and 31 deletions

View File

@ -88,6 +88,7 @@ Constructors
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`
@ -108,9 +109,8 @@ Methods
5. `emitWithAck(event:String, _ items:AnyObject...) -> (timeout:UInt64, callback:(NSArray?) -> Void) -> Void` - Sends a message that requests an acknowledgement from the server. Returns a function which you can use to add a handler. See example. Note: The message is not sent until you call the returned function.
6. `emitWithAck(event:String, withItems items:[AnyObject]) -> (UInt64, (NSArray?) -> Void) -> Void` - `emitWithAck` for Objective-C. Note: The message is not sent until you call the returned function.
7. `connect()` - Establishes a connection to the server. A "connect" event is fired upon successful connection.
8. `connectWithParams(params:[String: AnyObject])` - Establishes a connection to the server passing the specified params. A "connect" event is fired upon successful connection.
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.
8. `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.
9. `reconnect()` - Causes the client to reconnect to the server.
Client Events
------

View File

@ -25,14 +25,13 @@
import Foundation
public final class SocketIOClient: NSObject, SocketEngineClient, SocketLogClient {
private lazy var params = [String: AnyObject]()
private var anyHandler:((SocketAnyEvent) -> Void)?
private var _closed = false
private var _connected = false
private var _connecting = false
private var currentReconnectAttempt = 0
private var handlers = ContiguousArray<SocketEventHandler>()
private var paramConnect = false
private var params:[String: AnyObject]?
private var _secure = false
private var _sid:String?
private var _reconnecting = false
@ -88,11 +87,16 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketLogClient
self.socketURL = socketURL
self.opts = opts
// Set options
if let sessionDelegate = opts?["sessionDelegate"] as? NSURLSessionDelegate {
self.sessionDelegate = sessionDelegate
}
if let connectParams = opts?["connectParams"] as? [String: AnyObject] {
self.params = connectParams
}
if let log = opts?["log"] as? Bool {
self.log = log
}
@ -156,37 +160,38 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketLogClient
Connect to the server.
*/
public func connect() {
if self.closed {
println("Warning! This socket was previously closed. This might be dangerous!")
self._closed = false
}
if self.connected {
return
}
self.addEngine()
self.engine?.open()
self.connect(timeoutAfter: 0, withTimeoutHandler: nil)
}
/**
Connect to the server with params that will be passed on connection.
Connect to the server. If we aren't connected after timeoutAfter, call handler
*/
public func connectWithParams(params:[String: AnyObject]) {
public func connect(#timeoutAfter:Int, withTimeoutHandler handler:(() -> Void)?) {
if self.closed {
println("Warning! This socket was previously closed. This might be dangerous!")
SocketLogger.log("Warning! This socket was previously closed. This might be dangerous!", client: self)
self._closed = false
}
if self.connected {
} else if self.connected {
return
}
self.params = params
self.paramConnect = true
self.addEngine()
self.engine?.open(opts: params)
self.engine?.open(opts: self.params)
if timeoutAfter == 0 {
return
}
let time = dispatch_time(DISPATCH_TIME_NOW, Int64(timeoutAfter) * Int64(NSEC_PER_SEC))
dispatch_after(time, dispatch_get_main_queue()) {[weak self] in
if let this = self where !this.connected {
this._closed = true
this._connecting = false
this.engine?.close(fast: true)
handler?()
}
}
}
private func createOnAck(event:String, items:[AnyObject]) -> OnAckCallback {
@ -489,10 +494,6 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketLogClient
isInternalMessage: true)
self.currentReconnectAttempt++
if self.paramConnect {
self.connectWithParams(self.params)
} else {
self.connect()
}
self.connect()
}
}