moonbitstack/moonasgi/ws does not have a README file

    WebSocketHandler

    pub(all) struct WebSocketHandler {
    on_connect : (
    WebSocketScope
    ) -> WsAccept
    on_receive : (WsMessage) -> Array[WsSend]
    on_disconnect : (Int, String?) -> Array[WsSend]
    }

    A synchronous WebSocket application in the connect / receive / disconnect shape, the WebSocket analog of the http Handler. on_connect decides the handshake from the scope; on_receive maps each inbound WsMessage to the frames to send back (it may Close); on_disconnect runs when the client goes away, carrying the close code and the optional close reason (ASGI 2.5). Driven in-process by ws_run and the TestClient, so accept/subprotocol/echo/close logic is testable on every backend without an async socket — the faithful synchronous core the async server (mooncat) lifts onto AsgiApp.

    WebSocketHandler::echo

    fn WebSocketHandler::echo(subprotocol? : String) -> WebSocketHandler

    An echo handler: accept the handshake (optionally negotiating subprotocol), then send every received message straight back — text as text, binary as binary. The canonical WebSocket smoke test.

    WebSocketHandler::new

    fn WebSocketHandler::new(on_connect? : (
    WebSocketScope
    ) -> WsAccept, on_receive? : (WsMessage) -> Array[WsSend], on_disconnect? : (Int, String?) -> Array[WsSend]) -> WebSocketHandler

    Build a WebSocketHandler. on_connect defaults to accepting the handshake with no chosen subprotocol and no extra headers; on_receive and on_disconnect default to doing nothing. Callers override only the phases they care about.

    WsAccept

    pub(all) enum WsAccept {
    Accept(subprotocol~ : String?, headers~ : Array[(String, String)])
    Reject(code~ : Int, reason~ : String)
    DenyHttp(status~ : Int, headers~ : Array[(String, String)], body~ : Bytes)
    } derive(Eq)

    The application's answer to a WebSocketConnect, mirroring the three replies ASGI allows to an opening handshake: Accept it (optionally choosing a subprotocol and adding response headers), Reject it with a bare close code (ASGI closes the handshake with websocket.close), or DenyHttp it with a full HTTP response (the websocket.http.response extension — a real status
    • headers + body instead of a bare close).

    WsAccept::equal

    fn WsAccept::equal(WsAccept, WsAccept) -> Bool

    WsAccept::not_equal

    fn WsAccept::not_equal(x : WsAccept, y : WsAccept) -> Bool

    WsMessage

    pub(all) enum WsMessage {
    Text(String)
    Binary(Bytes)
    } derive(Eq)

    A message crossing a WebSocket in either direction: a UTF-8 Text frame or a Binary frame. The ergonomic form of WebSocketReceive / WebSocketSendText / WebSocketSendBytes, so a WebSocketHandler reasons over messages instead of raw events.

    WsMessage::equal

    fn WsMessage::equal(WsMessage, WsMessage) -> Bool

    WsMessage::not_equal

    fn WsMessage::not_equal(x : WsMessage, y : WsMessage) -> Bool

    WsSend

    pub(all) enum WsSend {
    SendText(String)
    SendBinary(Bytes)
    Close(code~ : Int, reason~ : String)
    } derive(Eq)

    An outbound action a WebSocketHandler takes while the connection is open: send a SendText / SendBinary frame, or Close the connection with a code and reason. A Close ends the handler's send stream — the driver stops pulling further inbound messages, mirroring a server that has closed the socket.

    WsSend::equal

    fn WsSend::equal(WsSend, WsSend) -> Bool

    WsSend::not_equal

    fn WsSend::not_equal(x : WsSend, y : WsSend) -> Bool

    ws_run

    Drive a WebSocketHandler over an inbound event sequence, returning the outbound events a server would send. The WebSocket counterpart of run_http: a WebSocket scope is driven through drive_ws; any other scope yields [], since this sugar covers websocket connections only.

    ws_run_app

    The general sans-transport WebSocket core: hand the whole inbound event stream and the @spec.WebSocketScope to app and return the outbound events it emits. The escape hatch under ws_run for applications whose control flow is not the connect/receive/disconnect fold — an app is free to inspect every scope field (subprotocols, headers, extensions, state) and emit any sequence. Non-websocket scopes yield []. The WebSocket analog of run_http_app.

    Source Files