-
-
Notifications
You must be signed in to change notification settings - Fork 79
/
WebSocket+Connect.swift
200 lines (194 loc) · 8.2 KB
/
WebSocket+Connect.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import NIOCore
import NIOHTTP1
import Foundation
extension WebSocket {
/// Establish a WebSocket connection.
///
/// - Parameters:
/// - url: URL for the WebSocket server.
/// - headers: Headers to send to the WebSocket server.
/// - configuration: Configuration for the WebSocket client.
/// - eventLoopGroup: Event loop group to be used by the WebSocket client.
/// - onUpgrade: An escaping closure to be executed after the upgrade is completed by `NIOWebSocketClientUpgrader`.
/// - Returns: A future which completes when the connection to the WebSocket server is established.
@preconcurrency
public static func connect(
to url: String,
headers: HTTPHeaders = [:],
configuration: WebSocketClient.Configuration = .init(),
on eventLoopGroup: EventLoopGroup,
onUpgrade: @Sendable @escaping (WebSocket) -> ()
) -> EventLoopFuture<Void> {
guard let url = URL(string: url) else {
return eventLoopGroup.any().makeFailedFuture(WebSocketClient.Error.invalidURL)
}
return self.connect(
to: url,
headers: headers,
configuration: configuration,
on: eventLoopGroup,
onUpgrade: onUpgrade
)
}
/// Establish a WebSocket connection.
///
/// - Parameters:
/// - url: URL for the WebSocket server.
/// - headers: Headers to send to the WebSocket server.
/// - configuration: Configuration for the WebSocket client.
/// - eventLoopGroup: Event loop group to be used by the WebSocket client.
/// - onUpgrade: An escaping closure to be executed after the upgrade is completed by `NIOWebSocketClientUpgrader`.
/// - Returns: A future which completes when the connection to the WebSocket server is established.
@preconcurrency
public static func connect(
to url: URL,
headers: HTTPHeaders = [:],
configuration: WebSocketClient.Configuration = .init(),
on eventLoopGroup: EventLoopGroup,
onUpgrade: @Sendable @escaping (WebSocket) -> ()
) -> EventLoopFuture<Void> {
let scheme = url.scheme ?? "ws"
return self.connect(
scheme: scheme,
host: url.host ?? "localhost",
port: url.port ?? (scheme == "wss" ? 443 : 80),
path: url.path,
query: url.query,
headers: headers,
configuration: configuration,
on: eventLoopGroup,
onUpgrade: onUpgrade
)
}
/// Establish a WebSocket connection.
///
/// - Parameters:
/// - scheme: Scheme component of the URI for the WebSocket server.
/// - host: Host component of the URI for the WebSocket server.
/// - port: Port on which to connect to the WebSocket server.
/// - path: Path component of the URI for the WebSocket server.
/// - query: Query component of the URI for the WebSocket server.
/// - headers: Headers to send to the WebSocket server.
/// - configuration: Configuration for the WebSocket client.
/// - eventLoopGroup: Event loop group to be used by the WebSocket client.
/// - onUpgrade: An escaping closure to be executed after the upgrade is completed by `NIOWebSocketClientUpgrader`.
/// - Returns: A future which completes when the connection to the WebSocket server is established.
@preconcurrency
public static func connect(
scheme: String = "ws",
host: String,
port: Int = 80,
path: String = "/",
query: String? = nil,
headers: HTTPHeaders = [:],
configuration: WebSocketClient.Configuration = .init(),
on eventLoopGroup: EventLoopGroup,
onUpgrade: @Sendable @escaping (WebSocket) -> ()
) -> EventLoopFuture<Void> {
return WebSocketClient(
eventLoopGroupProvider: .shared(eventLoopGroup),
configuration: configuration
).connect(
scheme: scheme,
host: host,
port: port,
path: path,
query: query,
headers: headers,
onUpgrade: onUpgrade
)
}
/// Establish a WebSocket connection via a proxy server.
///
/// - Parameters:
/// - scheme: Scheme component of the URI for the origin server.
/// - host: Host component of the URI for the origin server.
/// - port: Port on which to connect to the origin server.
/// - path: Path component of the URI for the origin server.
/// - query: Query component of the URI for the origin server.
/// - headers: Headers to send to the origin server.
/// - proxy: Host component of the URI for the proxy server.
/// - proxyPort: Port on which to connect to the proxy server.
/// - proxyHeaders: Headers to send to the proxy server.
/// - proxyConnectDeadline: Deadline for establishing the proxy connection.
/// - configuration: Configuration for the WebSocket client.
/// - eventLoopGroup: Event loop group to be used by the WebSocket client.
/// - onUpgrade: An escaping closure to be executed after the upgrade is completed by `NIOWebSocketClientUpgrader`.
/// - Returns: A future which completes when the connection to the origin server is established.
@preconcurrency
public static func connect(
scheme: String = "ws",
host: String,
port: Int = 80,
path: String = "/",
query: String? = nil,
headers: HTTPHeaders = [:],
proxy: String?,
proxyPort: Int? = nil,
proxyHeaders: HTTPHeaders = [:],
proxyConnectDeadline: NIODeadline = NIODeadline.distantFuture,
configuration: WebSocketClient.Configuration = .init(),
on eventLoopGroup: EventLoopGroup,
onUpgrade: @Sendable @escaping (WebSocket) -> ()
) -> EventLoopFuture<Void> {
return WebSocketClient(
eventLoopGroupProvider: .shared(eventLoopGroup),
configuration: configuration
).connect(
scheme: scheme,
host: host,
port: port,
path: path,
query: query,
headers: headers,
proxy: proxy,
proxyPort: proxyPort,
proxyHeaders: proxyHeaders,
proxyConnectDeadline: proxyConnectDeadline,
onUpgrade: onUpgrade
)
}
/// Description
/// - Parameters:
/// - url: URL for the origin server.
/// - headers: Headers to send to the origin server.
/// - proxy: Host component of the URI for the proxy server.
/// - proxyPort: Port on which to connect to the proxy server.
/// - proxyHeaders: Headers to send to the proxy server.
/// - proxyConnectDeadline: Deadline for establishing the proxy connection.
/// - configuration: Configuration for the WebSocket client.
/// - eventLoopGroup: Event loop group to be used by the WebSocket client.
/// - onUpgrade: An escaping closure to be executed after the upgrade is completed by `NIOWebSocketClientUpgrader`.
/// - Returns: A future which completes when the connection to the origin server is established.
@preconcurrency
public static func connect(
to url: String,
headers: HTTPHeaders = [:],
proxy: String?,
proxyPort: Int? = nil,
proxyHeaders: HTTPHeaders = [:],
proxyConnectDeadline: NIODeadline = NIODeadline.distantFuture,
configuration: WebSocketClient.Configuration = .init(),
on eventLoopGroup: EventLoopGroup,
onUpgrade: @Sendable @escaping (WebSocket) -> ()
) -> EventLoopFuture<Void> {
guard let url = URL(string: url) else {
return eventLoopGroup.any().makeFailedFuture(WebSocketClient.Error.invalidURL)
}
let scheme = url.scheme ?? "ws"
return self.connect(
scheme: scheme,
host: url.host ?? "localhost",
port: url.port ?? (scheme == "wss" ? 443 : 80),
path: url.path,
query: url.query,
headers: headers,
proxy: proxy,
proxyPort: proxyPort,
proxyHeaders: proxyHeaders,
proxyConnectDeadline: proxyConnectDeadline,
on: eventLoopGroup,
onUpgrade: onUpgrade
)
}
}