WebSocket

WEBSOCKET SUPPORT:

Full example:

1 import arsd.cgi;
2 
3 void websocketEcho(Cgi cgi) {
4 	if(cgi.websocketRequested()) {
5 		if(cgi.origin != "http://arsdnet.net")
6 			throw new Exception("bad origin");
7 		auto websocket = cgi.acceptWebsocket();
8 
9 		websocket.send("hello");
10 		websocket.send(" world!");
11 
12 		auto msg = websocket.recv();
13 		while(msg.opcode != WebSocketOpcode.close) {
14 			if(msg.opcode == WebSocketOpcode.text) {
15 				websocket.send(msg.textData);
16 			} else if(msg.opcode == WebSocketOpcode.binary) {
17 				websocket.send(msg.data);
18 			}
19 
20 			msg = websocket.recv();
21 		}
22 
23 		websocket.close();
24 	} else assert(0, "i want a web socket!");
25 }
26 
27 mixin GenericMain!websocketEcho;

Members

Functions

close
void close(int code = 0, string reason = null)

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(in char[] textData)

Sends a text message through the websocket.

send
void send(in 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

Meta