@@ -19,26 +19,14 @@ open class WebSocket: NSObject, WebSocketConnection, URLSessionWebSocketDelegate
19
19
20
20
init ( request: URLRequest , connectAutomatically: Bool = false ) {
21
21
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)
30
24
}
31
25
32
26
init ( url: URL , connectAutomatically: Bool = false ) {
33
27
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)
42
30
}
43
31
44
32
// MARK: - URLSessionWebSocketDelegate conformance
@@ -131,6 +119,16 @@ open class WebSocket: NSObject, WebSocketConnection, URLSessionWebSocketDelegate
131
119
132
120
// MARK: - Private methods
133
121
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
+
134
132
private func send( message: URLSessionWebSocketTask . Message ) {
135
133
webSocketTask? . send ( message) { [ weak self] error in
136
134
guard let self = self else {
0 commit comments