mirror of
https://github.com/apple/swift-nio
synced 2026-11-24 04:37:54 +00:00
* Introduce new typed `HTTPServerUpgrader` and `WebSocketServerUpgrader` # Motivation With our new `NIOAsyncChannel` and typed bootstrap APIs we want to be able to let users spell out their pipeline in a typed way. Pipelines can contain handlers that have to make a forking decision such as HTTP upgrading. Our current `HTTPServerUpgradeHandler` is one of those handlers but it lacks strict typing. To interact nicely with our new typed APIs we need to have a new variant of the `HTTPServerUpgradeHandler` that can carry type information. # Modification This PR adds a few things: 1. A new `NIOTypedHTTPServerUpgradeHandler` + `NIOTypedHTTPServeProtocolUpgrader`. I also moved the state handling logic to a separate state machine. I thought about unifying the state machines of the _old_ handler and the new one but they differ in behaviour which makes the state machine more complicated. 2. A new `NIOTypedWebSocketServerUpgrader` that conforms to `NIOTypedHTTPServerProtocolUpgrader` 3. An overhauled WebSocket server example that fully uses Concurrency. # Result We now have a way to fully type the server side of HTTP protocol upgrading. Code review Update parameter names for new API and fix example * Introduce new configuration struct and rename to `UpgradablePipeline` * Review comments
NIOWebSocketClient
This sample application provides a simple WebSocket client. First it performs an upgrade from a HTTP connection. Once upgraded, it sends a 'ping' frame, with a body, to the server. It then checks that the body matches in the returning 'pong' frame. It also prints out any text frame received from the server. Invoke it using one of the following syntaxes:
swift run NIOWebSocketClient # Connects to a server on ::1, port 8888.
swift run NIOWebSocketClient 9899 # Connects to a server on ::1, port 9899
swift run NIOWebSocketClient /path/to/unix/socket # Connects to a server using the given UNIX socket
swift run NIOWebSocketClient echo.example.com 9899 # Connects to a server on echo.example.com:9899