@@ -26,10 +26,10 @@ public class WebSocketClient {
2626 var channel: Channel? = nil
2727 var tlsEnabled: Bool = false
2828 var closeSent: Bool = false
29-
30- let upgradedSignalled = DispatchSemaphore(value: 0 )
31- let locker = DispatchQueue(label: WEBSOCKET_LOCKER_QUEUE)
32- let threadGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
29+
30+ let locker = DispatchQueue(label: WEBSOCKET_LOCKER_QUEUE, qos: .background )
31+
32+ var threadGroup: MultiThreadedEventLoopGroup? = nil
3333
3434 weak var delegate: WebSocketClientDelegate? = nil
3535
@@ -184,6 +184,10 @@ public class WebSocketClient {
184184 self.maxFrameSize = maxFrameSize
185185 self.tlsEnabled = tlsEnabled
186186 self.delegate = delegate
187+
188+ DispatchQueue.global(qos: .background).async {
189+ self.threadGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
190+ }
187191 }
188192
189193 /// Create a new `WebSocketClient`.
@@ -207,10 +211,14 @@ public class WebSocketClient {
207211 self.maxFrameSize = 24
208212 self.tlsEnabled = tlsEnabled
209213 self.delegate = delegate
214+
215+ DispatchQueue.global(qos: .background).async {
216+ self.threadGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
217+ }
210218 }
211219
212220 deinit {
213- try! threadGroup.syncShutdownGracefully()
221+ try! threadGroup! .syncShutdownGracefully()
214222 }
215223
216224 // MARK: - Open connection
@@ -221,16 +229,16 @@ public class WebSocketClient {
221229 SocketOptionLevel(SOL_SOCKET),
222230 SO_REUSEPORT
223231 )
232+
233+ while(threadGroup == nil) {}
224234
225- let bootstrap = ClientBootstrap(group: threadGroup)
235+ let bootstrap = ClientBootstrap(group: threadGroup! )
226236 .channelOption(socketOptions, value: 1)
227237 .channelInitializer(self.openChannel)
228238
229239 _ = try bootstrap
230240 .connect(host: self.host, port: self.port)
231241 .wait()
232-
233- self.upgradedSignalled.wait()
234242 }
235243
236244 private func openChannel(channel: Channel) -> EventLoopFuture<Void > {
@@ -260,13 +268,10 @@ public class WebSocketClient {
260268 }
261269
262270 private func upgradePipelineHandler(channel: Channel, response: HTTPResponseHead) -> EventLoopFuture<Void > {
263- self.onOpen(channel)
264-
265271 let handler = MessageHandler(client: self)
266272
267273 if response.status == .switchingProtocols {
268274 self.channel = channel
269- self.upgradedSignalled.signal()
270275 }
271276
272277 return channel.pipeline.addHandler(handler)
0 commit comments