net::ws
WebSocket, RFC 6455, over any async transport — so the same code handles ws:// over TCP and wss:// over TLS. It is non-blocking throughout, over one buffered connection.
public module net::ws::frame;
public module net::ws::handshake;
public module net::ws::conn;
| File | What it holds | Import |
|---|---|---|
conn | WebSocket<S>, Message, connect, accept | import std::net::ws::conn; |
frame | Frame, OpCode, and the frame reader/writer on BufStream | import std::net::ws::frame; |
handshake | generate_key, compute_accept | import std::net::ws::handshake; |
import std::net::ws;
mut ws: WebSocket<TcpStream> = await ws::connect(conn, host, path)?;
await ws.send_text(Str::new("hello"));
match (await ws.recv()) {
Result::Ok(msg) => { /* Message::Text / Binary / Close */ }
Result::Err(e) => { }
}
SHA-1 and base64 for the accept key are pure Cryo from encoding, so plain ws:// pulls in no crypto dependency at all.
public function name() { ... }
public:Visible to any module that imports this one - the default for top-level items and for struct fields, so it is mostly written to be explicit. public: opens a visibility block that groups the members after it.
public module collections::array;In a directory's _module.cryo aggregator, lists a submodule and whether it is public. Importing the directory's namespace sees only the modules declared public here.
namespace std::net::ws::conn;WebSocket connection: handshake + message send/recv over a buffered async connection.
module pageimport a::b;
import a::b::{ X, Y };
import a::b as c;
import a::b::*;Brings a module, or selected items from it, into scope. One path per declaration: use the brace form for several items, as to alias, and * for everything public (prefer the brace form; wildcards invite name collisions).
mut name: Type = value;A mutable binding, reassignable after declaration - mutability is opt-in, const is the default. At module level it declares mutable global state. Before & it marks an exclusive reference or receiver instead.
mut ws: WebSocket<TcpStream>type struct WebSocket<S> {
conn: BufStream<S>;
is_client: boolean;
closed: boolean;
}type struct TcpStream {
fd: i32;
}await futureSuspends the enclosing async body until the operand - which must implement Future - completes, and evaluates to its output. A prefix operator allowed anywhere an expression is (conditions, arms, guards), but only inside async code.
async function connect(sock: TcpStream, host: Str, path: Str) -> Result<WebSocket<TcpStream>, IoError>Connect a WebSocket over an already-connected TCP socket: wrap it, run the client handshake, and hand back the upgraded connection. The socket is consumed — the WebSocket owns it from here.
value?Error propagation. On a Result<T, E> it yields the T of an Ok and otherwise returns the Err(e) from the enclosing function unchanged; on an Option<T> it yields the T of a Some or returns None. The enclosing function must return a matching Result / Option.
mut ws: WebSocket<TcpStream>async WebSocket::send_text(mut &this, text: Str) -> Result<(), IoError>
where S: AsyncTransportSend a text message in a single frame.
std::net::ws::conntype struct Str {
bytes: u8*;
length: u64;
}Str: a borrowed UTF-8 string slice.
std::collections::strstatic Str::new(text: string) -> StrConstruct from a NUL-terminated C-style string literal. The caller certifies that text is valid UTF-8 and outlives this view. The NUL terminator is not included in the view.
match (subject) {
Pattern(x) => { ... }
_ => { ... }
}The primary discriminator: branches on enum variants, integers, ranges, and destructuring patterns, and the compiler rejects a match that misses a case. There is no fallthrough. As an expression it evaluates to the chosen arm's value, so every arm must produce the same type.
async WebSocket::recv(mut &this) -> Result<Message, IoError>
where S: AsyncTransportReceive the next application message, reassembling fragments and transparently answering Pings with Pongs and skipping Pongs. A Close frame surfaces as Message::Close.
Result::Ok(T)msgpattern => { body }Separates a match arm's pattern from its body. Arms do not fall through.
Result::Err(E)etype enum Message {
Text(String);
Binary(Array<u8>);
Close;
}A complete application message. Owns its payload.
std::net::ws::conntype struct Frame {
fin: boolean;
opcode: OpCode;
masked: boolean;
payload: Array<u8>;
}One decoded frame. Owns its payload bytes; call drop. masked records whether the wire frame set the mask bit, so the connection layer - which knows whether it is the client or the server - can enforce the RFC 6455 section 5.1 mask-direction rule (the frame reader itself is role-agnostic).
type enum OpCode : u8 {
Continuation = 0x0;
Text = 0x1;
Binary = 0x2;
Close = 0x8;
Ping = 0x9;
Pong = 0xA;
}type struct BufStream<S> {
inner: S;
rbuf: Array<u8>;
rpos: u64;
scratch: Array<u8>;
wbuf: Array<u8>;
}