cromo The Cromo Guide
Download PDF IT

std.websocket

WebSocket (RFC 6455): a connection that stays open and carries messages in both directions, texts or bytes, with TLS for wss. A client opens one with connect(url); a handler of std.http turns its request into one with req.webSocket(...). Both sides then use a Socket: send and sendBytes send a message, receive() waits for the next one, answering the pings by itself, and throws Closed when the other side closes.

cromo
websocket.Socket ws = websocket.connect("wss://example.com/chat")
ws.send("hello")
switch (ws.receive()) {
    case Text(string text) {
        println("{}", text)
    }
    case Binary(list<u8> bytes) {
        println("{} bytes", bytes.len())
    }
}
ws.close()

The guide: Networking and HTTP.

Made from the documentation in the sources of the library.

#Types

#Message

cromo
enum Message

A message of a WebSocket: a text, or bytes.

  • Text(string text): a message of text, UTF-8
  • Binary(list<u8> bytes): a message of bytes

#Closed

cromo
struct Closed : Exception

The exception of a WebSocket that is closed: receive() throws it when the other side has closed, or the connection has ended, and so do send and sendBytes after a close.

cromo
try {
    for {
        ws.receive()
    }
} catch (websocket.Closed e) {
    println("closed: {} {}", e.code, e.reason)
}
  • i32 code: The code of the close: 1000 a normal end, 1001 a side that goes away, 1005 a close without a code, 1006 a connection that ended without a close.
  • string reason: the reason that the other side gave, often ""

#Closed.message

cromo
string message() const

The text of the exception: "the WebSocket is closed: 1000", followed by the reason when there is one.

#Socket

cromo
struct Socket

A WebSocket, of a client from connect or of a server from Request.webSocket of std.http. Each call waits until it is done, or until the timeout; in a task, the wait leaves the thread to the other tasks. The destructor ends the connection without a close of WebSocket, which the other side sees as the code 1006: close() ends it cleanly. It can be moved, not copied.

#Socket.send

cromo
void send(string text)

Sends the text as one message. An exception on an error of the network or a timeout, and Closed after a close.

#Socket.sendBytes

cromo
void sendBytes(list<u8> bytes)

Sends the bytes as one message, binary. An exception on an error of the network or a timeout, and Closed after a close.

#Socket.timeout

cromo
void timeout(i64 milliseconds)

Sets how long each read and write waits, in milliseconds, as for a connection; past it the call throws. -1, the default, lets receive() wait as long as it takes.

#Socket.receive

cromo
Message receive()

The next message, a text or bytes, waiting for it. The fragments of a message are joined, and the pings that arrive meanwhile are answered. It throws Closed when the other side closes, after answering its close, or when the connection ends without one, and again at each call after. An exception for a message larger than 16 MiB, a frame that is not valid, a text that is not UTF-8, or a timeout.

#Socket.close

cromo
void close()

Ends the WebSocket cleanly: sends the close, with the code 1000, and waits for that of the other side, dropping the messages that arrive meanwhile, then closes the connection. Without a timeout it waits as long as the other side takes to answer; with one, an answer that does not come in time is an exception. Then send and receive() throw Closed.

#Functions

#connect

cromo
Socket connect(string url)

A WebSocket of a client, opened with a URL as "ws://host:port/path", or "wss://..." with TLS; without a port, 80 or 443. An exception if the URL does not start with ws:// or wss://, or its port is not a number, if the host is not found or cannot be reached, or if the server does not accept the WebSocket, with what it answered, as "HTTP/1.1 404 Not Found". Connecting, with the handshake of TLS, has no timeout; then the server must answer within 30 seconds. The socket it gives has no timeout.

#accepted

cromo
Socket accepted(move net.Connection connection)

The WebSocket of a server, on a connection whose request it has answered with "101 Switching Protocols", which is moved into it; it has no timeout. Request.webSocket of std.http does all this: a program rarely needs it.

#acceptKey

cromo
string acceptKey(string key)

The value of Sec-WebSocket-Accept that answers the Sec-WebSocket-Key of a request (RFC 6455): the base64 of the SHA-1 of the key with a fixed GUID.