STTNet
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Public Member Functions | List of all members
stt::network::WebSocketClient Class Reference

Websocket client operation class. More...

#include <sttnet_English.h>

Inheritance diagram for stt::network::WebSocketClient:
stt::network::TcpClient

Public Member Functions

 WebSocketClient (const bool &TLS=false, const char *ca="", const char *cert="", const char *key="", const char *passwd="")
 Constructor of WebSocketClient class. More...
 
void setFunction (std::function< bool(const std::string &message, WebSocketClient &k)> fc)
 Set the callback function after receiving a message from the server Register a callback function. More...
 
bool connect (const std::string &url, const int &min=20)
 Connect to a websocket server. More...
 
bool sendMessage (const std::string &message, const std::string &type="0001")
 Send a WebSocket message. More...
 
void close (const std::string &closeCodeAndMessage, const bool &wait=true)
 Send a close frame and close the WebSocket connection (simplified method) More...
 
void close (const short &code=1000, const std::string &message="bye", const bool &wait=true)
 Send a close frame and close the WebSocket connection (standard method) More...
 
bool isConnect ()
 Return the connection status. More...
 
std::string getUrl ()
 Return the url if connected to the server. More...
 
std::string getServerIp ()
 Return the server ip if connected to the server. More...
 
std::string getServerPort ()
 Return the server port if connected to the server. More...
 
 ~WebSocketClient ()
 Destructor of WebSocketClient class, gracefully disconnects when destroying the object. More...
 

Detailed Description

Websocket client operation class.

Constructor & Destructor Documentation

stt::network::WebSocketClient::WebSocketClient ( const bool &  TLS = false,
const char *  ca = "",
const char *  cert = "",
const char *  key = "",
const char *  passwd = "" 
)
inline

Constructor of WebSocketClient class.

Parameters
TLStrue: Enable wss encryption, false: Do not enable wss encryption (default is false)
caPath to CA root certificate (must be filled if TLS encryption is enabled, default is empty)
certPath to client certificate (optional, default is empty)
keyPath to client private key (optional, default is empty)
passwdPassword for private key decryption (optional, default is empty)
Note
  • ca is used to verify whether the server's certificate is trustworthy
  • If wss encryption is enabled, ca is required, others are optional
  • If the server requires client authentication (two-way TLS/SSL), you need to provide a valid client certificate.
stt::network::WebSocketClient::~WebSocketClient ( )

Destructor of WebSocketClient class, gracefully disconnects when destroying the object.

Member Function Documentation

void stt::network::WebSocketClient::close ( const std::string &  closeCodeAndMessage,
const bool &  wait = true 
)

Send a close frame and close the WebSocket connection (simplified method)

Directly pass the encoded close payload, where the first two bytes are the close code (big-endian), followed by the UTF-8 encoded close reason description, for simplified calling.

Parameters
closeCodeAndMessageEncoded close frame payload (2-byte close code + optional message)
waitWhether to wait for the underlying listening thread/epoll event processing to exit
void stt::network::WebSocketClient::close ( const short &  code = 1000,
const std::string &  message = "bye",
const bool &  wait = true 
)

Send a close frame and close the WebSocket connection (standard method)

Build a close frame (opcode = 0x8) that conforms to RFC 6455, with the frame payload containing the close code (2 bytes) and an optional close reason string.

Parameters
codeWebSocket close code, common ones include:
  • 1000: Normal Closure
  • 1001: Going Away
  • 1002: Protocol Error
  • 1003: Unsupported Data
  • 1006: Abnormal Closure (no close frame, used internally by the program)
  • 1008: Policy Violation
  • 1011: Internal Error
messageOptional close reason for debugging or logging
waitWhether to wait for the underlying listening thread/epoll event processing to exit
bool stt::network::WebSocketClient::connect ( const std::string &  url,
const int &  min = 20 
)

Connect to a websocket server.

Parameters
urlComplete ws/wss url (note that the port and path need to be specified explicitly) e.g., wss://google.com should be written as wss://google.com:443/ (complete with :443 and /)
minHeartbeat time in minutes (default is 20 minutes)
Warning
Requires a complete ws/wss url (note that the port and path need to be specified explicitly) e.g., wss://google.com should be written as wss://google.com:443/
std::string stt::network::WebSocketClient::getServerIp ( )
inline

Return the server ip if connected to the server.

Returns
The server ip
std::string stt::network::WebSocketClient::getServerPort ( )
inline

Return the server port if connected to the server.

Returns
The server port
std::string stt::network::WebSocketClient::getUrl ( )
inline

Return the url if connected to the server.

Returns
The url
bool stt::network::WebSocketClient::isConnect ( )
inline

Return the connection status.

Returns
true: Connected to the server, false: Not connected
bool stt::network::WebSocketClient::sendMessage ( const std::string &  message,
const std::string &  type = "0001" 
)

Send a WebSocket message.

According to the WebSocket protocol, encapsulate and send a masked data frame (clients must use masking), supporting automatic frame format selection based on payload length:

  • payload <= 125 bytes: Use 1-byte length
  • 126 <= payload <= 65535: Use 2-byte extended length (with 126 marker)
  • payload > 65535: Use 8-byte extended length (with 127 marker)
Parameters
messageContent of the message to be sent (already encoded as text or binary)
typeCustom field specifying the message type (usually the WebSocket frame's opcode) Conventional format is "1000" + type, where:
  • "0001" represents text frame (Text Frame)
  • "0010" represents binary frame (Binary Frame)
  • "1000" represents connection close (Close Frame)
  • "1001" represents Ping frame
  • "1010" represents Pong frame Please use according to internal conventions, text frame (Text Frame) is used by default
Returns
true if sending is successful
false if sending fails (may be due to unestablished connection or sending exception)
void stt::network::WebSocketClient::setFunction ( std::function< bool(const std::string &message, WebSocketClient &k)>  fc)
inline

Set the callback function after receiving a message from the server Register a callback function.

Parameters
fcA function or function object for processing logic after receiving a message from the server
  • Parameters: string &message - Message to be processed WebSocketClient &k - Reference to the current object
  • Return: bool - true for successful processing, false for processing failure
Note
The passed function should have the signature bool func(const std::string &message, WebSocketClient &k)
If processing fails, the entire websocket connection will be closed directly

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