Import std.net.ws.websocket
std.net.ws.websocket
std.net.ws's WebSocket, per RFC 6455: messages of text or bytes both ways over one connection, a client's from Connect, at a ws:// URL, or a wss:// one over TLS, or a server's from Accept, which answers the client's handshake. Each is blocking or Async, and a Ping gets its Pong while a Receive reads.
std/net/ws/websocket.fb
Types
WebSocketState
enum
Public Enum WebSocketState
What a WebSocket is at.
std/net/ws/websocket.fb, line 30
-
Open.
-
Closing, once it's sent its Close and waits for the other side's.
-
Closed, once the Close has gone both ways, or the connection ended.
WebSocketMessage
struct
Public Struct WebSocketMessage Implements Printable
A WebSocket's message: text, which is UTF-8, or bytes.
std/net/ws/websocket.fb, line 40
-
Whether it's text, or bytes.
-
Its bytes: a text message's UTF-8.
-
A message of
bytes, text whenisText. -
Its text: a text message's, or a binary one's bytes as UTF-8, or "" when they aren't.
-
A text message's text, or how many bytes a binary one has.
WebSocket
class
Public Class WebSocket
A WebSocket connection. A Receive gives each message as it comes, and answers a Ping with a Pong, and the other side's Close with its own, after which it gives Null; only one Receive reads at a time. Send and Ping throw a NetError of CLOSED once Close has been called, and every error is a NetError: of CLOSED for a connection that ended without a Close frame, RESET for one the other side ended badly, and OTHER for a frame that breaks RFC 6455, which closes it with the code the RFC gives. The connection closes when the WebSocket goes, or at Abort.
std/net/ws/websocket.fb, line 76
-
The subprotocol the handshake chose, or "".
-
@PanicsAtCaller Public Static Method Connect(url: String, protocols: String[] = [], timeout: Duration? = Null, tls: TlsClient? = Null): WebSocket Throws NetError, TimeoutErrorstatic method #Connects to a ws:// URL, or a wss:// one over TLS as
tlsconnects, or for a Null one, a TlsClient that trusts the OS's roots, and opens a WebSocket there, asking for the subprotocols, if any, in the order it would have them, waiting up totimeoutto connect and as long for each read of the TLS handshake and the server's answer, or as long as it takes for a Null one. Throws a NetError as TcpStream.Connect and TlsClient.Connect do, and of OTHER for a URL that isn't ws:// or wss:// and an answer that isn't a WebSocket's. Panics when a subprotocol isn't a token. -
@PanicsAtCaller Public Static Async Method ConnectAsync(url: String, protocols: String[] = [], timeout: Duration? = Null, tls: TlsClient? = Null): WebSocket Throws NetError, TimeoutErrorstatic method #Connect's Async twin, which waits on the main thread.
-
@PanicsAtCaller Public Static Method Accept(stream: TcpStream, protocols: String[] = []): WebSocket Throws NetErrorstatic method #Reads a client's handshake from a connection a TcpListener accepted, and opens a WebSocket over it, choosing the first of the client's subprotocols that
protocolshas, or none. A request that isn't a WebSocket's is answered with an error's status, and closed, and throws a NetError of OTHER; one that ends first throws a NetError of CLOSED. Panics when a subprotocol isn't a token. -
@PanicsAtCaller Public Static Async Method AcceptAsync(stream: TcpStream, protocols: String[] = []): WebSocket Throws NetErrorstatic method #Accept's Async twin, which waits on the main thread.
-
@PanicsAtCaller Public Static Async Method AcceptAsync(request: HttpRequest, protocols: String[] = []): WebSocket Throws NetErrorstatic method #Opens a WebSocket over the connection of a request that an HttpServer's handler was given, answering its handshake as AcceptAsync of a TcpStream does, and takes the connection from the server, which then sends nothing of the handler's response, and doesn't close it as it stops. A request that isn't a WebSocket's throws a NetError of OTHER, and the server sends the handler's response to it. Panics when a subprotocol isn't a token, or the
request's connection is a WebSocket's already. -
Where it is: OPEN, CLOSING, or CLOSED.
-
The code of the Close that came, 1005 for one without, or 1006 when the connection ended without one, as one this side failed for breaking RFC 6455 does, or Null while none has.
-
The reason the other side's Close gave, or "".
-
The most bytes a message it receives can have, 16 MiB unless it's set; a longer one closes it with 1009 and throws. Panics when it's set below 1.
-
The next message, waiting for it, or Null once the other side's Close has come, and after. Panics when another Receive is reading.
-
Receive's Async twin, which waits on the main thread.
-
Sends a text message.
-
Sends a binary message.
-
What Send does, without blocking its thread: a call gives its Task.
-
What Send does, without blocking its thread: a call gives its Task.
-
Sends a Ping, which the other side answers with a Pong of the same bytes, 125 at most, or it panics. A Receive reads past the Pong.
-
What Ping does, without blocking its thread: a call gives its Task.
-
Sends a Close with
codeandreason, unless it has already, and is CLOSING: Receive gives the messages that came before the other side's Close, and then Null. Thecodeis 1000, a normal close, or one RFC 6455 has an endpoint send, from 1001 to 1003 or 1007 to 1014, or one of 3000 to 4999, and thereason's UTF-8 is 123 bytes at most, or it panics. A Close that can't be sent closes the connection. -
What Close does, without blocking its thread: a call gives its Task.
-
Closes the connection at once, without a Close frame, and is CLOSED.
Report an issue with this page or with FutureBlitz on GitHub.