Skip to content

Commit d7d8903

Browse files
committed
Add ability to send payload with connect
1 parent ce4de49 commit d7d8903

File tree

6 files changed

+38
-18
lines changed

6 files changed

+38
-18
lines changed

Socket.IO-Client-Swift.podspec

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Pod::Spec.new do |s|
22
s.name = "Socket.IO-Client-Swift"
33
s.module_name = "SocketIO"
4-
s.version = "16.0.0-beta1"
4+
s.version = "16.0.0-beta2"
55
s.summary = "Socket.IO-client for iOS and OS X"
66
s.description = <<-DESC
77
Socket.IO-client for iOS and OS X.
@@ -18,7 +18,7 @@ Pod::Spec.new do |s|
1818
s.requires_arc = true
1919
s.source = {
2020
:git => "https://github.com/socketio/socket.io-client-swift.git",
21-
:tag => 'v16.0.0-beta1',
21+
:tag => 'v16.0.0-beta2',
2222
:submodules => true
2323
}
2424

Source/SocketIO/Client/SocketIOClient.swift

+12-7
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ open class SocketIOClient: NSObject, SocketIOClientSpec {
7979
public private(set) var sid: String?
8080

8181
let ackHandlers = SocketAckManager()
82+
var connectPayload: [String: Any]?
8283

8384
private(set) var currentAck = -1
8485

@@ -107,8 +108,8 @@ open class SocketIOClient: NSObject, SocketIOClientSpec {
107108
/// Connect to the server. The same as calling `connect(timeoutAfter:withHandler:)` with a timeout of 0.
108109
///
109110
/// Only call after adding your event listeners, unless you know what you're doing.
110-
open func connect() {
111-
connect(timeoutAfter: 0, withHandler: nil)
111+
open func connect(withPayload payload: [String: Any]? = nil) {
112+
connect(withPayload: payload, timeoutAfter: 0, withHandler: nil)
112113
}
113114

114115
/// Connect to the server. If we aren't connected after `timeoutAfter` seconds, then `withHandler` is called.
@@ -118,7 +119,7 @@ open class SocketIOClient: NSObject, SocketIOClientSpec {
118119
/// - parameter timeoutAfter: The number of seconds after which if we are not connected we assume the connection
119120
/// has failed. Pass 0 to never timeout.
120121
/// - parameter handler: The handler to call when the client fails to connect.
121-
open func connect(timeoutAfter: Double, withHandler handler: (() -> ())?) {
122+
open func connect(withPayload payload: [String: Any]? = nil, timeoutAfter: Double, withHandler handler: (() -> ())?) {
122123
assert(timeoutAfter >= 0, "Invalid timeout: \(timeoutAfter)")
123124

124125
guard let manager = self.manager, status != .connected else {
@@ -128,7 +129,7 @@ open class SocketIOClient: NSObject, SocketIOClientSpec {
128129

129130
status = .connecting
130131

131-
joinNamespace()
132+
joinNamespace(withPayload: payload)
132133

133134
guard timeoutAfter != 0 else { return }
134135

@@ -340,11 +341,15 @@ open class SocketIOClient: NSObject, SocketIOClientSpec {
340341
manager?.disconnectSocket(self)
341342
}
342343

343-
/// Joins `nsp`.
344-
open func joinNamespace() {
344+
/// Joins `nsp`. You shouldn't need to call this directly, instead call `connect`.
345+
///
346+
/// - Parameter payload: The optional
347+
open func joinNamespace(withPayload payload: [String: Any]? = nil) {
345348
DefaultSocketLogger.Logger.log("Joining namespace \(nsp)", type: logType)
346349

347-
manager?.connectSocket(self)
350+
connectPayload = payload
351+
352+
manager?.connectSocket(self, withPayload: connectPayload)
348353
}
349354

350355
/// Removes handler(s) for a client event.

Source/SocketIO/Client/SocketIOClientSpec.swift

+8-3
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,19 @@ public protocol SocketIOClientSpec : AnyObject {
6565
/// Connect to the server. The same as calling `connect(timeoutAfter:withHandler:)` with a timeout of 0.
6666
///
6767
/// Only call after adding your event listeners, unless you know what you're doing.
68-
func connect()
68+
///
69+
/// - parameter payload: An optional payload sent on connect
70+
func connect(withPayload payload: [String: Any]?)
6971

7072
/// Connect to the server. If we aren't connected after `timeoutAfter` seconds, then `withHandler` is called.
7173
///
7274
/// Only call after adding your event listeners, unless you know what you're doing.
7375
///
76+
/// - parameter payload: An optional payload sent on connect
7477
/// - parameter timeoutAfter: The number of seconds after which if we are not connected we assume the connection
7578
/// has failed. Pass 0 to never timeout.
7679
/// - parameter handler: The handler to call when the client fails to connect.
77-
func connect(timeoutAfter: Double, withHandler handler: (() -> ())?)
80+
func connect(withPayload payload: [String: Any]?, timeoutAfter: Double, withHandler handler: (() -> ())?)
7881

7982
/// Called when the client connects to a namespace. If the client was created with a namespace upfront,
8083
/// then this is only called when the client connects to that namespace.
@@ -162,7 +165,9 @@ public protocol SocketIOClientSpec : AnyObject {
162165
func leaveNamespace()
163166

164167
/// Joins `nsp`.
165-
func joinNamespace()
168+
///
169+
/// - Parameter withPayload: The payload to connect when joining this namespace
170+
func joinNamespace(withPayload payload: [String: Any]?)
166171

167172
/// Removes handler(s) for a client event.
168173
///

Source/SocketIO/Manager/SocketManager.swift

+12-3
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,8 @@ open class SocketManager: NSObject, SocketManagerSpec, SocketParsable, SocketDat
202202
/// Connects a socket through this manager's engine.
203203
///
204204
/// - parameter socket: The socket who we should connect through this manager.
205-
open func connectSocket(_ socket: SocketIOClient) {
205+
/// - parameter withPayload: Optional payload to send on connect
206+
open func connectSocket(_ socket: SocketIOClient, withPayload payload: [String: Any]? = nil) {
206207
guard status == .connected else {
207208
DefaultSocketLogger.Logger.log("Tried connecting socket when engine isn't open. Connecting",
208209
type: SocketManager.logType)
@@ -211,7 +212,15 @@ open class SocketManager: NSObject, SocketManagerSpec, SocketParsable, SocketDat
211212
return
212213
}
213214

214-
engine?.send("0\(socket.nsp),", withData: [])
215+
var payloadStr = ""
216+
217+
if payload != nil,
218+
let payloadData = try? JSONSerialization.data(withJSONObject: payload!, options: .fragmentsAllowed),
219+
let jsonString = String(data: payloadData, encoding: .utf8) {
220+
payloadStr = jsonString
221+
}
222+
223+
engine?.send("0\(socket.nsp),\(payloadStr)", withData: [])
215224
}
216225

217226
/// Called when the manager has disconnected from socket.io.
@@ -341,7 +350,7 @@ open class SocketManager: NSObject, SocketManagerSpec, SocketParsable, SocketDat
341350
status = .connected
342351

343352
for (_, socket) in nsps where socket.status == .connecting {
344-
connectSocket(socket)
353+
connectSocket(socket, withPayload: socket.connectPayload)
345354
}
346355
}
347356

Source/SocketIO/Manager/SocketManagerSpec.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ public protocol SocketManagerSpec : AnyObject, SocketEngineClient {
9191
/// Connects a socket through this manager's engine.
9292
///
9393
/// - parameter socket: The socket who we should connect through this manager.
94-
func connectSocket(_ socket: SocketIOClient)
94+
/// - parameter withPayload: Optional payload to send on connect
95+
func connectSocket(_ socket: SocketIOClient, withPayload: [String: Any]?)
9596

9697
/// Called when the manager has disconnected from socket.io.
9798
///

Tests/TestSocketIO/SocketSideEffectTest.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ class SocketSideEffectTest: XCTestCase {
454454
}
455455
}
456456

457-
struct ThrowingData : SocketData {
457+
struct ThrowingData: SocketData {
458458
enum ThrowingError : Error {
459459
case error
460460
}
@@ -465,7 +465,7 @@ struct ThrowingData : SocketData {
465465

466466
}
467467

468-
class TestEngine : SocketEngineSpec {
468+
class TestEngine: SocketEngineSpec {
469469
weak var client: SocketEngineClient?
470470
private(set) var closed = false
471471
private(set) var compress = false

0 commit comments

Comments
 (0)