WebSocket

Represents a single WebSocket connection.

int main (string[] args)
{
  auto taskHandle = runTask(() => connectToWS());
  return runApplication(&args);
}

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!");
}

Members

Functions

close
void close(short code, const(char)[] reason)

Actively closes the connection.

receive
void receive(void delegate(scope IncomingWebSocketMessage) @(safe) receiver)

Receives a new message using an InputStream.

receiveBinary
ubyte[] receiveBinary(bool strict)
receiveText
string receiveText(bool strict)

Receives a new message and returns its contents as a newly allocated data array.

send
void send(const(char)[] data)

Sends a text message.

send
void send(ubyte[] data)

Sends a binary message.

send
void send(void delegate(scope OutgoingWebSocketMessage) @(safe) sender, FrameOpcode frameOpcode)

Sends a message using an output stream.

waitForData
bool waitForData()
bool waitForData(Duration timeout)

Waits until either a message arrives or until the connection is closed.

Properties

closeCode
short closeCode [@property getter]

Returns the close code sent by the remote end.

closeReason
const(char)[] closeReason [@property getter]

Returns the close reason sent by the remote end.

connected
bool connected [@property getter]

Determines if the WebSocket connection is still alive and ready for sending.

dataAvailableForRead
bool dataAvailableForRead [@property getter]

Checks if data is readily available for read.

request
const(HTTPServerRequest) request [@property getter]

The HTTP request that established the web socket connection.

Meta