Allow changing config after init but before connect. Fixes #680
This commit is contained in:
parent
91eea96308
commit
321bb186df
@ -351,6 +351,25 @@ class SocketSideEffectTest: XCTestCase {
|
|||||||
waitForExpectations(timeout: 0.2)
|
waitForExpectations(timeout: 0.2)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func testSettingConfigAfterInit() {
|
||||||
|
socket.setTestStatus(.notConnected)
|
||||||
|
socket.config.insert(.log(true))
|
||||||
|
|
||||||
|
XCTAssertTrue(DefaultSocketLogger.Logger.log, "It should set logging to true after creation")
|
||||||
|
|
||||||
|
socket.config = [.log(false), .nsp("/test")]
|
||||||
|
|
||||||
|
XCTAssertFalse(DefaultSocketLogger.Logger.log, "It should set logging to false after creation")
|
||||||
|
XCTAssertEqual(socket.nsp, "/test", "It should set the namespace after creation")
|
||||||
|
}
|
||||||
|
|
||||||
|
func testSettingConfigAfterInitWhenConnectedIgnoresChanges() {
|
||||||
|
socket.config = [.log(true), .nsp("/test")]
|
||||||
|
|
||||||
|
XCTAssertFalse(DefaultSocketLogger.Logger.log, "It should set logging to false after creation")
|
||||||
|
XCTAssertEqual(socket.nsp, "/", "It should set the namespace after creation")
|
||||||
|
}
|
||||||
|
|
||||||
let data = "test".data(using: String.Encoding.utf8)!
|
let data = "test".data(using: String.Encoding.utf8)!
|
||||||
let data2 = "test2".data(using: String.Encoding.utf8)!
|
let data2 = "test2".data(using: String.Encoding.utf8)!
|
||||||
private var socket: SocketIOClient!
|
private var socket: SocketIOClient!
|
||||||
|
|||||||
@ -51,7 +51,24 @@ open class SocketIOClient : NSObject, SocketIOClientSpec, SocketEngineClient, So
|
|||||||
public var nsp = "/"
|
public var nsp = "/"
|
||||||
|
|
||||||
/// The configuration for this client.
|
/// The configuration for this client.
|
||||||
public var config: SocketIOClientConfiguration
|
///
|
||||||
|
/// **This cannot be set after calling one of the connect methods**.
|
||||||
|
public var config: SocketIOClientConfiguration {
|
||||||
|
didSet {
|
||||||
|
guard status == .notConnected else {
|
||||||
|
DefaultSocketLogger.Logger.error("Tried setting config after calling connect",
|
||||||
|
type: SocketIOClient.logType)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if socketURL.absoluteString.hasPrefix("https://") {
|
||||||
|
config.insert(.secure(true))
|
||||||
|
}
|
||||||
|
|
||||||
|
config.insert(.path("/socket.io/"), replacing: false)
|
||||||
|
setConfigs()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// If `true`, this client will try and reconnect on any disconnects.
|
/// If `true`, this client will try and reconnect on any disconnects.
|
||||||
@objc
|
@objc
|
||||||
@ -130,32 +147,11 @@ open class SocketIOClient : NSObject, SocketIOClientSpec, SocketEngineClient, So
|
|||||||
self.config.insert(.secure(true))
|
self.config.insert(.secure(true))
|
||||||
}
|
}
|
||||||
|
|
||||||
for option in config {
|
|
||||||
switch option {
|
|
||||||
case let .reconnects(reconnects):
|
|
||||||
self.reconnects = reconnects
|
|
||||||
case let .reconnectAttempts(attempts):
|
|
||||||
reconnectAttempts = attempts
|
|
||||||
case let .reconnectWait(wait):
|
|
||||||
reconnectWait = abs(wait)
|
|
||||||
case let .nsp(nsp):
|
|
||||||
self.nsp = nsp
|
|
||||||
case let .log(log):
|
|
||||||
DefaultSocketLogger.Logger.log = log
|
|
||||||
case let .logger(logger):
|
|
||||||
DefaultSocketLogger.Logger = logger
|
|
||||||
case let .handleQueue(queue):
|
|
||||||
handleQueue = queue
|
|
||||||
case let .forceNew(force):
|
|
||||||
forceNew = force
|
|
||||||
default:
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
self.config.insert(.path("/socket.io/"), replacing: false)
|
self.config.insert(.path("/socket.io/"), replacing: false)
|
||||||
|
|
||||||
super.init()
|
super.init()
|
||||||
|
|
||||||
|
setConfigs()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Not so type safe way to create a SocketIOClient, meant for Objective-C compatiblity.
|
/// Not so type safe way to create a SocketIOClient, meant for Objective-C compatiblity.
|
||||||
@ -670,6 +666,31 @@ open class SocketIOClient : NSObject, SocketIOClientSpec, SocketEngineClient, So
|
|||||||
handleQueue.asyncAfter(deadline: DispatchTime.now() + Double(reconnectWait), execute: _tryReconnect)
|
handleQueue.asyncAfter(deadline: DispatchTime.now() + Double(reconnectWait), execute: _tryReconnect)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private func setConfigs() {
|
||||||
|
for option in config {
|
||||||
|
switch option {
|
||||||
|
case let .reconnects(reconnects):
|
||||||
|
self.reconnects = reconnects
|
||||||
|
case let .reconnectAttempts(attempts):
|
||||||
|
reconnectAttempts = attempts
|
||||||
|
case let .reconnectWait(wait):
|
||||||
|
reconnectWait = abs(wait)
|
||||||
|
case let .nsp(nsp):
|
||||||
|
self.nsp = nsp
|
||||||
|
case let .log(log):
|
||||||
|
DefaultSocketLogger.Logger.log = log
|
||||||
|
case let .logger(logger):
|
||||||
|
DefaultSocketLogger.Logger = logger
|
||||||
|
case let .handleQueue(queue):
|
||||||
|
handleQueue = queue
|
||||||
|
case let .forceNew(force):
|
||||||
|
forceNew = force
|
||||||
|
default:
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Test properties
|
// Test properties
|
||||||
|
|
||||||
var testHandlers: [SocketEventHandler] {
|
var testHandlers: [SocketEventHandler] {
|
||||||
|
|||||||
@ -32,6 +32,9 @@ public protocol SocketIOClientSpec : class {
|
|||||||
/// A handler that will be called on any event.
|
/// A handler that will be called on any event.
|
||||||
var anyHandler: ((SocketAnyEvent) -> ())? { get }
|
var anyHandler: ((SocketAnyEvent) -> ())? { get }
|
||||||
|
|
||||||
|
/// The configuration for this client.
|
||||||
|
var config: SocketIOClientConfiguration { get set }
|
||||||
|
|
||||||
/// The queue that all interaction with the client must be on.
|
/// The queue that all interaction with the client must be on.
|
||||||
var handleQueue: DispatchQueue { get set }
|
var handleQueue: DispatchQueue { get set }
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user