conn
import std::net::ws::conn; · source
Types
WebSocket<S>
type struct WebSocket<S> {
conn: BufStream<S>;
is_client: boolean;
closed: boolean;
static of(conn: BufStream<S>, is_client: boolean) -> WebSocket<S>;
connection(mut &this) -> BufStream<S>*;
is_closed(&this) -> boolean;
async client_handshake(mut &this, host: Str, path: Str) -> Result<(), IoError>
where S: AsyncTransport;
async server_handshake(mut &this) -> Result<(), IoError>
where S: AsyncTransport;
async send_text(mut &this, text: Str) -> Result<(), IoError>
where S: AsyncTransport;
async send_binary(mut &this, data: Slice<u8>) -> Result<(), IoError>
where S: AsyncTransport;
async ping(mut &this, data: Slice<u8>) -> Result<(), IoError>
where S: AsyncTransport;
async close(mut &this, code: u16, reason: Str) -> Result<(), IoError>
where S: AsyncTransport;
async recv(mut &this) -> Result<Message, IoError>
where S: AsyncTransport;
}
A WebSocket<S> owns a BufStream<S> over any AsyncTransport. connect(sock, host, path) and accept(sock) wrap a plain TcpStream and run the handshake in one step. For a TLS transport, or to control the buffering, build with of(conn, is_client) and call client_handshake(host, path) or server_handshake() yourself. The handshake is an HTTP/1.1 Upgrade.
mut ws: WebSocket<TlsStream> = WebSocket::<TlsStream>::of(BufStream::<TlsStream>::of(tls), true);
await ws.client_handshake(host, path)?;
The message surface is send_text, send_binary, ping, close(code, reason), and recv — all async. Framing covers text and binary messages, fragmentation reassembly, and the Ping/Pong/Close control frames. recv yields application messages only: it answers a Ping with a Pong itself, skips incoming Pongs, and surfaces a Close frame as Message::Close. A bounded number of control frames is serviced per recv, so a peer flooding pings cannot keep the call from ever returning. is_closed() reports whether a close has been sent or received; connection() surfaces the transport for socket options, not for I/O.
Clients mask every frame they send and servers must not, as the RFC requires; is_client is what decides, and a frame arriving with the wrong masking is a protocol error.
Properties
| Property | Type |
|---|---|
conn | BufStream<S> |
is_client | boolean |
closed | boolean |
Constructors
static of(conn: BufStream<S>, is_client: boolean) -> WebSocket<S>;
static of(conn: BufStream<S>, is_client: boolean) -> WebSocket<S>;Take ownership of an already-upgraded connection. is_client selects masked (client) vs unmasked (server) framing per RFC 6455. Use client_handshake / server_handshake to run the upgrade first.
Methods
connection(mut &this) -> BufStream<S>*;
connection(mut &this) -> BufStream<S>*;The buffered connection, for callers that need the transport itself (socket options, the negotiated ALPN protocol). Not for reading or writing — that would desynchronize the framing.
is_closed(&this) -> boolean;
is_closed(&this) -> boolean;Has a Close frame been sent or received on this connection?
async client_handshake(mut &this, host: Str, path: Str) -> Result<(), IoError> where S: AsyncTransport;
async client_handshake(mut &this, host: Str, path: Str) -> Result<(), IoError> where S: AsyncTransport;Client handshake: send the GET upgrade, then verify the 101 and the Sec-WebSocket-Accept echo. Bytes that arrived after the blank line stay buffered for recv.
async server_handshake(mut &this) -> Result<(), IoError> where S: AsyncTransport;
async server_handshake(mut &this) -> Result<(), IoError> where S: AsyncTransport;Server handshake: read the client GET, reply 101 with the computed accept value. Bytes that arrived after the blank line — a client that pipelined its first frame into the same write — stay buffered.
async send_text(mut &this, text: Str) -> Result<(), IoError> where S: AsyncTransport;
async send_text(mut &this, text: Str) -> Result<(), IoError> where S: AsyncTransport;Send a text message in a single frame.
async send_binary(mut &this, data: Slice<u8>) -> Result<(), IoError> where S: AsyncTransport;
async send_binary(mut &this, data: Slice<u8>) -> Result<(), IoError> where S: AsyncTransport;Send a binary message in a single frame.
async ping(mut &this, data: Slice<u8>) -> Result<(), IoError> where S: AsyncTransport;
async ping(mut &this, data: Slice<u8>) -> Result<(), IoError> where S: AsyncTransport;Send a Ping with optional application data.
async close(mut &this, code: u16, reason: Str) -> Result<(), IoError> where S: AsyncTransport;
async close(mut &this, code: u16, reason: Str) -> Result<(), IoError> where S: AsyncTransport;Send a Close frame with a status code and reason. The transport itself is closed when the WebSocket is dropped.
async recv(mut &this) -> Result<Message, IoError> where S: AsyncTransport;
async recv(mut &this) -> Result<Message, IoError> where S: AsyncTransport;Receive the next application message, reassembling fragments and transparently answering Pings with Pongs and skipping Pongs. A Close frame surfaces as Message::Close.
Message
type enum Message {
Text(String);
Binary(Array<u8>);
Close;
}
What recv yields: a reassembled text or binary message, or the peer's close.
Variants
| Variant |
|---|
Text(String) |
Binary(Array<u8>) |
Close |
Trait implementations
implement<S> trait Drop for struct WebSocket<S>
Dropping the socket drops the connection. Send close first if you want the peer to see a clean shutdown rather than a reset.
Free Functions
async function connect(sock: TcpStream, host: Str, path: Str) -> Result<WebSocket<TcpStream>, IoError>;
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.
The one-step client constructor over a plain TcpStream: runs the opening handshake against host and path and returns a ready WebSocket<TcpStream>.
async function accept(sock: TcpStream) -> Result<WebSocket<TcpStream>, IoError>;
async function accept(sock: TcpStream) -> Result<WebSocket<TcpStream>, IoError>;Accept a WebSocket over an accepted TCP socket: wrap it, run the server handshake, and hand back the upgraded connection.
The server-side counterpart: answers a client's upgrade request on an accepted TcpStream and returns the ready WebSocket<TcpStream>.
Constants
const MAX_HEAD_LINE: u64 = 8192;
const MAX_HEAD_LINES: u64 = 100;
const MAX_MESSAGE: u64 = 67108864;
const MAX_CONTROL_PER_RECV: u64 = 256;type struct Name { ... }
type enum Name { ... }
type trait Name { ... }
type union Name { ... }Begins a type declaration; the keyword that follows says which kind. On its own, type Name = Existing; declares an alias.
type struct Name {
field: Type;
method(&this) -> Ret { ... }
}A value type with named fields and optional methods. Structs live on the stack and are passed by value. Fields are public by default and may carry = default values; methods take their receiver as &this, mut &this, or this.
type struct WebSocket<S> {
conn: BufStream<S>;
is_client: boolean;
closed: boolean;
}type parameter SWebSocket.conn: BufStream<S>type struct BufStream<S> {
inner: S;
rbuf: Array<u8>;
rpos: u64;
scratch: Array<u8>;
wbuf: Array<u8>;
}type parameter SWebSocket.is_client: booleanWebSocket.closed: booleanstatic name(param: Type) -> Ret { ... }A method that belongs to the type rather than an instance, called as Type::name(..). For structs, static new(..) returning a struct literal is the idiomatic constructor.
static WebSocket::of(conn: BufStream<S>, is_client: boolean) -> WebSocket<S>Take ownership of an already-upgraded connection. is_client selects masked (client) vs unmasked (server) framing per RFC 6455. Use client_handshake / server_handshake to run the upgrade first.
(parameter) conn: BufStream<S>(parameter) is_client: booleantype struct WebSocket<S> {
conn: BufStream<S>;
is_client: boolean;
closed: boolean;
}WebSocket::connection(mut &this) -> BufStream<S>*The buffered connection, for callers that need the transport itself (socket options, the negotiated ALPN protocol). Not for reading or writing — that would desynchronize the framing.
mut &this
mut &TypeAn exclusive, mutating borrow. As a receiver, mut &this lets the method modify the fields - and callers can see that from the signature alone. As a parameter type, mut &T lets the callee mutate through the reference while the caller keeps ownership.
&this
mut &this
thisThe receiver of a method. Its first parameter declares how the method takes it: &this borrows read-only, mut &this borrows exclusively, and this / mut this move the value in and consume it. In the body, this.field reaches the members.
WebSocket::is_closed(&this) -> booleanHas a Close frame been sent or received on this connection?
async function name(param: Type) -> T { ... }Compiles the function into a stackless state machine. Calling it builds a future whose output is the declared return type; nothing runs until that future is polled. Allowed on free functions, methods, and trait methods.
language referenceasync WebSocket::client_handshake(mut &this, host: Str, path: Str) -> Result<(), IoError>
where S: AsyncTransportClient handshake: send the GET upgrade, then verify the 101 and the Sec-WebSocket-Accept echo. Bytes that arrived after the blank line stay buffered for recv.
(parameter) host: Strtype struct Str {
bytes: u8*;
length: u64;
}Str: a borrowed UTF-8 string slice.
std::collections::str(parameter) path: Strprimitive ()The unit type: a value that carries no information, and the empty tuple. Distinct from void, which is the absence of a value - Result<(), E> succeeds with nothing to return. 0 bytes.
type struct IoError {
kind: IoErrorKind;
os_code: i32;
}where T: Ord + Clone, U: HashConstrains generic parameters to types that implement the listed traits, which is what makes those traits' methods callable on T. Bounds on one parameter join with +; parameters separate with ,. A where on a single method narrows the type's parameter for that method only.
type trait AsyncTransport {
async read_into(mut &this, buf: Array<u8>) -> Transfer;
async write_from(mut &this, buf: Array<u8>) -> Transfer;
}One raw, unbuffered async byte channel: what BufStream<S> needs of a transport, and nothing more.
async WebSocket::server_handshake(mut &this) -> Result<(), IoError>
where S: AsyncTransportServer handshake: read the client GET, reply 101 with the computed accept value. Bytes that arrived after the blank line — a client that pipelined its first frame into the same write — stay buffered.
async WebSocket::send_text(mut &this, text: Str) -> Result<(), IoError>
where S: AsyncTransportSend a text message in a single frame.
(parameter) text: Strasync WebSocket::send_binary(mut &this, data: Slice<u8>) -> Result<(), IoError>
where S: AsyncTransportSend a binary message in a single frame.
(parameter) data: Slice<u8>type struct Slice<T> {
ptr: T*;
length: u64;
}Slice<T>: a borrowed view into a contiguous sequence.
std::core::sliceasync WebSocket::ping(mut &this, data: Slice<u8>) -> Result<(), IoError>
where S: AsyncTransportSend a Ping with optional application data.
async WebSocket::close(mut &this, code: u16, reason: Str) -> Result<(), IoError>
where S: AsyncTransportSend a Close frame with a status code and reason. The transport itself is closed when the WebSocket is dropped.
(parameter) code: u16(parameter) reason: Strasync 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.
type enum Message {
Text(String);
Binary(Array<u8>);
Close;
}A complete application message. Owns its payload.
std::net::ws::connmut 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<TlsStream>type struct TlsStream {
ssl: void*;
inner: TcpStream;
}static WebSocket::of(conn: BufStream<S>, is_client: boolean) -> WebSocket<S>Take ownership of an already-upgraded connection. is_client selects masked (client) vs unmasked (server) framing per RFC 6455. Use client_handshake / server_handshake to run the upgrade first.
trueThe boolean literal. Booleans are a distinct 1-byte type, not interchangeable with integers; if and while conditions must be boolean.
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.
mut ws: WebSocket<TlsStream>async WebSocket::client_handshake(mut &this, host: Str, path: Str) -> Result<(), IoError>
where S: AsyncTransportClient handshake: send the GET upgrade, then verify the 101 and the Sec-WebSocket-Accept echo. Bytes that arrived after the blank line stay buffered for recv.
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.
type enum Name {
Unit,
Payload(T),
Named { x: i32 },
}An algebraic data type. Variants may be bare or carry a tuple or struct payload, and a match on an enum must cover every variant. Name::Variant names a variant; methods are added with implement enum Name { ... }.
type enum Message {
Text(String);
Binary(Array<u8>);
Close;
}A complete application message. Owns its payload.
Message::Text(String)type struct String<A = GlobalAlloc> {
buffer: RawBuffer<u8, A>;
length: u64;
}String: owned, heap-allocated, length-typed UTF-8.
std::collections::stringMessage::Binary(Array<u8>)type struct Array<T, A = GlobalAlloc> where A: Allocator {
ptr: T*;
length: i64;
capacity: i64;
alloc: A;
}Array<T, A>: growable, heap-backed, contiguous sequence.
std::collections::arrayMessage::Closeconst NAME: Type = value;At module level, a true compile-time constant, conventionally SCREAMING_SNAKE_CASE. (mut at module level declares mutable global state instead.)
const MAX_HEAD_LINE: u64 = 8192;const MAX_HEAD_LINES: u64 = 100;const MAX_MESSAGE: u64 = 67108864;const MAX_CONTROL_PER_RECV: u64 = 256;import 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).
namespace std::net::ws::conn;WebSocket connection: handshake + message send/recv over a buffered async connection.
module pageasync 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.
async function accept(sock: TcpStream) -> Result<WebSocket<TcpStream>, IoError>Accept a WebSocket over an accepted TCP socket: wrap it, run the server handshake, and hand back the upgraded connection.
std::net::ws::conntype struct TcpStream {
fd: i32;
}WebSocket.conn: BufStream<S>WebSocket.is_client: booleanasync WebSocket::server_handshake(mut &this) -> Result<(), IoError>
where S: AsyncTransportServer handshake: read the client GET, reply 101 with the computed accept value. Bytes that arrived after the blank line — a client that pipelined its first frame into the same write — stay buffered.
async WebSocket::send_text(mut &this, text: Str) -> Result<(), IoError>
where S: AsyncTransportSend a text message in a single frame.
std::net::ws::connasync WebSocket::send_binary(mut &this, data: Slice<u8>) -> Result<(), IoError>
where S: AsyncTransportSend a binary message in a single frame.
std::net::ws::connasync WebSocket::ping(mut &this, data: Slice<u8>) -> Result<(), IoError>
where S: AsyncTransportSend a Ping with optional application data.
std::net::ws::connasync WebSocket::close(mut &this, code: u16, reason: Str) -> Result<(), IoError>
where S: AsyncTransportSend a Close frame with a status code and reason. The transport itself is closed when the WebSocket is dropped.
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.
OpCode::Ping = 0x9OpCode::Pong = 0xAMessage::CloseWebSocket::is_closed(&this) -> booleanHas a Close frame been sent or received on this connection?
std::net::ws::connWebSocket::connection(mut &this) -> BufStream<S>*The buffered connection, for callers that need the transport itself (socket options, the negotiated ALPN protocol). Not for reading or writing — that would desynchronize the framing.
std::net::ws::connWebSocket.closed: booleanMessage::Text(String)Message::Binary(Array<u8>)implement struct Name { ... }
implement trait Trait for Type { ... }
implement u64 { ... }Adds methods to a type outside its declaration. implement struct T (or enum, or a bare primitive) opens an inherent block; implement trait Tr for T supplies a trait's methods for one type. Resolution is at compile time - no dynamic dispatch.
type trait Name : Base {
method(&this, other: &This) -> Ret;
}Names a set of methods a type may implement. Inside the body This is the implementing type; methods may carry default bodies, and a trait may require a base trait (type trait Ord : Eq). Generic code asks for a trait with where T: Name.
implement trait Trait for Type { ... }Names the type a trait implementation is for. The block that follows supplies the bodies the trait requires.
language referencefunction name<T>(param: Type) -> Ret { ... }Declares a function. Parameters are written name: Type and are never inferred; the return type follows -> and defaults to void. Declaration order does not matter: every signature is collected before any body is checked, so functions may call each other freely and recurse.
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.
(parameter) sock: TcpStreamasync function accept(sock: TcpStream) -> Result<WebSocket<TcpStream>, IoError>Accept a WebSocket over an accepted TCP socket: wrap it, run the server handshake, and hand back the upgraded connection.