Class WebSocket

A WebSocket. You can use this to send and receive WebSocket messages.

class WebSocket ;
websocket.send("Hello world");      // Send a message to client
auto msg = websocket.receiveMessage();  // Receive a message from client

Constructors

NameDescription
this (socket, role) Create a WebSocket. If you are using this as client, you should pass WebSocket.Role.Client as second parameter.

Fields

NameTypeDescription
onBinaryMessage bool delegate(in ubyte[])Set a callback to be called when a binary message is received. Return true to propagate the message to the next callback.
onCloseMessage bool delegate(in WebSocketMessage)Set a callback to be called when a close message is received. Return true to propagate the message to the next callback.
onMessage bool delegate(in WebSocketMessage)Set a callback to be called when a message is received. Return true to propagate the message to the next callback.
onTextMessage bool delegate(in string)Set a callback to be called when a text message is received. Return true to propagate the message to the next callback.

Methods

NameDescription
isDirty () Returns true if the WebSocket is dirty.
isSendBufferEmpty () If there is any data to send, it will be sent on next send() call
kill (reason) Close the WebSocket connection.
killReason () Why the WebSocket connection was closed?
killRequested () Is the WebSocket connection closed?
receiveMessage () Receive a message from the WebSocket. There could be more than one message in the buffer so you should call this function in a loop until it returns an invalid message.
send () Try to send buffered data, if any. Returns false if there is still data to send. (only for non-blocking sockets)
send (data) Send a message. You can send a string, a basic type or an array of a basic type.
sendClose () Send a close message.
sendMessage (message, flagFIN) Send a custom message. flagFIN is true by default and should be true for most cases. You want to set it to false if you are sending a message in parts. The last part should have flagFIN set to true. The masked parameter is false by default and should be false for most cases. It is used if a message is sent from a client to a server.
sendPing () Send a ping message. The peer should reply with a pong.
socket () Return the underlying socket.