@@ -241,7 +241,8 @@ extension WebSocketServer {
241
241
resolvedAddress: SocketAddress ,
242
242
childChannelInitializer: @escaping ( Channel ) -> EventLoopFuture < Void >
243
243
) -> EventLoopFuture < Channel > {
244
- if self . eventloopGroup is MultiThreadedEventLoopGroup {
244
+
245
+ func makeServerBootstrap( ) -> EventLoopFuture < Channel > {
245
246
return ServerBootstrap ( group: self . eventloopGroup)
246
247
. serverChannelOption ( . socketOption( . so_reuseaddr) , value: 1 )
247
248
. serverChannelInitializer { channel in
@@ -282,7 +283,10 @@ extension WebSocketServer {
282
283
. childChannelOption ( . socketOption( . so_reuseaddr) , value: 1 )
283
284
. childChannelInitializer ( childChannelInitializer)
284
285
. bind ( to: resolvedAddress)
285
- } else {
286
+ }
287
+
288
+ #if canImport(Network)
289
+ func makeNIOTSListenerBootstrap( ) -> EventLoopFuture < Channel > {
286
290
return NIOTSListenerBootstrap ( group: self . eventloopGroup)
287
291
. serverChannelOption ( . socketOption( . so_reuseaddr) , value: 1 )
288
292
. serverChannelInitializer { channel in
@@ -324,20 +328,23 @@ extension WebSocketServer {
324
328
. childChannelInitializer ( childChannelInitializer)
325
329
. bind ( to: resolvedAddress)
326
330
}
331
+ #endif
332
+
333
+ #if canImport(Network)
334
+ if self . eventloopGroup is MultiThreadedEventLoopGroup {
335
+ return makeServerBootstrap ( )
336
+ } else {
337
+ return makeNIOTSListenerBootstrap ( )
338
+ }
339
+ #else
340
+ return makeServerBootstrap ( )
341
+ #endif
342
+
327
343
}
328
344
}
329
345
330
346
#if !canImport(Darwin) || swift(>=5.10)
331
347
extension WebSocketServer {
332
- // @available(macOS 13, iOS 16, watchOS 9, tvOS 16, visionOS 1.0, *)
333
- // public func listen(
334
- // to host: String,
335
- // port: Int,
336
- // configuration: LCLWebSocket.Configuration
337
- // ) throws -> EventLoopFuture<Void> {
338
- // let addr = try SocketAddress(ipAddress: host, port: port)
339
- // return self.listen(to: addr, configuration: configuration)
340
- // }
341
348
342
349
/// Let the WebSocket server bind and listen to the given address, using the provided configuration.
343
350
///
@@ -348,7 +355,7 @@ extension WebSocketServer {
348
355
///
349
356
/// - Note: this is functionally the same as `listen(to:configuration:)`. But this function relies on infrastructures that
350
357
/// is available only on Swift >= 5.10
351
- @available ( macOS 13 , iOS 16 , watchOS 9 , tvOS 16 , visionOS 1 . 0 , * )
358
+ @available ( macOS 13 , iOS 16 , watchOS 9 , tvOS 16 , * )
352
359
public func typedListen(
353
360
to address: SocketAddress ,
354
361
configuration: LCLWebSocket . Configuration
@@ -375,7 +382,7 @@ extension WebSocketServer {
375
382
}
376
383
}
377
384
378
- @available ( macOS 13 , iOS 16 , watchOS 9 , tvOS 16 , visionOS 1 . 0 , * )
385
+ @available ( macOS 13 , iOS 16 , watchOS 9 , tvOS 16 , * )
379
386
private func configureTypedWebSocketServerUpgrade(
380
387
on channel: Channel ,
381
388
configuration: LCLWebSocket . Configuration
@@ -520,9 +527,9 @@ extension WebSocketServer {
520
527
case ( . end, . ok) :
521
528
context. fireChannelRead ( data)
522
529
case ( . end, . invalidMethod) :
523
- let resposneHead = makeResponse ( with: . methodNotAllowed)
524
- context. channel. write ( self . wrapOutboundOut ( . head( resposneHead ) ) , promise: nil )
525
- context. channel. writeAndFlush ( self . wrapOutboundOut ( . end( nil ) ) , promise: nil )
530
+ let responseHead = makeResponse ( with: . methodNotAllowed)
531
+ context. channel. write ( HTTPServerResponsePart . head ( responseHead ) , promise: nil )
532
+ context. channel. writeAndFlush ( HTTPServerResponsePart . end ( nil ) , promise: nil )
526
533
context. fireErrorCaught ( LCLWebSocketError . methodNotAllowed)
527
534
context. close ( mode: . all, promise: nil )
528
535
}
@@ -576,8 +583,8 @@ extension WebSocketServer {
576
583
responseHead = makeResponse ( with: . internalServerError)
577
584
}
578
585
logger. debug ( " closing channel due to error \( error) . response head \( responseHead) " )
579
- context. channel. write ( self . wrapOutboundOut ( . head( responseHead) ) , promise: nil )
580
- context. channel. writeAndFlush ( self . wrapOutboundOut ( . end( nil ) ) , promise: nil )
586
+ context. channel. write ( HTTPServerResponsePart . head ( responseHead) , promise: nil )
587
+ context. channel. writeAndFlush ( HTTPServerResponsePart . end ( nil ) , promise: nil )
581
588
context. close ( mode: . all, promise: nil )
582
589
}
583
590
}
0 commit comments