WebSocketServer server operation class. More...
#include <sttnet_English.h>
Public Member Functions | |
| WebSocketServer (const int &maxFD=10000000, const bool &security_open=true, const int &connectionNumLimit=20, const int &connectionRateLimit=6, const int &buffer_size=8, const int &requestRate=12) | |
| Constructor, which is enabled by default. Limit the maximum number of connections to an IP address to 20; The fastest connection speed per second for the same IP address is 6. More... | |
| void | setStartFunction (std::function< void(const WebSocketFDInformation &inf, WebSocketServer &k)> fccc) |
| Set the callback function to be executed after a websocket connection is successful Register a callback function. More... | |
| void | setJudgeFunction (std::function< bool(const WebSocketFDInformation &k)> fcc) |
| Set the handshake phase check function for websocket, only execute subsequent handshake if check passes Register a callback function. More... | |
| void | setFunction (std::function< bool(const std::string &msg, WebSocketServer &k, const WebSocketFDInformation &inf)> fc) |
| Set the callback function after receiving a message from the client Register a callback function. More... | |
| void | setTimeOutTime (const int &seca) |
| Set heartbeat time. More... | |
| void | setHBTimeOutTime (const int &secb) |
| Set the waiting time after sending a heartbeat. More... | |
| bool | close (const int &fd, const std::string &closeCodeAndMessage) |
| Send a close frame to close the WebSocket connection of the corresponding socket (simplified method) More... | |
| bool | close (const int &fd, const short &code=1000, const std::string &message="bye") |
| Send a close frame to close the WebSocket connection of the corresponding socket (standard method) More... | |
| bool | sendMessage (const int &fd, const std::string &msg, const std::string &type="0001") |
| Send a WebSocket message to a specific client. More... | |
| bool | close () |
| Close listening and all connections. More... | |
| bool | startListen (const int &port, const int &threads=8) |
| Open the Websocket server listening program. More... | |
| void | sendMessage (const std::string &msg, const std::string &type="0001") |
| Broadcast send a WebSocket message. More... | |
| ~WebSocketServer () | |
| Destructor of WebSocketServer. More... | |
Public Member Functions inherited from stt::network::TcpServer | |
| TcpServer (const int &maxFD=10000, const bool &security_open=true, const int &connectionNumLimit=20, const int &connectionRateLimit=6, const int &buffer_size=8, const int &requestRate=12, const int &checkFrequency=1, const int &connectionTimeout=1800) | |
| Constructor, which is enabled by default. Limit the maximum number of connections to an IP address to 20; The fastest connection speed per second for the same IP address is 6. More... | |
| bool | startListen (const int &port, const int &threads=8) |
| Start the TCP server listening program. More... | |
| bool | setTLS (const char *cert, const char *key, const char *passwd, const char *ca) |
| Enable TLS encryption and configure server-side certificate and key. More... | |
| void | redrawTLS () |
| Revoke TLS encryption, CA certificate, etc. More... | |
| bool | setFunction (std::function< bool(TcpFDHandler &k, TcpFDInf &inf)> fc) |
| Set the callback function after receiving a message from the client Register a callback function. More... | |
| bool | stopListen () |
| Stop listening. More... | |
| bool | close () |
| Close listening and all connected sockets. More... | |
| bool | close (const int &fd) |
| Close the connection of a specific socket. More... | |
| bool | isListen () |
| Return the listening status of the object. More... | |
| SSL * | getSSL (const int &fd) |
| Query the connection with the server, pass in the socket, and return the encrypted SSL handle. More... | |
| ~TcpServer () | |
| Destructor of TcpServer class. More... | |
Additional Inherited Members | |
Protected Member Functions inherited from stt::network::TcpServer | |
| bool | allowRequest (const int &cclientfd) |
| void | connectionDetect () |
Protected Attributes inherited from stt::network::TcpServer | |
| unsigned long | buffer_size |
| int | maxFD |
| security::ConnectionLimiter | connectionLimiter |
| TcpFDInf * | clientfd |
| int | flag1 = true |
| std::queue< QueueFD > * | fdQueue |
| std::mutex * | lq1 |
| std::condition_variable * | cv |
| int | consumerNum |
| std::mutex | lco1 |
| bool | unblock |
| SSL_CTX * | ctx = nullptr |
| bool | TLS = false |
| int | requestRate |
| int | checkFrequency |
| int | connectionTimeout |
| bool | security_open |
WebSocketServer server operation class.
|
inline |
Constructor, which is enabled by default. Limit the maximum number of connections to an IP address to 20; The fastest connection speed per second for the same IP address is 6.
| maxFD | service object can accept the maximum number of connections |
| security_open | true: enable the security module false: disable the security module (enabled by default) |
| connectionNumLimit | The maximum number of connections from the same IP address |
| connectionRateLimit | The maximum number of connections per second to the same IP address |
| buffer_size | The maximum amount of data allowed to be transferred over the same connection (in KB) is 8KB by default |
|
inline |
Destructor of WebSocketServer.
| bool stt::network::WebSocketServer::close | ( | const int & | fd, |
| const std::string & | closeCodeAndMessage | ||
| ) |
Send a close frame to close the WebSocket connection of the corresponding socket (simplified method)
Pass in the socket fd to close the connection 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.
| fd | Socket fd |
| closeCodeAndMessage | Encoded close frame payload (2-byte close code + optional message) |
| bool stt::network::WebSocketServer::close | ( | const int & | fd, |
| const short & | code = 1000, |
||
| const std::string & | message = "bye" |
||
| ) |
Send a close frame to close the WebSocket connection of the corresponding socket (standard method)
Pass in the socket fd to close the connection 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.
| fd | Socket fd |
| code | WebSocket close code, common ones include:
|
| message | Optional close reason for debugging or logging |
| bool stt::network::WebSocketServer::close | ( | ) |
Close listening and all connections.
|
inline |
Send a WebSocket message to a specific client.
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:
| fd | Socket connected to the client |
| msg | Message content 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:
|
| void stt::network::WebSocketServer::sendMessage | ( | const std::string & | msg, |
| const std::string & | type = "0001" |
||
| ) |
Broadcast send a WebSocket message.
Broadcast and send messages to all clients 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:
| msg | Message content 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:
|
|
inline |
Set the callback function after receiving a message from the client Register a callback function.
| fc | A function or function object for processing logic after receiving a message from the client
|
|
inline |
Set the waiting time after sending a heartbeat.
| secb | Waiting time after sending a heartbeat in seconds. Default is 30 seconds if not set |
|
inline |
Set the handshake phase check function for websocket, only execute subsequent handshake if check passes Register a callback function.
| fc | A function or function object to be executed after a websocket connection is successful
|
|
inline |
Set the callback function to be executed after a websocket connection is successful Register a callback function.
| fc | A function or function object to be executed after a websocket connection is successful
|
|
inline |
Set heartbeat time.
| seca | Heartbeat time in minutes. Default is 20 minutes if not set |
|
inline |
Open the Websocket server listening program.
| port | Port to listen on |
| threads | Number of consumer threads (default is 8) |
1.8.5