Sign in

    moonbitstack/moonapi/socket does not have a README file

    WsHandler

    type WsHandler = (WebSocket) -> Unit

    A WebSocket route handler: given the connection, drive the exchange. Usually accept, then a receive loop, then close.

    WebSocket

    pub struct WebSocket {
    inbox : Array[WsMessage]
    cursor : Int
    params : Map[String, String]
    subprotocols : Array[String]
    accepted : Bool
    closed : Bool
    outbox : Array[
    Event
    ]
    }

    The handler's view of a WebSocket connection. It reads client frames off an inbound queue and records its own actions (accept / send / close) into an outbound event log the transport replays. params are the matched :name path segments, as with an HTTP Context.

    WebSocket::accept

    fn WebSocket::accept(self : WebSocket, subprotocol? : String, headers? : Array[(String, String)]) -> Unit

    Accept the handshake (← await websocket.accept()), optionally selecting a subprotocol and adding response headers. Idempotent: a second call is a no-op, so accept-once handlers stay simple.

    WebSocket::close

    fn WebSocket::close(self : WebSocket, code? : Int, reason? : String) -> Unit

    Close the connection with a status code (default 1000, normal closure) and reason. Idempotent.

    WebSocket::new

    fn WebSocket::new(inbox : Array[WsMessage], params : Map[String, String], subprotocols : Array[String]) -> WebSocket

    Build a connection over a pre-supplied inbound queue — the shape a test or the serving shell hands the handler.

    WebSocket::offered_subprotocols

    fn WebSocket::offered_subprotocols(self : WebSocket) -> Array[String]

    The subprotocols the client offered (the Sec-WebSocket-Protocol list).

    WebSocket::param

    fn WebSocket::param(self : WebSocket, name : String) -> String?

    Look up a matched path parameter by name.

    WebSocket::receive

    fn WebSocket::receive(self : WebSocket) -> WsMessage?

    Pull the next client frame, None once the client has sent them all (the disconnect). The receive a handler loops on.

    WebSocket::receive_bytes

    fn WebSocket::receive_bytes(self : WebSocket) -> Bytes?

    The next client frame as bytes: Some(b) for a binary frame, None on a text frame or the disconnect.

    WebSocket::receive_text

    fn WebSocket::receive_text(self : WebSocket) -> String?

    The next client frame as text: Some(s) for a text frame, None on a binary frame or the disconnect (← await websocket.receive_text()).

    WebSocket::send_bytes

    fn WebSocket::send_bytes(self : WebSocket, bytes : Bytes) -> Unit

    Send a binary frame to the client.

    WebSocket::send_text

    fn WebSocket::send_text(self : WebSocket, text : String) -> Unit

    Send a text frame to the client (← await websocket.send_text(...)).

    WebSocket::sent

    The events the handler emitted, in order — the transcript a test asserts on.

    WsMessage

    pub(all) enum WsMessage {
    WsText(String)
    WsBinary(Bytes)
    } derive(Eq)

    One inbound WebSocket message: a text frame or a binary frame.

    WsMessage::equal

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

    WsMessage::not_equal

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

    WsRoute

    pub(all) struct WsRoute {
    path : String
    handler : (WebSocket) -> Unit
    }

    drive_websocket

    fn drive_websocket(handler : (WebSocket) -> Unit, inbound : Array[WsMessage], params? : Map[String, String], subprotocols? : Array[String]) -> Array[
    Event
    ]

    Run a WebSocket handler against an in-memory frame queue and return the events it emitted — the synchronous test driver (the WS half of a TestClient). Feed the client's frames as inbound; get back the handler's accept / send / close sequence.

    Source Files