Cocos2d-x  v3.12
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Groups
WebSocket Class Reference

@ WebSocket 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 ()
 ctor ()
 WebSocket ()
virtual ~WebSocket ()
 Destructor of WebSocket.
 
bool init (const Delegate &delegate, const std::string &url, const std::vector< std::string > *protocols=nullptr)
 The initialized method for websocket. More...
 
void send (const std::string &message)
 Sends string data to websocket server. More...
 
void send (const unsigned char *binaryMsg, unsigned int len)
 Sends binary data to websocket server. More...
 
var send ( var binaryMsg, var len)
 Sends binary data to websocket server. More...
 
local sendstring ( local binaryMsg, local len)
 Sends binary data to websocket server. More...
 
void close ()
 Closes the connection to server.
 
var close ()
 Closes the connection to server.
 
local close ()
 Closes the connection to server.
 
void closeAsync ()
 Closes the connection to server asynchronously. More...
 
var closeAsync ()
 Closes the connection to server asynchronously. More...
 
local closeAsync ()
 Closes the connection to server asynchronously. More...
 
State getReadyState ()
 Gets current state of connection. More...
 

Static Public Member Functions

static void closeAllConnections ()
 Close all connections and wait for all websocket threads to exit This method has to be invoked on Cocos Thread.
 
var closeAllConnections ()
 Close all connections and wait for all websocket threads to exit This method has to be invoked on Cocos Thread.
 
local closeAllConnections ()
 Close all connections and wait for all websocket threads to exit This method has to be invoked on Cocos Thread.
 

Detailed Description

@ WebSocket

WebSocket is wrapper of the libwebsockets-protocol, let the developer could call the websocket easily. Please note that all public methods of WebSocket have to be invoked on Cocos Thread.

Constructor & Destructor Documentation

WebSocket ( )
var ctor ( )
local WebSocket ( )

Member Function Documentation

bool init ( const Delegate delegate,
const std::string &  url,
const std::vector< std::string > *  protocols = nullptr 
)

The initialized method for websocket.

It needs to be invoked right after websocket instance is allocated. The delegate which want to receive event from websocket. The URL of websocket server. The protocol of the socket. true: Success, false: Failure.

var init ( var  delegate,
var  url,
var  protocols = nullptr 
)

The initialized method for websocket.

It needs to be invoked right after websocket instance is allocated. The delegate which want to receive event from websocket. The URL of websocket server. The protocol of the socket. true: Success, false: Failure.

void send ( const std::string &  message)

Sends string data to websocket server.

string data.

var send ( var  message)

Sends string data to websocket server.

string data.

local sendstring ( local  message)

Sends string data to websocket server.

string data.

void send ( const unsigned char *  binaryMsg,
unsigned int  len 
)

Sends binary data to websocket server.

binary string data. the size of binary string data.

var send ( var  binaryMsg,
var  len 
)

Sends binary data to websocket server.

binary string data. the size of binary string data.

local sendstring ( local  binaryMsg,
local  len 
)

Sends binary data to websocket server.

binary string data. the size of binary string data.

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'.
var 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'.
local 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.

State the state value coule be State::CONNECTING, State::OPEN, State::CLOSING or State::CLOSED.

var getReadyState ( )

Gets current state of connection.

State the state value coule be State::CONNECTING, State::OPEN, State::CLOSING or State::CLOSED.

local getReadyState ( )

Gets current state of connection.

State the state value coule be State::CONNECTING, State::OPEN, State::CLOSING or State::CLOSED.


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