Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-fowler committed Apr 6, 2024
1 parent 2aa4755 commit ae56a40
Showing 1 changed file with 33 additions and 14 deletions.
47 changes: 33 additions & 14 deletions README.md
@@ -1,30 +1,49 @@
# Hummingbird Websocket

Adds support for upgrading HTTP connections to WebSocket.
Adds support for upgrading HTTP1 connections to WebSocket.

## Usage

Setup WebSocket upgrades with a closure that either returns `.upgrade` with response headers and the handler for the WebSocket or a `.dontUpgrade`
```swift
let app = HBApplication(
let app = Application(
router: router,
server: .http1WebSocketUpgrade { channel, head in
if head.uri == "ws" {
return .upgrade(HTTPHeaders()) { inbound, outbound, _ in
for try await packet in inbound {
if case .text("disconnect") = packet {
break
}
try await outbound.write(.custom(packet.webSocketFrame))
}
server: .http1WebSocketUpgrade { request, channel, logger in
// upgrade if request URI is "/ws"
guard request.uri == "/ws" else { return .dontUpgrade }
// The upgrade response includes the headers to include in the response and
// the WebSocket handler
return .upgrade([:]) { inbound, outbound, context in
for try await packet in inbound {
// send "Received" for every packet we receive
try await outbound.write(.text("Received"))
}
} else {
return .dontUpgrade
}
}
)
app.runService()
```
Or alternatively use a `Router`. Using a router means you can add middleware to process the initial upgrade request before it is handled eg for authenticating the request.
```swift
let wsRouter = Router(context: BasicWebSocketRequestContext.self)
wsRouter.middlewares.add(BasicAuthenticator())
// An upgrade only occurs if a WebSocket path is matched
wsRouter.ws("/ws") { request, context in
// allow upgrade
.upgrade()
} onUpgrade: { inbound, outbound, context in
for try await packet in inbound {
// send "Received" for every packet we receive
try await outbound.write(.text("Received"))
}
}
let app = Application(
router: router,
server: .http1WebSocketUpgrade(webSocketRouter: wsRouter)
)
app.runService()
```

## Documentation

You can find reference documentation for HummingbirdWebSocket [here](https://hummingbird-project.github.io/hummingbird-docs/2.0/documentation/hummingbirdwebsocket). The [hummingbird-examples](https://github.com/hummingbird-project/hummingbird-examples) repository has a number of examples of different uses of the library.
You can find documentation for HummingbirdWebSocket [here](https://hummingbird-project.github.io/hummingbird-docs/2.0/documentation/hummingbirdwebsocket). The [hummingbird-examples](https://github.com/hummingbird-project/hummingbird-examples) repository has a number of examples of different uses of the library.

0 comments on commit ae56a40

Please sign in to comment.