websocket¶
WebSocket connections, text frames only, on the desktop and in a browser.
import "websocket";
fn main() {
let socket = 0;
socket = websocket::open("wss://echo.example.com", fn(e) {
match e {
Open => { websocket::send(socket, "hello"); }
Message(text) => {
print(text);
websocket::close(socket, 1000, "done");
}
Error(why) => print(why),
Close(code, reason, clean) => print("closed " + string(code)),
}
});
}
On the desktop each connection runs on a thread of its own and wss:// is TLS
with the Mozilla root set. In a browser the page's own WebSocket carries it,
through the package's JavaScript module. An http:// or https:// URL is
taken as ws:// or wss://, as a page takes it. A binary frame is reported as
an Error rather than delivered.
open¶
on_event: afn(websocket::Event), called for each event.- Returns: a handle for
send,closeandstate.
Event variant |
Meaning |
|---|---|
Open |
The connection is up |
Message(text) |
A text frame arrived |
Error(message) |
Something failed; a Close follows when the connection is over |
Close(code, reason, clean) |
The connection is over, with the close code, the reason, and whether the closing handshake finished cleanly. Always last |
A connection that never opens reports an Error and then Close(1006, "",
false).
send¶
- Returns: false when the connection is not open.
close¶
Starts the closing handshake. The Close event follows once it is over.
code: 1000 for a normal close, or 3000 to 4999 for one of your own.reason: at most 123 bytes.- Returns: false when there is no connection to close.
- Raises:
websocket_bad_closefor another code or a longer reason.
state¶
- Returns:
connecting,open,closingorclosed, which is also the answer for a handle with no connection.