Websocket client operation class.
More...
#include <sttnet_English.h>
|
| | 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...
|
| |
Websocket client operation class.
- If you need to reset the TLS/Https encryption certificate, you currently need to destroy the object and reconstruct it The underlying TCP is blocking by default
| 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
-
| TLS | true: Enable wss encryption, false: Do not enable wss encryption (default is false) |
| ca | Path to CA root certificate (must be filled if TLS encryption is enabled, default is empty) |
| cert | Path to client certificate (optional, default is empty) |
| key | Path to client private key (optional, default is empty) |
| passwd | Password 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.
| 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
-
| closeCodeAndMessage | Encoded close frame payload (2-byte close code + optional message) |
| wait | Whether 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
-
| code | WebSocket 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
|
| message | Optional close reason for debugging or logging |
| wait | Whether 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
-
| url | 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/ (complete with :443 and /) |
| min | Heartbeat 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
-
| message | Content of the message to be sent (already encoded as text or binary) |
| type | Custom 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
-
| fc | A 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: