Websocket Client. More...

#include <WebsocketClient.h>

Inheritance diagram for WebsocketClient:
Collaboration diagram for WebsocketClient:

Public Member Functions

 WebsocketClient (HttpConnection *connection=nullptr)
 
HttpConnectiongetHttpConnection ()
 
bool connect (const Url &url)
 Connects websocket client to server. More...
 
void sendPing (const String &payload=nullptr)
 Send websocket ping to server. More...
 
void sendPong (const String &payload=nullptr)
 Send websocket ping to server. More...
 
void setSslInitHandler (Ssl::Session::InitDelegate handler)
 Set the SSL session initialisation callback. More...
 
void setBinaryHandler (WebsocketBinaryDelegate handler)
 Sets the callback handler to be called after a binary websocket message is received. More...
 
void setConnectionHandler (WebsocketDelegate handler)
 Sets the callback handler to be called after successful websocket connection. More...
 
void setDisconnectionHandler (WebsocketDelegate handler)
 Sets the callback handler to be called before closing a websocket connection. More...
 
void setMessageHandler (WebsocketMessageDelegate handler)
 Sets the callback handler to be called after a websocket message is received. More...
 
bool send (const char *message, size_t length, ws_frame_type_t type=WS_FRAME_TEXT)
 Sends a websocket message from a buffer. More...
 
bool send (const String &message, ws_frame_type_t type=WS_FRAME_TEXT)
 Sends websocket message from a String. More...
 
bool send (IDataSourceStream *source, ws_frame_type_t type=WS_FRAME_TEXT, bool useMask=false, bool isFin=true)
 Sends websocket message from a stream. More...
 
bool sendBinary (const uint8_t *data, size_t length)
 Sends a binary websocket message. More...
 
bool sendString (const String &message)
 Sends a string websocket message. More...
 
void close ()
 Closes a websocket connection (without closing the underlying http connection) More...
 
WsConnectionState getState ()
 Gets the state of the websocket connection. More...
 

Protected Member Functions

int verifyKey (HttpConnection &connection, HttpResponse &response)
 
- Protected Member Functions inherited from WebsocketConnection
bool processFrame (TcpClient &client, char *at, int size)
 Callback handler to process a received TCP data frame. More...
 
 WebsocketConnection (HttpConnection *connection=nullptr, bool isClientConnection=true)
 Constructs a websocket connection on top of http client or server connection. More...
 
virtual ~WebsocketConnection ()
 
bool bind (HttpRequest &request, HttpResponse &response)
 Binds websocket connection to an http server connection. More...
 
bool send (const char *message, size_t length, ws_frame_type_t type=WS_FRAME_TEXT)
 Sends a websocket message from a buffer. More...
 
bool send (const String &message, ws_frame_type_t type=WS_FRAME_TEXT)
 Sends websocket message from a String. More...
 
bool send (IDataSourceStream *source, ws_frame_type_t type=WS_FRAME_TEXT, bool useMask=false, bool isFin=true)
 Sends websocket message from a stream. More...
 
bool sendString (const String &message)
 Sends a string websocket message. More...
 
bool sendBinary (const uint8_t *data, size_t length)
 Sends a binary websocket message. More...
 
void close ()
 Closes a websocket connection (without closing the underlying http connection) More...
 
void reset ()
 Resets a websocket connection. More...
 
void setUserData (void *userData)
 Attaches a user data to a websocket connection. More...
 
void * getUserData ()
 Retrieves user data attached. More...
 
bool operator== (const WebsocketConnection &rhs) const
 Test if another connection refers to the same object. More...
 
void setConnectionHandler (WebsocketDelegate handler)
 Sets the callback handler to be called after successful websocket connection. More...
 
void setMessageHandler (WebsocketMessageDelegate handler)
 Sets the callback handler to be called after a websocket message is received. More...
 
void setBinaryHandler (WebsocketBinaryDelegate handler)
 Sets the callback handler to be called after a binary websocket message is received. More...
 
void setPongHandler (WebsocketDelegate handler)
 Sets the callback handler to be called when pong reply received. More...
 
void setDisconnectionHandler (WebsocketDelegate handler)
 Sets the callback handler to be called before closing a websocket connection. More...
 
void activate ()
 Should be called after a websocket connection is established to activate the websocket parser and allow sending of websocket data. More...
 
bool onConnected ()
 Call this method when the websocket connection was (re)activated. More...
 
HttpConnectiongetConnection ()
 Gets the underlying HTTP connection. More...
 
