Cocos2d-x  4.0.0
Classes | Public Types | Public Member Functions | Static Public Member Functions | List of all members
WebSocket Class Reference

WebSocket is wrapper of the libwebsockets-protocol, let the develop could call the websocket easily. More...

Classes

struct  Data
 Data structure for message. More...
 
class  Delegate
 The delegate class is used to process websocket events. More...
 

Public Types

enum  ErrorCode { , CONNECTION_FAILURE, UNKNOWN }
 ErrorCode enum used to represent the error in the websocket. More...
 
enum  State { , OPEN, CLOSING, CLOSED }
 State enum used to represent the Websocket state. More...
 

Public Member Functions

 WebSocket ()
 Constructor of WebSocket.
 
virtual ~WebSocket ()
 Destructor of WebSocket.
 
bool init (const Delegate &delegate, const std::string &url, const std::vector< std::string > *protocols=nullptr, const std::string &caFilePath="")
 The initialized method for websocket.
 
void send (const std::string &message)
 Sends string data to websocket server.
 
void send (const unsigned char *binaryMsg, unsigned int len)
 Sends binary data to websocket server.
 
void close ()
 Closes the connection to server synchronously.
 
void closeAsync ()
 Closes the connection to server asynchronously.
 
State getReadyState ()
 Gets current state of connection.
 
const std::string & getUrl () const
 Gets the URL of websocket connection.
 
const std::string & getProtocol () const
 Gets the protocol selected by websocket server.
 

Static Public Member Functions

static void closeAllConnections ()
 Close all connections and wait for all websocket threads to exit.
 

Detailed Description

WebSocket is wrapper of the libwebsockets-protocol, let the develop could call the websocket easily.

Please note that all public methods of WebSocket have to be invoked on Cocos Thread.

Member Enumeration Documentation

enum ErrorCode

ErrorCode enum used to represent the error in the websocket.

Enumerator
CONNECTION_FAILURE 

< value 0

UNKNOWN 

< value 1

enum State

State enum used to represent the Websocket state.

Enumerator
OPEN 

< value 0

CLOSING 

< value 1

CLOSED 

< value 2

Constructor & Destructor Documentation

WebSocket ( )

Constructor of WebSocket.

ctor

virtual ~WebSocket ( )
virtual

Destructor of WebSocket.

NA NA

Member Function Documentation

static void closeAllConnections ( )
static

Close all connections and wait for all websocket threads to exit.

Note
This method has to be invoked on Cocos Thread
bool init ( const Delegate delegate,
const std::string &  url,
const std::vector< std::string > *  protocols = nullptr,
const std::string &  caFilePath = "" 
)

The initialized method for websocket.

   It needs to be invoked right after websocket instance is allocated.
Parameters
delegateThe delegate which want to receive event from websocket.
urlThe URL of websocket server.
protocolsThe websocket protocols that agree with websocket server
caFilePathThe ca file path for wss connection
Returns
true: Success, false: Failure. NA
void send ( const std::string &  message)

Sends string data to websocket server.

Parameters
messagestring data. sendstring
void send ( const unsigned char *  binaryMsg,
unsigned int  len 
)

Sends binary data to websocket server.

Parameters
binaryMsgbinary string data.
lenthe size of binary string data. sendstring
void close ( )

Closes the connection to server synchronously.

Note
It's a synchronous method, it will not return until websocket thread exits.
void closeAsync ( )

Closes the connection to server asynchronously.

Note
It's an asynchronous method, it just notifies websocket thread to exit and returns directly, If using 'closeAsync' to close websocket connection, be careful of not using destructed variables in the callback of 'onClose'.
State getReadyState ( )

Gets current state of connection.

Returns
State the state value could be State::CONNECTING, State::OPEN, State::CLOSING or State::CLOSED

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