Skip to content

Commit

Permalink
docs updates
Browse files Browse the repository at this point in the history
  • Loading branch information
tanner0101 committed Apr 18, 2018
1 parent d57ef4c commit aaa0724
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 81 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
/.build
/Packages
/*.xcodeproj
Package.resolved

61 changes: 0 additions & 61 deletions Package.resolved

This file was deleted.

2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ let package = Package(
.package(url: "https://github.com/vapor/core.git", from: "3.0.0"),

// 🚀 Non-blocking, event-driven HTTP for Swift built on Swift NIO.
.package(url: "https://github.com/vapor/http.git", .branch("gm")),
.package(url: "https://github.com/vapor/http.git", .branch("master")),

// Event-driven network application framework for high performance protocol servers & clients, non-blocking.
.package(url: "https://github.com/apple/swift-nio.git", from: "1.4.0"),
Expand Down
24 changes: 17 additions & 7 deletions Sources/WebSocket/WebSocket+Client.swift
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
/// Allows `HTTPClient` to be used to create `WebSocket` connections.
///
/// let ws = try HTTPClient.webSocket(hostname: "echo.websocket.org", on: ...).wait()
/// ws.onText { ws, text in
/// print("server said: \(text)")
/// }
/// ws.send("Hello, world!")
/// try ws.onClose.wait()
///
extension HTTPClient {
/// Performs an HTTP protocol upgrade to WebSocket protocol `HTTPClient`.
// MARK: Client Upgrade

/// Performs an HTTP protocol upgrade to` WebSocket` protocol `HTTPClient`.
///
/// let worker = MultiThreadedEventLoopGroup(numThreads: 1)
/// let webSocket = try HTTPClient.webSocket(hostname: "echo.websocket.org", on: worker).wait()
/// webSocket.onText { ws, text in
/// print("server said: \(text))
/// let ws = try HTTPClient.webSocket(hostname: "echo.websocket.org", on: ...).wait()
/// ws.onText { ws, text in
/// print("server said: \(text)")
/// }
/// webSocket.send("Hello, world!")
/// try webSocket.onClose.wait()
/// ws.send("Hello, world!")
/// try ws.onClose.wait()
///
/// - parameters:
/// - scheme: Transport layer security to use, either tls or plainText.
Expand Down
27 changes: 23 additions & 4 deletions Sources/WebSocket/WebSocket+Server.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
extension WebSocket {
/// Creates an `HTTPProtocolUpgrader` that will create instances of this class upon HTTP upgrade.
/// Allows `HTTPServer` to accept `WebSocket` connections.
///
/// let ws = HTTPServer.webSocketUpgrader(shouldUpgrade: { req in
/// // return non-nil HTTPHeaders to allow upgrade
/// }, onUpgrade: { ws, req in
/// // setup callbacks or send data to connected WebSocket
/// })
///
/// HTTPServer.start(..., upgraders: [ws])
///
extension HTTPServer {
// MARK: Server Upgrade

/// Creates an `HTTPProtocolUpgrader` that will accept incoming `WebSocket` upgrade requests.
///
/// Use this with `HTTPServer.start(...)`.
public static func httpProtocolUpgrader(
/// let ws = HTTPServer.webSocketUpgrader(shouldUpgrade: { req in
/// // return non-nil HTTPHeaders to allow upgrade
/// }, onUpgrade: { ws, req in
/// // setup callbacks or send data to connected WebSocket
/// })
///
/// HTTPServer.start(..., upgraders: [ws])
///
public static func webSocketUpgrader(
shouldUpgrade: @escaping (HTTPRequest) -> (HTTPHeaders?),
onUpgrade: @escaping (WebSocket, HTTPRequest) -> ()
) -> HTTPProtocolUpgrader {
Expand Down
22 changes: 14 additions & 8 deletions Sources/WebSocket/WebSocket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,11 @@
/// }
///
public final class WebSocket: BasicWorker {
/// `true` if the `WebSocket` has been closed.
public private(set) var isClosed: Bool

/// See `BasicWorker`.
public var eventLoop: EventLoop {
return channel.eventLoop
}

/// A `Future` that will be completed when the `WebSocket` closes.
public var onClose: Future<Void> {
return channel.closeFuture
}

/// Outbound `WebSocketEventHandler`.
private let channel: Channel

Expand All @@ -41,6 +33,8 @@ public final class WebSocket: BasicWorker {
self.onErrorCallback = { _, _ in }
}

// MARK: Receive

/// Adds a callback to this `WebSocket` to receive text-formatted messages.
///
/// ws.onText { ws, string in
Expand Down Expand Up @@ -83,6 +77,8 @@ public final class WebSocket: BasicWorker {
onErrorCallback = callback
}

// MARK: Send

/// Sends text-formatted data to the connected client.
///
/// ws.onText { ws, string in
Expand Down Expand Up @@ -135,6 +131,16 @@ public final class WebSocket: BasicWorker {
send(binary, opcode: .binary, promise: promise)
}

// MARK: Close

/// `true` if the `WebSocket` has been closed.
public private(set) var isClosed: Bool

/// A `Future` that will be completed when the `WebSocket` closes.
public var onClose: Future<Void> {
return channel.closeFuture
}

/// Closes the `WebSocket`'s connection, disconnecting the client.
public func close() {
guard !isClosed else {
Expand Down

0 comments on commit aaa0724

Please sign in to comment.