WebSocket

WEBSOCKET SUPPORT:

Full example:

import arsd.cgi;

void websocketEcho(Cgi cgi) {
	if(cgi.websocketRequested()) {
		if(cgi.origin != "http://arsdnet.net")
			throw new Exception("bad origin");
		auto websocket = cgi.acceptWebsocket();

		websocket.send("hello");
		websocket.send(" world!");

		auto msg = websocket.recv();
		while(msg.opcode != WebSocketOpcode.close) {
			if(msg.opcode == WebSocketOpcode.text) {
				websocket.send(msg.textData);
			} else if(msg.opcode == WebSocketOpcode.binary) {
				websocket.send(msg.data);
			}

			msg = websocket.recv();
		}

		websocket.close();
	} else {
		cgi.write("You are loading the websocket endpoint in a browser instead of a websocket client. Use a websocket client on this url instead.\n", true);
	}
}

mixin GenericMain!websocketEcho;

Members

Functions

close
void close(int code, string reason)

Closes the connection, sending a graceful teardown message to the other side.

isMessageBuffered
bool isMessageBuffered()

Is there a message in the buffer already? If true, waitForNextMessage is guaranteed to return immediately. If false, check isDataPending as the next step.

onmessage
void onmessage(void delegate(in char[]) dg)
void onmessage(void delegate(in ubyte[]) dg)

Group: browser_api

ping
void ping()

Sends a ping message to the server. This is done automatically by the library if you set a non-zero Config.pingFrequency, but you can also send extra pings explicitly as well with this function.

readyState
int readyState()

Returns one of CONNECTING, OPEN, CLOSING, or CLOSED.

send
void send(char[] textData)

Sends a text message through the websocket.

send
void send(ubyte[] binaryData)

Sends a binary message through the websocket.

waitForNextMessage
WebSocketFrame waitForNextMessage()

Waits for and returns the next complete message on the socket.

waitForNextMessageWouldBlock
bool waitForNextMessageWouldBlock()

Tells if waitForNextMessage would block.

Manifest constants

CLOSED
enum CLOSED;

The connection is closed or couldn't be opened.

CLOSING
enum CLOSING;

The connection is in the process of closing.

CONNECTING
enum CONNECTING;

Socket has been created. The connection is not yet open.

OPEN
enum OPEN;

The connection is open and ready to communicate.

Structs

Config
struct Config

Group: foundational

Variables

onbinarymessage
void delegate(in ubyte[]) onbinarymessage;
onclose
void delegate() onclose;
onerror
void delegate() onerror;
onopen
void delegate() onopen;
ontextmessage
void delegate(in char[]) ontextmessage;
Suggestion Box / Bug Report