std::net::websocketWebSocket client and server for bidirectional communication.
Provides blocking WebSocket connections that can send and receive
text messages. Both client and server connections use the same Conn
type, so the send/receive API is identical in both directions.
import std.net.websocket;
fn main() {
match websocket.connect("ws://localhost:8080/ws") {
.Ok(ws) => {
ws.send_text("hello");
let msg = ws.recv();
msg.close();
ws.close();
}
.Err(_err) => {
println(websocket.last_error());
}
}
}
import std.net.websocket;
fn main() {
match websocket.listen("0.0.0.0:8080") {
.Ok(server) => {
let conn = server.accept();
let msg = conn.recv();
conn.send_text("got it");
conn.close();
server.close();
}
.Err(_err) => {
println(websocket.last_error());
}
}
}
last_errorReturn the detail of this actor's most recent WebSocket failure.
Returns the empty string when the last call succeeded. Call it immediately
after an Err for the precise, non-secret reason.
connectOpen a WebSocket connection to the given URL (client).
Returns Err(net.NetError) when the connection or the WebSocket handshake
fails. Use websocket.last_error() for the precise detail.
import std.net;
import std.net.websocket;
fn main() {
match websocket.connect("ws://127.0.0.1:9001/") {
.Ok(conn) => conn.close(),
.Err(_) => println(f"connect failed"),
}
}
listenStart a WebSocket server listening on the given address.
Returns Err(net.NetError) when the address cannot be bound. Use
websocket.last_error() for the precise detail.
import std.net;
import std.net.websocket;
fn main() {
match websocket.listen("127.0.0.1:0") {
.Ok(server) => server.close(),
.Err(_) => println(f"listen failed"),
}
}
ConnAn open WebSocket connection handle.
Created by calling websocket.connect(url), which returns Err when the
connection or handshake fails. Use send_text() to
send messages and recv() to receive them.
MessageAn incoming WebSocket message handle.
Obtained from conn.recv(). Messages close automatically at scope exit;
call close() to release one early.
ServerA WebSocket server listening for incoming connections.
Created by calling websocket.listen(addr), which returns Err when the
address cannot be bound. Use accept() to wait
for the next client connection.
ConnMethodsMethods available on a WebSocket Conn.
Send a text message over the connection. Return 0 on success.
Send a text message over the connection.
Block until a message is received and return it.
Receive a message with a per-call deadline.
deadline_ms <= 0 disables the deadline and behaves like recv.
Returns Err(NetError.TimedOut(0)) on deadline expiry. On other
failures returns Err(NetError.Other(0)) (WebSocket errors carry
no OS errno channel).
Attach this connection to an actor (Erlang-style active mode). The runtime reads frames in a background thread and delivers them as actor messages. The connection remains the caller's resource; attach transfers read authority only. Outbound sends and close remain valid while the reader retains the transport's shared inner state.
handler is the actor that will receive on_message and on_close.
The compiler synthesises both protocol message IDs from the concrete
actor type behind the handle.
MessageMethodsMethods available on a WebSocket Message.
Get the message type (0=text, 1=binary, 2=ping, 3=pong, 4=close, -1=error).
Extract the text content of a text message.
ServerMethodsMethods available on a WebSocket Server.
Accept the next WebSocket connection. Blocks until a client connects and completes the handshake.
Get the port the server is listening on.
WebSocketHandlerTrait for actors that handle WebSocket connections.
Implement this on an actor and call conn.attach(actor) to have
the runtime deliver frames as actor messages. The actor never calls
recv() โ the runtime reads in a background thread.
import std.net.websocket;
actor Echo {
let conn: websocket.Conn;
receive fn on_message(text: string) {
conn.send_text("echo: " + text);
}
receive fn on_close() {
println("disconnected");
}
}
Called when a text frame arrives from the client.
Called when the connection closes or errors.