Class WebSocket
Represents a single WebSocket connection.
shared static this ()
{
runTask(() => connectToWS());
}
void connectToWS ()
{
auto ws_url = URL("wss://websockets.example.com/websocket/auth_token");
auto ws = connectWebSocket(ws_url);
logInfo("WebSocket connected");
while (ws.waitForData())
{
auto txt = ws.receiveText;
logInfo("Received: %s", txt);
}
logFatal("Connection lost!");
}
Properties
Name | Type | Description |
closeCode [get]
|
short | Returns the close code sent by the remote end.
|
closeReason [get]
|
const(char)[] | Returns the close reason sent by the remote end.
|
connected [get]
|
bool | Determines if the WebSocket connection is still alive and ready for sending.
|
dataAvailableForRead [get]
|
bool | Checks if data is readily available for read.
|
request [get]
|
const(HTTPServerRequest) | The HTTP request that established the web socket connection.
|
Methods
Name | Description |
close
(code, reason)
|
Actively closes the connection.
|
receive
(receiver)
|
Receives a new message using an InputStream.
|
receiveBinary
(strict)
|
Receives a new message and returns its contents as a newly allocated data array.
|
receiveText
(strict)
|
Receives a new message and returns its contents as a newly allocated data array.
|
send
(data)
|
Sends a text message.
|
send
(data)
|
Sends a binary message.
|
send
(sender, frameOpcode)
|
Sends a message using an output stream.
|
send
(sender)
|
Compatibility overload - will be removed soon.
|
waitForData
()
|
Waits until either a message arrives or until the connection is closed.
|