Add ability to send payload with connect
This commit is contained in:
parent
ce4de49d50
commit
86be3540f0
@ -1,7 +1,7 @@
|
||||
Pod::Spec.new do |s|
|
||||
s.name = "Socket.IO-Client-Swift"
|
||||
s.module_name = "SocketIO"
|
||||
s.version = "16.0.0-beta1"
|
||||
s.version = "16.0.0-beta2"
|
||||
s.summary = "Socket.IO-client for iOS and OS X"
|
||||
s.description = <<-DESC
|
||||
Socket.IO-client for iOS and OS X.
|
||||
@ -18,7 +18,7 @@ Pod::Spec.new do |s|
|
||||
s.requires_arc = true
|
||||
s.source = {
|
||||
:git => "https://github.com/socketio/socket.io-client-swift.git",
|
||||
:tag => 'v16.0.0-beta1',
|
||||
:tag => 'v16.0.0-beta2',
|
||||
:submodules => true
|
||||
}
|
||||
|
||||
|
||||
@ -79,6 +79,7 @@ open class SocketIOClient: NSObject, SocketIOClientSpec {
|
||||
public private(set) var sid: String?
|
||||
|
||||
let ackHandlers = SocketAckManager()
|
||||
var connectPayload: [String: Any]?
|
||||
|
||||
private(set) var currentAck = -1
|
||||
|
||||
@ -107,8 +108,8 @@ open class SocketIOClient: NSObject, SocketIOClientSpec {
|
||||
/// Connect to the server. The same as calling `connect(timeoutAfter:withHandler:)` with a timeout of 0.
|
||||
///
|
||||
/// Only call after adding your event listeners, unless you know what you're doing.
|
||||
open func connect() {
|
||||
connect(timeoutAfter: 0, withHandler: nil)
|
||||
open func connect(withPayload payload: [String: Any]? = nil) {
|
||||
connect(withPayload: payload, timeoutAfter: 0, withHandler: nil)
|
||||
}
|
||||
|
||||
/// 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 {
|
||||
/// - parameter timeoutAfter: The number of seconds after which if we are not connected we assume the connection
|
||||
/// has failed. Pass 0 to never timeout.
|
||||
/// - parameter handler: The handler to call when the client fails to connect.
|
||||
open func connect(timeoutAfter: Double, withHandler handler: (() -> ())?) {
|
||||
open func connect(withPayload payload: [String: Any]? = nil, timeoutAfter: Double, withHandler handler: (() -> ())?) {
|
||||
assert(timeoutAfter >= 0, "Invalid timeout: \(timeoutAfter)")
|
||||
|
||||
guard let manager = self.manager, status != .connected else {
|
||||
@ -128,7 +129,7 @@ open class SocketIOClient: NSObject, SocketIOClientSpec {
|
||||
|
||||
status = .connecting
|
||||
|
||||
joinNamespace()
|
||||
joinNamespace(withPayload: payload)
|
||||
|
||||
guard timeoutAfter != 0 else { return }
|
||||
|
||||
@ -340,11 +341,15 @@ open class SocketIOClient: NSObject, SocketIOClientSpec {
|
||||
manager?.disconnectSocket(self)
|
||||
}
|
||||
|
||||
/// Joins `nsp`.
|
||||
open func joinNamespace() {
|
||||
/// Joins `nsp`. You shouldn't need to call this directly, instead call `connect`.
|
||||
///
|
||||
/// - Parameter payload: The optional
|
||||
open func joinNamespace(withPayload payload: [String: Any]? = nil) {
|
||||
DefaultSocketLogger.Logger.log("Joining namespace \(nsp)", type: logType)
|
||||
|
||||
manager?.connectSocket(self)
|
||||
connectPayload = payload
|
||||
|
||||
manager?.connectSocket(self, withPayload: connectPayload)
|
||||
}
|
||||
|
||||
/// Removes handler(s) for a client event.
|
||||
|
||||
@ -65,16 +65,19 @@ public protocol SocketIOClientSpec : AnyObject {
|
||||
/// Connect to the server. The same as calling `connect(timeoutAfter:withHandler:)` with a timeout of 0.
|
||||
///
|
||||
/// Only call after adding your event listeners, unless you know what you're doing.
|
||||
func connect()
|
||||
///
|
||||
/// - parameter payload: An optional payload sent on connect
|
||||
func connect(withPayload payload: [String: Any]?)
|
||||
|
||||
/// Connect to the server. If we aren't connected after `timeoutAfter` seconds, then `withHandler` is called.
|
||||
///
|
||||
/// Only call after adding your event listeners, unless you know what you're doing.
|
||||
///
|
||||
/// - parameter payload: An optional payload sent on connect
|
||||
/// - parameter timeoutAfter: The number of seconds after which if we are not connected we assume the connection
|
||||
/// has failed. Pass 0 to never timeout.
|
||||
/// - parameter handler: The handler to call when the client fails to connect.
|
||||
func connect(timeoutAfter: Double, withHandler handler: (() -> ())?)
|
||||
func connect(withPayload payload: [String: Any]?, timeoutAfter: Double, withHandler handler: (() -> ())?)
|
||||
|
||||
/// Called when the client connects to a namespace. If the client was created with a namespace upfront,
|
||||
/// then this is only called when the client connects to that namespace.
|
||||
@ -162,7 +165,9 @@ public protocol SocketIOClientSpec : AnyObject {
|
||||
func leaveNamespace()
|
||||
|
||||
/// Joins `nsp`.
|
||||
func joinNamespace()
|
||||
///
|
||||
/// - Parameter withPayload: The payload to connect when joining this namespace
|
||||
func joinNamespace(withPayload payload: [String: Any]?)
|
||||
|
||||
/// Removes handler(s) for a client event.
|
||||
///
|
||||
|
||||
@ -202,7 +202,8 @@ open class SocketManager: NSObject, SocketManagerSpec, SocketParsable, SocketDat
|
||||
/// Connects a socket through this manager's engine.
|
||||
///
|
||||
/// - parameter socket: The socket who we should connect through this manager.
|
||||
open func connectSocket(_ socket: SocketIOClient) {
|
||||
/// - parameter withPayload: Optional payload to send on connect
|
||||
open func connectSocket(_ socket: SocketIOClient, withPayload payload: [String: Any]? = nil) {
|
||||
guard status == .connected else {
|
||||
DefaultSocketLogger.Logger.log("Tried connecting socket when engine isn't open. Connecting",
|
||||
type: SocketManager.logType)
|
||||
@ -211,7 +212,15 @@ open class SocketManager: NSObject, SocketManagerSpec, SocketParsable, SocketDat
|
||||
return
|
||||
}
|
||||
|
||||
engine?.send("0\(socket.nsp),", withData: [])
|
||||
var payloadStr = ""
|
||||
|
||||
if payload != nil,
|
||||
let payloadData = try? JSONSerialization.data(withJSONObject: payload!, options: .fragmentsAllowed),
|
||||
let jsonString = String(data: payloadData, encoding: .utf8) {
|
||||
payloadStr = jsonString
|
||||
}
|
||||
|
||||
engine?.send("0\(socket.nsp),\(payloadStr)", withData: [])
|
||||
}
|
||||
|
||||
/// Called when the manager has disconnected from socket.io.
|
||||
@ -341,7 +350,7 @@ open class SocketManager: NSObject, SocketManagerSpec, SocketParsable, SocketDat
|
||||
status = .connected
|
||||
|
||||
for (_, socket) in nsps where socket.status == .connecting {
|
||||
connectSocket(socket)
|
||||
connectSocket(socket, withPayload: socket.connectPayload)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -91,7 +91,8 @@ public protocol SocketManagerSpec : AnyObject, SocketEngineClient {
|
||||
/// Connects a socket through this manager's engine.
|
||||
///
|
||||
/// - parameter socket: The socket who we should connect through this manager.
|
||||
func connectSocket(_ socket: SocketIOClient)
|
||||
/// - parameter withPayload: Optional payload to send on connect
|
||||
func connectSocket(_ socket: SocketIOClient, withPayload: [String: Any]?)
|
||||
|
||||
/// Called when the manager has disconnected from socket.io.
|
||||
///
|
||||
|
||||
@ -454,7 +454,7 @@ class SocketSideEffectTest: XCTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
struct ThrowingData : SocketData {
|
||||
struct ThrowingData: SocketData {
|
||||
enum ThrowingError : Error {
|
||||
case error
|
||||
}
|
||||
@ -465,7 +465,7 @@ struct ThrowingData : SocketData {
|
||||
|
||||
}
|
||||
|
||||
class TestEngine : SocketEngineSpec {
|
||||
class TestEngine: SocketEngineSpec {
|
||||
weak var client: SocketEngineClient?
|
||||
private(set) var closed = false
|
||||
private(set) var compress = false
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user