Skip to content

Commit 321bb18

Browse files
committed
Allow changing config after init but before connect. Fixes socketio#680
1 parent 91eea96 commit 321bb18

File tree

3 files changed

+67
-24
lines changed

3 files changed

+67
-24
lines changed

SocketIO-MacTests/SocketSideEffectTest.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,25 @@ class SocketSideEffectTest: XCTestCase {
351351
waitForExpectations(timeout: 0.2)
352352
}
353353

354+
func testSettingConfigAfterInit() {
355+
socket.setTestStatus(.notConnected)
356+
socket.config.insert(.log(true))
357+
358+
XCTAssertTrue(DefaultSocketLogger.Logger.log, "It should set logging to true after creation")
359+
360+
socket.config = [.log(false), .nsp("/test")]
361+
362+
XCTAssertFalse(DefaultSocketLogger.Logger.log, "It should set logging to false after creation")
363+
XCTAssertEqual(socket.nsp, "/test", "It should set the namespace after creation")
364+
}
365+
366+
func testSettingConfigAfterInitWhenConnectedIgnoresChanges() {
367+
socket.config = [.log(true), .nsp("/test")]
368+
369+
XCTAssertFalse(DefaultSocketLogger.Logger.log, "It should set logging to false after creation")
370+
XCTAssertEqual(socket.nsp, "/", "It should set the namespace after creation")
371+
}
372+
354373
let data = "test".data(using: String.Encoding.utf8)!
355374
let data2 = "test2".data(using: String.Encoding.utf8)!
356375
private var socket: SocketIOClient!

Source/SocketIO/Client/SocketIOClient.swift

Lines changed: 45 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,24 @@ open class SocketIOClient : NSObject, SocketIOClientSpec, SocketEngineClient, So
5151
public var nsp = "/"
5252

5353
/// The configuration for this client.
54-
public var config: SocketIOClientConfiguration
54+
///
55+
/// **This cannot be set after calling one of the connect methods**.
56+
public var config: SocketIOClientConfiguration {
57+
didSet {
58+
guard status == .notConnected else {
59+
DefaultSocketLogger.Logger.error("Tried setting config after calling connect",
60+
type: SocketIOClient.logType)
61+
return
62+
}
63+
64+
if socketURL.absoluteString.hasPrefix("https://") {
65+
config.insert(.secure(true))
66+
}
67+
68+
config.insert(.path("/socket.io/"), replacing: false)
69+
setConfigs()
70+
}
71+
}
5572

5673
/// If `true`, this client will try and reconnect on any disconnects.
5774
@objc
@@ -130,32 +147,11 @@ open class SocketIOClient : NSObject, SocketIOClientSpec, SocketEngineClient, So
130147
self.config.insert(.secure(true))
131148
}
132149

133-
for option in config {
134-
switch option {
135-
case let .reconnects(reconnects):
136-
self.reconnects = reconnects
137-
case let .reconnectAttempts(attempts):
138-
reconnectAttempts = attempts
139-
case let .reconnectWait(wait):
140-
reconnectWait = abs(wait)
141-
case let .nsp(nsp):
142-
self.nsp = nsp
143-
case let .log(log):
144-
DefaultSocketLogger.Logger.log = log
145-
case let .logger(logger):
146-
DefaultSocketLogger.Logger = logger
147-
case let .handleQueue(queue):
148-
handleQueue = queue
149-
case let .forceNew(force):
150-
forceNew = force
151-
default:
152-
continue
153-
}
154-
}
155-
156150
self.config.insert(.path("/socket.io/"), replacing: false)
157151

158152
super.init()
153+
154+
setConfigs()
159155
}
160156

161157
/// 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
670666
handleQueue.asyncAfter(deadline: DispatchTime.now() + Double(reconnectWait), execute: _tryReconnect)
671667
}
672668

669+
private func setConfigs() {
670+
for option in config {
671+
switch option {
672+
case let .reconnects(reconnects):
673+
self.reconnects = reconnects
674+
case let .reconnectAttempts(attempts):
675+
reconnectAttempts = attempts
676+
case let .reconnectWait(wait):
677+
reconnectWait = abs(wait)
678+
case let .nsp(nsp):
679+
self.nsp = nsp
680+
case let .log(log):
681+
DefaultSocketLogger.Logger.log = log
682+
case let .logger(logger):
683+
DefaultSocketLogger.Logger = logger
684+
case let .handleQueue(queue):
685+
handleQueue = queue
686+
case let .forceNew(force):
687+
forceNew = force
688+
default:
689+
continue
690+
}
691+
}
692+
}
693+
673694
// Test properties
674695

675696
var testHandlers: [SocketEventHandler] {

Source/SocketIO/Client/SocketIOClientSpec.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ public protocol SocketIOClientSpec : class {
3232
/// A handler that will be called on any event.
3333
var anyHandler: ((SocketAnyEvent) -> ())? { get }
3434

35+
/// The configuration for this client.
36+
var config: SocketIOClientConfiguration { get set }
37+
3538
/// The queue that all interaction with the client must be on.
3639
var handleQueue: DispatchQueue { get set }
3740

0 commit comments

Comments
 (0)