JFLib

plat/web_net.rs#

Source: lib/plat/web_net.rs

web_net -- the browser's WebSocket (G7d): plat::net::WebSocket through the JS host

  WebSocket::connect(url) / connect_with(url, WsLimits) -> NetResult<WebSocket>
           "ws://host:port/path", "wss://...", or "/path": the page's own server (ws:// for a
           page over http, wss:// over https); net.connect is checked on the URL's host and
           port first (policy::net_host_allowed), a refusal is NetErrorKind::Denied.  The
           page's own server (the host and port of the page's origin) needs no rule: it served
           the app (the user's decision of 2026-11-18)
  ws.state() -> WsState (Connecting, Open, Closing, Closed)
  ws.send_text(&str) / send_binary(&[u8])     while Open
  ws.recv() -> Option<WsMessage>                never blocks
  ws.close(code, reason) / close_info() / protocol()

The worker (web/worker.js) holds the browser's WebSocket objects, so frames never pass the
page: REQUEST_WEBSOCKET with the socket's id as the request's id, one op byte and its data; the
host answers through jfgui_ws(id, kind, len) with the bytes in the inbox and wakes the app
(plat::wake: E_WAKE, the UI runs a pass).  The browser does the handshake, the masking,
fragments and pings itself.  What a socket keeps for the app is bounded (WsLimits): a bigger
message or more queued bytes close it with 1009.  The sockets belong to the app's thread (the
worker that runs it): compute threads of the threaded build do not reach them.
/// (browser) a WebSocket operation for the worker: the socket's id as the request's id, one op
/// byte (1 open: the URL; 2 send text; 3 send binary; 4 close: u16 code, the reason), its data
REQUEST_WEBSOCKET: u32 = 32
/// sockets an app may hold at once in the browser
WS_MAX_SOCKETS: usize = 64
  • enum WsState where a browser WebSocket stands

    • Connecting -- the browser connects (handshake on the way)
    • Open -- connected: messages go both ways
    • Closing -- close() was called, or a limit closed it; the browser's close has not come yet
    • Closed -- ended: close_info() says how
  • enum WsMessage a whole message from the server

    • Text(String) -- a text message
    • Binary(Vec<u8>) -- a binary message
  • struct WsLimits what a browser WebSocket keeps for the app until recv takes it

    • max_message: usize -- the largest message; a bigger one closes the socket (1009)
    • max_queued: usize -- bytes received and not taken yet; more close the socket (1009)
  • struct WebSocket a WebSocket of the page (the browser's object lives in the worker). Dropping it closes it

  • fn ws_url_split(url: &str, origin: Option<&str>) -> Option<(String, String, u16)> a WebSocket URL -> (the absolute URL, the host as written, the port); "/path" against the page's origin (jfgui_PAGE_ORIGIN, which the worker sets); None: not a ws:// / wss:// URL

  • fn page_origin() -> Option<String> the page's origin (jfgui_PAGE_ORIGIN at the start); None outside a page

  • fn is_page_server(origin: Option<&str>, host: &str, port: u16) -> bool whether host and port are the page's own server (the host and port of origin, its default port when it names none)

impl WebSocket

  • fn connect(url: &str) -> NetResult<WebSocket> connect to a WebSocket server with the default limits (see the top of this file)

  • fn connect_with(url: &str, lim: WsLimits) -> NetResult<WebSocket> connect with these limits: Connecting at once, Open when the browser's handshake is done

  • fn state(&self) -> WsState where it stands

  • fn recv(&self) -> Option<WsMessage> the next message, or None (never waits)

  • fn pending(&self) -> usize messages waiting for recv

  • fn send_text(&self, s: &str) -> NetResult<()> send a text message (Open only: Other while Connecting, Eof after the close)

  • fn send_binary(&self, b: &[u8]) -> NetResult<()> send a binary message (as send_text)

  • fn close(&self, code: u16, reason: &str) close with code (1000 or 3000-4999, others become 1000; the browser allows no more) and a reason of at most 123 bytes

  • fn close_info(&self) -> Option<(u16, String)> how it ended: the close code (1006: no close frame, the browser's word for a failed connection too) and reason; None while it is not closed

  • fn protocol(&self) -> String the subprotocol the server chose (empty: none)

  • extern "C" fn jfgui_ws(id: u32, kind: u32, len: u32) -> u32 (browser host) a WebSocket's event: kind 1 open (the inbox holds the chosen subprotocol), 2 a text message, 3 a binary message, 4 closed (u16 code little-endian, the reason); the host calls jfgui_input after a burst. -> 1 taken

Source: docs/API.md in the JFLib repository.