void setConnection (HttpConnection *connection, bool isClientConnection=true)
 Sets the underlying (transport ) HTTP connection. More...
 
WsConnectionState getState ()
 Gets the state of the websocket connection. More...
 

Additional Inherited Members

- Static Protected Member Functions inherited from WebsocketConnection
static int staticOnDataBegin (void *userData, ws_frame_type_t type)
 
static int staticOnDataPayload (void *userData, const char *at, size_t length)
 
static int staticOnDataEnd (void *userData)
 
static int staticOnControlBegin (void *userData, ws_frame_type_t type)
 
static int staticOnControlPayload (void *userData, const char *, size_t length)
 
static int staticOnControlEnd (void *userData)
 
static void broadcast (const char *message, size_t length, ws_frame_type_t type=WS_FRAME_TEXT)
 Broadcasts a message to all active websocket connections. More...
 
static void broadcast (const String &message, ws_frame_type_t type=WS_FRAME_TEXT)
 Broadcasts a message to all active websocket connections. More...
 
static const WebsocketListgetActiveWebsockets ()
 Obtain the list of active websockets. More...
 
- Protected Attributes inherited from WebsocketConnection
WebsocketDelegate wsConnect = nullptr
 
WebsocketMessageDelegate wsMessage = nullptr
 
WebsocketBinaryDelegate wsBinary = nullptr
 
WebsocketDelegate wsPong = nullptr
 
WebsocketDelegate wsDisconnect = nullptr
 
void * userData = nullptr
 
WsConnectionState state
 

Detailed Description

Websocket Client.

Constructor & Destructor Documentation

◆ WebsocketClient()

WebsocketClient::WebsocketClient ( HttpConnection connection = nullptr)
inline

Member Function Documentation

◆ close()

void WebsocketConnection::close

Closes a websocket connection (without closing the underlying http connection)

◆ connect()

bool WebsocketClient::connect ( const Url url)

Connects websocket client to server.

Parameters
urlUrl address of websocket server

◆ getHttpConnection()

HttpConnection* WebsocketClient::getHttpConnection ( )

◆ getState()

WsConnectionState WebsocketConnection::getState
inline

Gets the state of the websocket connection.

Return values
WsConnectionState

◆ send() [1/3]

bool WebsocketConnection::send

Sends a websocket message from a buffer.

Parameters
message
lengthQuantity of data in message
type

◆ send() [2/3]

bool WebsocketConnection::send
inline

Sends websocket message from a String.

Parameters
messageString
type
Note
A String may contain arbitrary data, not just text, so can use this for any frame type

◆ send() [3/3]

bool WebsocketConnection::send

Sends websocket message from a stream.

Parameters
sourceThe stream to send - we get ownership of the stream
type
useMaskMUST be true for client connections
isFintrue if this is the final frame
Return values
booltrue on success

◆ sendBinary()

bool WebsocketConnection::sendBinary
inline

Sends a binary websocket message.

Parameters
data
length

◆ sendPing()

void WebsocketClient::sendPing ( const String payload = nullptr)
inline

Send websocket ping to server.

Parameters
payloadMaximum 255 bytes
Return values
booltrue if the data can be send, false otherwise

◆ sendPong()

void WebsocketClient::sendPong ( const String payload = nullptr)
inline

Send websocket ping to server.

Parameters
payloadMaximum 255 bytes
Return values
booltrue if the data can be send, false otherwise

◆ sendString()

bool WebsocketConnection::sendString
inline

Sends a string websocket message.

Parameters
message

◆ setBinaryHandler()

void WebsocketConnection::setBinaryHandler
inline

Sets the callback handler to be called after a binary websocket message is received.

Parameters
handler

◆ setConnectionHandler()

void WebsocketConnection::setConnectionHandler
inline

Sets the callback handler to be called after successful websocket connection.

Parameters
handler

◆ setDisconnectionHandler()

void WebsocketConnection::setDisconnectionHandler
inline

Sets the callback handler to be called before closing a websocket connection.

Parameters
handler

◆ setMessageHandler()

void WebsocketConnection::setMessageHandler
inline

Sets the callback handler to be called after a websocket message is received.

Parameters
handler

◆ setSslInitHandler()

void WebsocketClient::setSslInitHandler ( Ssl::Session::InitDelegate  handler)
inline

Set the SSL session initialisation callback.

Parameters
handler

◆ verifyKey()

int WebsocketClient::verifyKey ( HttpConnection connection,
HttpResponse response 
)
protected

The documentation for this class was generated from the following file: