Skip to content

Commit 5607ed3

Browse files
committed
Consolidate WebSocket connection configuration logic.
1 parent 38fbbca commit 5607ed3

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

Sources/Models/WebSocket.swift

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,14 @@ open class WebSocket: NSObject, WebSocketConnection, URLSessionWebSocketDelegate
1919

2020
init(request: URLRequest, connectAutomatically: Bool = false) {
2121
super.init()
22-
urlSession = URLSession(configuration: .default,
23-
delegate: self,
24-
delegateQueue: delegateQueue)
25-
webSocketTask = urlSession.webSocketTask(with: request)
26-
27-
if connectAutomatically {
28-
connect()
29-
}
22+
webSocketRequest = request
23+
configureConnection(connectAutomatically: connectAutomatically)
3024
}
3125

3226
init(url: URL, connectAutomatically: Bool = false) {
3327
super.init()
34-
urlSession = URLSession(configuration: .default,
35-
delegate: self,
36-
delegateQueue: delegateQueue)
37-
webSocketTask = urlSession.webSocketTask(with: url)
38-
39-
if connectAutomatically {
40-
connect()
41-
}
28+
webSocketRequest = URLRequest(url: url)
29+
configureConnection(connectAutomatically: connectAutomatically)
4230
}
4331

4432
// MARK: - URLSessionWebSocketDelegate conformance
@@ -131,6 +119,16 @@ open class WebSocket: NSObject, WebSocketConnection, URLSessionWebSocketDelegate
131119

132120
// MARK: - Private methods
133121

122+
private func configureConnection(connectAutomatically: Bool) {
123+
urlSession = URLSession(configuration: .default,
124+
delegate: self,
125+
delegateQueue: delegateQueue)
126+
127+
if connectAutomatically {
128+
connect()
129+
}
130+
}
131+
134132
private func send(message: URLSessionWebSocketTask.Message) {
135133
webSocketTask?.send(message) { [weak self] error in
136134
guard let self = self else {

0 commit comments

Comments
 (0)