Status & Roadmap
Authors & License
Funding U++
Search on this site

SourceForge.net Logo

SourceForge.net Logo

GitHub Logo

Discord Logo



class WebSocket

Provides the WebSocket protocol support.



Public Method List


WebSocket& NonBlocking(bool b = true)

If b is true, activates non-blocking mode. Default is blocking mode.



WebSocket& Headers(const String& h)

Replaces HTTP request headers for Connect method with h. Note that "Host:" and "Sec-WebSocket-Key:" headers are fixed, as they have to be generated during Connect and so should not be in h. Headers are supplied as text, should be "\r\n" separated. The whole text should end with "\r\n".



WebSocket& ClearHeaders()

Clears HTTP request headers for Connect.



WebSocket& AddHeaders(const String& h)

Adds text to HTTP request headers for Connect.



WebSocket& Header(const char *id, const String& data)

Adds single header id with value data to HTTP request headers for Connect.



String GetHeaders()

Returns current HTTP request headers for Connect.



bool IsBlocking() const

Returns true if WebSocket is in the blocking mode.



bool IsError() const

Returns true if there was any error.



String GetError() const

Returns the description of error.



bool Accept(TcpSocket& listen_socket)

Accepts connection from listen_socket. Returns true on success.



bool Connect(const String& uri_, const String& host, bool ssl, int port)

bool Connect(const String& uri, const String& host, bool ssl)

bool Connect(const String& url)

Initiates connection to url. In blocking mode returns false if connection cannot be established, otherwise true. Variant where port is not specified uses 440 if ssl and 80 if not. Variant with single url parameter derives all other parameters from url.



void Do()

Manages socket operations in non-blocking mode. Cannot be called in blocking mode.



String Receive()

Receives the message. In blocking mode waits until the message is available. In non-blocking mode returns either message or String::GetVoid() if there is no message. In both cases also returns String::GetVoid() in case of exceptional situations (errors, connection closed etc...). Note that in non-blocking mode Receive calls Do, so there is no need to call it separately.



bool IsFin() const

Returns true if the last received message had flag FIN set.



bool IsText() const

Returns true if the last received message was text.



bool IsBinary() const

Returns true if the last received message was text.



void SendText(const String& data)

Sends a single frame (non-fragmented) text message.



void SendTextMasked(const String& data)

Sends masked text message.



void SendBinary(const String& data)

Sends a single frame (non-fragmented) binary message.



void Ping(const String& data)

Sends PING.



void BeginText(const String& data)

Starts a multi-frame (fragmented) text message.



void BeginBinary(const String& data)

Starts a multi-frame (fragmented) binary message.



void Continue(const String& data)

Sends another chunk of data in a multi-frame (fragmented) message.



void Fin(const String& data)

Sends the final chunk of data in a multi-frame (fragmented) message.



void Close(const String& msg, bool wait_reply)

Initiates standard close of connection, sending CLOSE msg. If wait_reply is true, waits for the reply from the server.



bool IsOpen()

Returns true if the socket is open.



bool IsClosed() const

Returns true if the socket is closed.


dword GetWaitEvents() const

Returns a combination of WAIT_READ and WAIT_WRITE flags to indicate what is blocking the operation of WebSocket. Can be used with SocketWaitEvent.




Is there associated socket, returns its handle. Otherwise returns NULL.



void AddTo(SocketWaitEvent& e)

Adds WebSocket to SocketWaitEvent for waiting on it.



void Trace(bool b)

Activates tracing log.



Do you want to contribute?