Skip to content

Commit

Permalink
Add support for NIOTransportServices (#107)
Browse files Browse the repository at this point in the history
* Start using NIOTS

* Require watchOS 6

* Use MultiThreadedEventLoopGroup in all cases for EventLoopGroupProvider.createNew

* Update Package.swift

Co-authored-by: Gwynne Raskind <gwynne@darkrainfall.org>

Co-authored-by: Gwynne Raskind <gwynne@darkrainfall.org>
  • Loading branch information
PopFlamingo and gwynne authored Jul 25, 2022
1 parent 3af54d0 commit efb1bd5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
7 changes: 5 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ import PackageDescription
let package = Package(
name: "websocket-kit",
platforms: [
.macOS(.v10_15),
.iOS(.v11)
.macOS(.v10_15),
.iOS(.v13),
.tvOS(.v13),
],
products: [
.library(name: "WebSocketKit", targets: ["WebSocketKit"]),
],
dependencies: [
.package(url: "https://github.com/apple/swift-nio.git", from: "2.33.0"),
.package(url: "https://github.com/apple/swift-nio-ssl.git", from: "2.14.0"),
.package(url: "https://github.com/apple/swift-nio-transport-services.git", from: "1.11.4"),
],
targets: [
.target(name: "WebSocketKit", dependencies: [
Expand All @@ -23,6 +25,7 @@ let package = Package(
.product(name: "NIOHTTP1", package: "swift-nio"),
.product(name: "NIOSSL", package: "swift-nio-ssl"),
.product(name: "NIOWebSocket", package: "swift-nio"),
.product(name: "NIOTransportServices", package: "swift-nio-transport-services"),
]),
.testTarget(name: "WebSocketKitTests", dependencies: [
.target(name: "WebSocketKit"),
Expand Down
17 changes: 16 additions & 1 deletion Sources/WebSocketKit/WebSocketClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import NIOConcurrencyHelpers
import NIOHTTP1
import NIOWebSocket
import NIOSSL
import NIOTransportServices

public final class WebSocketClient {
public enum Error: Swift.Error, LocalizedError {
Expand Down Expand Up @@ -60,7 +61,7 @@ public final class WebSocketClient {
) -> EventLoopFuture<Void> {
assert(["ws", "wss"].contains(scheme))
let upgradePromise = self.group.next().makePromise(of: Void.self)
let bootstrap = ClientBootstrap(group: self.group)
let bootstrap = WebSocketClient.makeBootstrap(on: self.group)
.channelOption(ChannelOptions.socket(SocketOptionLevel(IPPROTO_TCP), TCP_NODELAY), value: 1)
.channelInitializer { channel in
let httpHandler = HTTPInitialRequestHandler(
Expand Down Expand Up @@ -141,6 +142,20 @@ public final class WebSocketClient {
}
}
}

private static func makeBootstrap(on eventLoop: EventLoopGroup) -> NIOClientTCPBootstrapProtocol {
#if canImport(Network)
if let tsBootstrap = NIOTSConnectionBootstrap(validatingGroup: eventLoop) {
return tsBootstrap
}
#endif

if let nioBootstrap = ClientBootstrap(validatingGroup: eventLoop) {
return nioBootstrap
}

fatalError("No matching bootstrap found")
}

deinit {
switch self.eventLoopGroupProvider {
Expand Down

0 comments on commit efb1bd5

Please sign in to comment.