The OpenD Programming Language

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(ubyte[] data)

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.

pong
void pong(ubyte[] data)

Sends a pong message to the server. This is normally done automatically in response to pings.

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

CloseEvent
struct CloseEvent

Arguments for the close event. The code and reason are provided from the close message on the websocket, if they are present. The spec says code 1000 indicates a normal, default reason close, but reserves the code range from 3000-5000 for future definition; the 3000s can be registered with IANA and the 4000's are application private use. The reason should be user readable, but not displayed to the end user. wasClean is true if the server actually sent a close event, false if it just disconnected.

Config
struct Config

Group: foundational

Variables

onbinarymessage
void delegate(in ubyte[]) onbinarymessage;
onclose
arsd.core.FlexibleDelegate!(void delegate(CloseEvent event)) onclose;

The CloseEvent you get references a temporary buffer that may be overwritten after your handler returns. If you want to keep it or the event.reason member, remember to .idup it.

onerror
void delegate() onerror;
onopen
void delegate() onopen;
ontextmessage
void delegate(in char[]) ontextmessage;

Meta