hummingbird-project / hummingbird-websocket

Websocket upgrade support for Hummingbird
Apache License 2.0
36 stars 6 forks source link

WebSocket Router #41

Closed adam-fowler closed 6 months ago

adam-fowler commented 6 months ago

Initialise websocket upgrade with a router. This can be the same router as the one you use for everything else or a separate router, which would be more optimal given you are testing less routes for the upgrade.

let router = Router(context: BasicWebSocketRequestContext.self)
router.ws("/ws") { request,channel in
    return .upgrade([:])
} handle: { inbound, outbound, _ in
    for try await packet in inbound {
        if case .text("disconnect") = packet {
            break
        }
        try await outbound.write(.custom(packet.webSocketFrame))
    }
}

let app = Application(
    router: router,
    server: .webSocketUpgrade(webSocketRouter: router)
)
try await app.runService()

This PR also removes any NIOHTTP1 types from the public interface

adam-fowler commented 6 months ago

Fixed issues with WebSocket upgrade fail not sending response. Solution isn't great, hopefully will get better solution from NIO at some point.