Sming Framework API
Sming - Open Source framework for high efficiency WiFi SoC ESP8266 native development with C++ language.
WebsocketClient Class Reference

Websocket Client. More...

#include <WebsocketClient.h>

Inheritance diagram for WebsocketClient:
WebsocketConnection

Public Member Functions

 WebsocketClient (HttpConnection *connection)
 
HttpConnectiongetHttpConnection ()
 
bool connect (const Url &url, uint32_t sslOptions=0)
 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 disconnect () SMING_DEPRECATED
 Disconnects websocket client from server. More...
 

Protected Member Functions

int verifyKey (HttpConnection &connection, HttpResponse &response)
 
bool bind (HttpRequest &request, HttpResponse &response)
 Binds websocket connection to an http server connection. More...
 
virtual void send (const char *message, size_t length, ws_frame_type_t type=WS_FRAME_TEXT)
 Sends a websocket message from a buffer. More...
 
void send (const String &message, ws_frame_type_t type=WS_FRAME_TEXT)
 Sends websocket message from a String. More...
 
void sendString (const String &message)
 Sends a string websocket message. More...
 
void 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 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...
 
bool processFrame (TcpClient &client, char *at, int size)
 Callback handler to process a received TCP data frame. More...
 
size_t encodeFrame (ws_frame_type_t type, const char *inData, size_t inLength, char *outData, size_t outLength, bool useMask=true, bool isFin=true)
 Encode user content into a valid websocket frame. More...
 

Static Protected Member Functions

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...
 
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)
 

Protected Attributes

WebsocketDelegate wsConnect = nullptr
 
WebsocketMessageDelegate wsMessage = nullptr
 
WebsocketBinaryDelegate wsBinary = nullptr
 
WebsocketDelegate wsDisconnect = nullptr
 
void * userData = nullptr
 
WsConnectionState state = eWSCS_Ready
 

Detailed Description

Websocket Client.

Member Function Documentation

void WebsocketConnection::activate ( )
inherited

Should be called after a websocket connection is established to activate the websocket parser and allow sending of websocket data.

bool WebsocketConnection::bind ( HttpRequest request,
HttpResponse response 
)
inherited

Binds websocket connection to an http server connection.

Parameters
request
response
Return values
booltrue on success, false otherwise
static void WebsocketConnection::broadcast ( const char *  message,
size_t  length,
ws_frame_type_t  type = WS_FRAME_TEXT 
)
staticinherited

Broadcasts a message to all active websocket connections.

Parameters
message
length
type
static void WebsocketConnection::broadcast ( const String message,
ws_frame_type_t  type = WS_FRAME_TEXT 
)
inlinestaticinherited

Broadcasts a message to all active websocket connections.

Parameters
message
type
void WebsocketConnection::close ( )
inherited

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

bool WebsocketClient::connect ( const Url url,
uint32_t  sslOptions = 0 
)

Connects websocket client to server.

Parameters
urlUrl address of websocket server
sslOptionsSpecify the SSL options to be used when calling websocket server over SSL
void WebsocketClient::disconnect ( )
inline

Disconnects websocket client from server.

Deprecated:
Use close() instead
size_t WebsocketConnection::encodeFrame ( ws_frame_type_t  type,
const char *  inData,
size_t  inLength,
char *  outData,
size_t  outLength,
bool  useMask = true,
bool  isFin = true 
)
protectedinherited

Encode user content into a valid websocket frame.

Parameters
type
inData
inLength
outData
outLength
useMaskMUST be true for client connections
isFintrue if this is the final frame
Return values
size_tSize of encoded frame
static const WebsocketList& WebsocketConnection::getActiveWebsockets ( )
inlinestaticinherited

Obtain the list of active websockets.

Return values
constWebsocketList&
Note
Return value is const as only restricted operations should be carried out on the list.
HttpConnection* WebsocketConnection::getConnection ( )
inlineinherited

Gets the underlying HTTP connection.

Return values
HttpConnection*
WsConnectionState WebsocketConnection::getState ( )
inlineinherited

Gets the state of the websocket connection.

Return values
WsConnectionState
void* WebsocketConnection::getUserData ( )
inlineinherited

Retrieves user data attached.

Return values
void*The user data previously set by setUserData()
bool WebsocketConnection::onConnected ( )
inherited

Call this method when the websocket connection was (re)activated.

Return values
booltrue on success
bool WebsocketConnection::operator== ( const WebsocketConnection rhs) const
inlineinherited

Test if another connection refers to the same object.

Parameters
rhsThe other WebsocketConnection to compare with
Return values
bool
bool WebsocketConnection::processFrame ( TcpClient client,
char *  at,
int  size 
)
protectedinherited

Callback handler to process a received TCP data frame.

Parameters
client
at
size
Return values
booltrue if data parsing successful
void WebsocketConnection::reset ( )
inherited

Resets a websocket connection.

virtual void WebsocketConnection::send ( const char *  message,
size_t  length,
ws_frame_type_t  type = WS_FRAME_TEXT 
)
virtualinherited

Sends a websocket message from a buffer.

Parameters
message
lengthQuantity of data in message
type
void WebsocketConnection::send ( const String message,
ws_frame_type_t  type = WS_FRAME_TEXT 
)
inlineinherited

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
void WebsocketConnection::sendBinary ( const uint8_t *  data,
size_t  length 
)
inlineinherited

Sends a binary websocket message.

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

Send websocket ping to server.

Parameters
Stringpayload - maximum 255 bytes
Return values
booltrue if the data can be send, false otherwise
void WebsocketClient::sendPong ( const String payload = nullptr)
inline

Send websocket ping to server.

Parameters
String&payload - maximum 255 bytes
Return values
booltrue if the data can be send, false otherwise
void WebsocketConnection::sendString ( const String message)
inlineinherited

Sends a string websocket message.

Parameters
message
void WebsocketConnection::setBinaryHandler ( WebsocketBinaryDelegate  handler)
inlineinherited

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

Parameters
handler
void WebsocketConnection::setConnection ( HttpConnection connection,
bool  isClientConnection = true 
)
inlineinherited

Sets the underlying (transport ) HTTP connection.

Parameters
connectionthe transport connection
isClientConnectiontrue when the passed connection is an http client conneciton
void WebsocketConnection::setConnectionHandler ( WebsocketDelegate  handler)
inlineinherited

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

Parameters
handler
void WebsocketConnection::setDisconnectionHandler ( WebsocketDelegate  handler)
inlineinherited

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

Parameters
handler
void WebsocketConnection::setMessageHandler ( WebsocketMessageDelegate  handler)
inlineinherited

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

Parameters
handler
void WebsocketConnection::setUserData ( void *  userData)
inlineinherited

Attaches a user data to a websocket connection.

Parameters
userData