Skip to content

Support socket.io 3 + starscream 4 #1309

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Feb 1, 2021
Prev Previous commit
Next Next commit
Add ability to send payload with connect
  • Loading branch information
nuclearace committed Nov 7, 2020
commit d7d8903feed53e18ff346a719bc16219d731545c
4 changes: 2 additions & 2 deletions Socket.IO-Client-Swift.podspec
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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
}

Expand Down
19 changes: 12 additions & 7 deletions Source/SocketIO/Client/SocketIOClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.
Expand All @@ -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 {
Expand All @@ -128,7 +129,7 @@ open class SocketIOClient: NSObject, SocketIOClientSpec {

status = .connecting

joinNamespace()
joinNamespace(withPayload: payload)

guard timeoutAfter != 0 else { return }

Expand Down Expand Up @@ -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.
Expand Down
11 changes: 8 additions & 3 deletions Source/SocketIO/Client/SocketIOClientSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
///
Expand Down
15 changes: 12 additions & 3 deletions Source/SocketIO/Manager/SocketManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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.
Expand Down Expand Up @@ -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)
}
}

Expand Down
3 changes: 2 additions & 1 deletion Source/SocketIO/Manager/SocketManagerSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down
4 changes: 2 additions & 2 deletions Tests/TestSocketIO/SocketSideEffectTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ class SocketSideEffectTest: XCTestCase {
}
}

struct ThrowingData : SocketData {
struct ThrowingData: SocketData {
enum ThrowingError : Error {
case error
}
Expand All @@ -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
Expand Down