Skip to content

Commit 5a5dc70

Browse files
committed
1 parent c049f62 commit 5a5dc70

File tree

2 files changed

+30
-29
lines changed

2 files changed

+30
-29
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ Constructors
8888

8989
Options
9090
-------
91+
- `connectParams: [String: AnyObject]?` - Dictionary whose contents will be passed with the connection.
9192
- `reconnects: Bool` Default is `true`
9293
- `reconnectAttempts: Int` Default is `-1` (infinite tries)
9394
- `reconnectWait: Int` Default is `10`
@@ -108,9 +109,8 @@ Methods
108109
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.
109110
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.
110111
7. `connect()` - Establishes a connection to the server. A "connect" event is fired upon successful connection.
111-
8. `connectWithParams(params:[String: AnyObject])` - Establishes a connection to the server passing the specified params. A "connect" event is fired upon successful connection.
112-
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.
113-
10. `reconnect()` - Causes the client to reconnect to the server.
112+
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.
113+
9. `reconnect()` - Causes the client to reconnect to the server.
114114

115115
Client Events
116116
------

SocketIOClientSwift/SocketIOClient.swift

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,13 @@
2525
import Foundation
2626

2727
public final class SocketIOClient: NSObject, SocketEngineClient, SocketLogClient {
28-
private lazy var params = [String: AnyObject]()
2928
private var anyHandler:((SocketAnyEvent) -> Void)?
3029
private var _closed = false
3130
private var _connected = false
3231
private var _connecting = false
3332
private var currentReconnectAttempt = 0
3433
private var handlers = ContiguousArray<SocketEventHandler>()
35-
private var paramConnect = false
34+
private var params:[String: AnyObject]?
3635
private var _secure = false
3736
private var _sid:String?
3837
private var _reconnecting = false
@@ -88,11 +87,16 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketLogClient
8887
self.socketURL = socketURL
8988
self.opts = opts
9089

90+
9191
// Set options
9292
if let sessionDelegate = opts?["sessionDelegate"] as? NSURLSessionDelegate {
9393
self.sessionDelegate = sessionDelegate
9494
}
9595

96+
if let connectParams = opts?["connectParams"] as? [String: AnyObject] {
97+
self.params = connectParams
98+
}
99+
96100
if let log = opts?["log"] as? Bool {
97101
self.log = log
98102
}
@@ -156,37 +160,38 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketLogClient
156160
Connect to the server.
157161
*/
158162
public func connect() {
159-
if self.closed {
160-
println("Warning! This socket was previously closed. This might be dangerous!")
161-
self._closed = false
162-
}
163-
164-
if self.connected {
165-
return
166-
}
167-
168-
self.addEngine()
169-
self.engine?.open()
163+
self.connect(timeoutAfter: 0, withTimeoutHandler: nil)
170164
}
171165

172166
/**
173-
Connect to the server with params that will be passed on connection.
167+
Connect to the server. If we aren't connected after timeoutAfter, call handler
174168
*/
175-
public func connectWithParams(params:[String: AnyObject]) {
169+
public func connect(#timeoutAfter:Int, withTimeoutHandler handler:(() -> Void)?) {
176170
if self.closed {
177-
println("Warning! This socket was previously closed. This might be dangerous!")
171+
SocketLogger.log("Warning! This socket was previously closed. This might be dangerous!", client: self)
178172
self._closed = false
173+
} else if self.connected {
174+
return
179175
}
180176

181-
if self.connected {
177+
self.addEngine()
178+
self.engine?.open(opts: self.params)
179+
180+
if timeoutAfter == 0 {
182181
return
183182
}
184183

185-
self.params = params
186-
self.paramConnect = true
184+
let time = dispatch_time(DISPATCH_TIME_NOW, Int64(timeoutAfter) * Int64(NSEC_PER_SEC))
187185

188-
self.addEngine()
189-
self.engine?.open(opts: params)
186+
dispatch_after(time, dispatch_get_main_queue()) {[weak self] in
187+
if let this = self where !this.connected {
188+
this._closed = true
189+
this._connecting = false
190+
this.engine?.close(fast: true)
191+
192+
handler?()
193+
}
194+
}
190195
}
191196

192197
private func createOnAck(event:String, items:[AnyObject]) -> OnAckCallback {
@@ -489,10 +494,6 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketLogClient
489494
isInternalMessage: true)
490495

491496
self.currentReconnectAttempt++
492-
if self.paramConnect {
493-
self.connectWithParams(self.params)
494-
} else {
495-
self.connect()
496-
}
497+
self.connect()
497498
}
498499
}

0 commit comments

Comments
 (0)