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() {
let ws = websocket.connect("ws://localhost:8080/ws");
ws.send_text("hello");
let msg = ws.recv();
msg.free();
ws.close();
}
import std::net::websocket;
fn main() {
let server = websocket.listen("0.0.0.0:8080");
let conn = server.accept();
let msg = conn.recv();
conn.send_text("got it");
conn.close();
server.close();
}
connectOpen a WebSocket connection to the given URL (client).
listenStart a WebSocket server listening on the given address.
let server = websocket.listen("0.0.0.0:8080");
let conn = server.accept();
ConnAn open WebSocket connection handle.
Created by calling websocket.connect(url). Use send_text() to
send messages and recv() to receive them.
MessageAn incoming WebSocket message handle.
Obtained from conn.recv(). Must be freed with free() when no
longer needed.
ServerA WebSocket server listening for incoming connections.
Created by calling websocket.listen(addr). 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(IoError::TimedOut(0)) on deadline expiry. On other
failures returns Err(IoError::Other(0)) (WebSocket errors carry
no OS errno channel).
Close the WebSocket connection.
Attach this connection to an actor (Erlang-style active mode). The runtime reads frames in a background thread and delivers them as actor messages. Transfers ownership — the Conn handle is consumed and must not be used for recv() after this call. The actor can still call conn.send_text() for outbound messages.
handler is the actor that will receive on_message and on_close.
on_message_type is the msg_type index for the on_message receive fn.
on_close_type is the msg_type index for the on_close receive fn.
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.
Release the message resources.
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.
Close the server and stop listening.
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.
actor Echo {
let conn: 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.