Skip to content

Commit 30e4f59

Browse files
committed
Allow changing the socketURL post init Fixes #470
1 parent 17bec21 commit 30e4f59

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

Source/SocketEnginePollable.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ extension SocketEnginePollable {
147147

148148
DefaultSocketLogger.Logger.log("Got polling response", type: "SocketEnginePolling")
149149

150-
if let str = String(data: data!, encoding: String.Encoding.utf8) {
150+
if let str = String(data: data!, encoding: .utf8) {
151151
this.parsePollingMessage(str)
152152
}
153153

Source/SocketIOClient.swift

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,6 @@ import Foundation
3333
open class SocketIOClient : NSObject, SocketIOClientSpec, SocketEngineClient, SocketParsable {
3434
// MARK: Properties
3535

36-
/// The URL of the socket.io server. This is set in the initializer.
37-
public let socketURL: URL
38-
3936
/// The engine for this client.
4037
public private(set) var engine: SocketEngineSpec?
4138

@@ -78,19 +75,25 @@ open class SocketIOClient : NSObject, SocketIOClientSpec, SocketEngineClient, So
7875
return engine?.sid
7976
}
8077

78+
/// The URL of the socket.io server.
79+
///
80+
/// If changed after calling `init`, `forceNew` must be set to `true`, or it will only connect to the url set in the
81+
/// init.
82+
public var socketURL: URL
83+
84+
var ackHandlers = SocketAckManager()
85+
var waitingPackets = [SocketPacket]()
86+
87+
private(set) var currentAck = -1
88+
private(set) var reconnectAttempts = -1
89+
8190
private let logType = "SocketIOClient"
8291

8392
private var anyHandler: ((SocketAnyEvent) -> Void)?
8493
private var currentReconnectAttempt = 0
8594
private var handlers = [SocketEventHandler]()
8695
private var reconnecting = false
8796

88-
private(set) var currentAck = -1
89-
private(set) var reconnectAttempts = -1
90-
91-
var ackHandlers = SocketAckManager()
92-
var waitingPackets = [SocketPacket]()
93-
9497
// MARK: Initializers
9598

9699
/// Type safe way to create a new SocketIOClient. `opts` can be omitted.
@@ -149,13 +152,11 @@ open class SocketIOClient : NSObject, SocketIOClientSpec, SocketEngineClient, So
149152

150153
// MARK: Methods
151154

152-
private func addEngine() -> SocketEngineSpec {
155+
private func addEngine() {
153156
DefaultSocketLogger.Logger.log("Adding engine", type: logType, args: "")
154157

155158
engine?.client = nil
156159
engine = SocketEngine(client: self, url: socketURL, config: config)
157-
158-
return engine!
159160
}
160161

161162
/// Connect to the server.
@@ -179,11 +180,11 @@ open class SocketIOClient : NSObject, SocketIOClientSpec, SocketEngineClient, So
179180
status = .connecting
180181

181182
if engine == nil || forceNew {
182-
addEngine().connect()
183-
} else {
184-
engine?.connect()
183+
addEngine()
185184
}
186185

186+
engine?.connect()
187+
187188
guard timeoutAfter != 0 else { return }
188189

189190
handleQueue.asyncAfter(deadline: DispatchTime.now() + Double(timeoutAfter)) {[weak self] in

0 commit comments

Comments
 (0)