Axmol Engine 2.4.0-258ceca
Loading...
Searching...
No Matches
WebSocket Class Reference

WebSocket implementation using yasio. More...

#include <WebSocket.h>

Classes

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

Public Types

enum class  ErrorCode { }
 ErrorCode enum used to represent the error in the websocket. More...
 
enum class  State {
}
 State enum used to represent the Websocket state. More...
 
enum class  ErrorCode { }
 ErrorCode enum used to represent the error in the websocket. More...
 
enum class  State {
}
 State enum used to represent the Websocket state. More...
 

Public Member Functions

 WebSocket ()
 Constructor of WebSocket.
 
virtual ~WebSocket ()
 Destructor of WebSocket.
 
bool open (Delegate *delegate, std::string_view url, std::string_view caFilePath="", std::string_view protocols="")
 The initialized method for websocket.
 
void send (std::string_view message)
 Sends string data to websocket server.
 
void send (const void *data, 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 () const
 Gets current state of connection.
 
std::string_view getUrl () const
 Gets the URL of websocket connection.
 
std::string_view getProtocol () const
 Gets the protocol selected by websocket server.
 
 WebSocket ()
 Constructor of WebSocket.
 
virtual ~WebSocket ()
 Destructor of WebSocket.
 
bool open (Delegate *delegate, std::string_view url, std::string_view caFilePath="", std::string_view protocols="")
 The initialized method for websocket.
 
void send (std::string_view message)
 Sends string data to websocket server.
 
void send (const void *data, 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 () const
 Gets current state of connection.
 
std::string_view getUrl () const
 Gets the URL of websocket connection.
 
std::string_view getProtocol () const
 Gets the protocol selected by websocket server.
 
void setHeaders (const std::vector< std::string > &headers)
 Set custom-defined headers.
 
const std::vector< std::string > & getHeaders () const
 Get custom headers.
 

Detailed Description

WebSocket implementation using yasio.

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

Member Enumeration Documentation

◆ ErrorCode [1/2]

enum class ErrorCode
strong

ErrorCode enum used to represent the error in the websocket.

Enumerator
CONNECTION_FAILURE 

< value 0

NO_SEC_ACCEPT 

< value 1

CONNECTION_FAILURE 

< value 0

NO_SEC_ACCEPT 

< value 1

◆ State [1/2]

enum class State
strong

State enum used to represent the Websocket state.

Enumerator
OPEN 

< value 0

CLOSING 

< value 1

CLOSED 

< value 2

OPEN 

< value 0

CLOSING 

< value 1

CLOSED 

< value 2

◆ ErrorCode [2/2]

enum class ErrorCode
strong

ErrorCode enum used to represent the error in the websocket.

Enumerator
CONNECTION_FAILURE 

< value 0

NO_SEC_ACCEPT 

< value 1

CONNECTION_FAILURE 

< value 0

NO_SEC_ACCEPT 

< value 1

◆ State [2/2]

enum class State
strong

State enum used to represent the Websocket state.

Enumerator
OPEN 

< value 0

CLOSING 

< value 1

CLOSED 

< value 2

OPEN 

< value 0

CLOSING 

< value 1

CLOSED 

< value 2

Constructor & Destructor Documentation

◆ WebSocket() [1/2]

WebSocket ( )

Constructor of WebSocket.

@js ctor

◆ ~WebSocket() [1/2]

virtual ~WebSocket ( )
virtual

Destructor of WebSocket.

@js NA @lua NA

◆ WebSocket() [2/2]

WebSocket ( )

Constructor of WebSocket.

@js ctor

◆ ~WebSocket() [2/2]

virtual ~WebSocket ( )
virtual

Destructor of WebSocket.

@js NA @lua NA

Member Function Documentation

◆ open() [1/2]

bool open ( Delegate * delegate,
std::string_view url,
std::string_view caFilePath = "",
std::string_view protocols = "" )

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
protocolsComma-separated list of sub-protocols that agree with websocket server
Returns
true: Success, false: Failure. @lua NA

◆ send() [1/4]

void send ( std::string_view message)

Sends string data to websocket server.

Parameters
messagestring data. @lua sendstring

◆ send() [2/4]

void send ( const void * data,
unsigned int len )

Sends binary data to websocket server.

Parameters
databinary string data.
lenthe size of binary string data. @lua sendstring

◆ close() [1/2]

void close ( )

Closes the connection to server synchronously.

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

◆ closeAsync() [1/2]

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

◆ getReadyState() [1/2]

State getReadyState ( ) const
inline

Gets current state of connection.

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

◆ open() [2/2]

bool open ( Delegate * delegate,
std::string_view url,
std::string_view caFilePath = "",
std::string_view protocols = "" )

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.
caFilePathThe ca file path for wss connection
protocolsComma-separated list of sub-protocols that agree with websocket server
Returns
true: Success, false: Failure. @lua NA

◆ send() [3/4]

void send ( std::string_view message)

Sends string data to websocket server.

Parameters
messagestring data. @lua sendstring

◆ send() [4/4]

void send ( const void * data,
unsigned int len )

Sends binary data to websocket server.

Parameters
databinary data.
lenthe size of binary string data. @lua sendstring

◆ close() [2/2]

void close ( )

Closes the connection to server synchronously.

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

◆ closeAsync() [2/2]

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

◆ getReadyState() [2/2]

State getReadyState ( ) const
inline

Gets current state of connection.

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

◆ setHeaders()

void setHeaders ( const std::vector< std::string > & headers)
inline

Set custom-defined headers.

Parameters
headersThe string vector of custom-defined headers.

◆ getHeaders()

const std::vector< std::string > & getHeaders ( ) const
inline

Get custom headers.

Returns
std::vector<std::string> the string vector of custom-defined headers.

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