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

WebSocketServer server operation class. More...

#include <sttnet_English.h>

Inheritance diagram for stt::network::WebSocketServer:
stt::network::TcpServer

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
 
TcpFDInfclientfd
 
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
 

Detailed Description

WebSocketServer server operation class.

Constructor & Destructor Documentation

stt::network::WebSocketServer::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 
)
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.

Note
Turning on the security module has a performance impact
Parameters
maxFDservice object can accept the maximum number of connections
security_opentrue: enable the security module false: disable the security module (enabled by default)
connectionNumLimitThe maximum number of connections from the same IP address
connectionRateLimitThe maximum number of connections per second to the same IP address
buffer_sizeThe maximum amount of data allowed to be transferred over the same connection (in KB) is 8KB by default
stt::network::WebSocketServer::~WebSocketServer ( )
inline

Destructor of WebSocketServer.

Note
When destroying the object, it will block until all connections and listening are closed

Member Function Documentation

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.

Parameters
fdSocket fd
closeCodeAndMessageEncoded close frame payload (2-byte close code + optional message)
Returns
true: Closed successfully, false: Failed to close
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.

Parameters
fdSocket fd
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
Returns
true: Closed successfully, false: Failed to close
bool stt::network::WebSocketServer::close ( )

Close listening and all connections.

Note
Will block until all are closed
bool stt::network::WebSocketServer::sendMessage ( const int &  fd,
const std::string &  msg,
const std::string &  type = "0001" 
)
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:

  • 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
fdSocket connected to the client
msgMessage content 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::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:

  • 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
msgMessage content 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
void stt::network::WebSocketServer::setFunction ( std::function< bool(const std::string &msg, WebSocketServer &k, const WebSocketFDInformation &inf)>  fc)
inline

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

Parameters
fcA function or function object for processing logic after receiving a message from the client
  • Parameters: string &message - Socket to be processed WebsocketClient &k - Reference to the current object ,WebSocketFDInformation &inf - restore client's information
  • 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
void stt::network::WebSocketServer::setHBTimeOutTime ( const int &  secb)
inline

Set the waiting time after sending a heartbeat.

Parameters
secbWaiting time after sending a heartbeat in seconds. Default is 30 seconds if not set
Note
If the peer does not respond within this time, the connection will be closed
void stt::network::WebSocketServer::setJudgeFunction ( std::function< bool(const WebSocketFDInformation &k)>  fcc)
inline

Set the handshake phase check function for websocket, only execute subsequent handshake if check passes Register a callback function.

Parameters
fcA function or function object to be executed after a websocket connection is successful
  • Parameters: const WebSocketFDInformation &inf - Websocket server information
  • Return value: true: Check passed, false: Check failed
Note
The passed function should have the signature bool func(const WebSocketFDInformation &k)
Only execute subsequent handshake if check passes, otherwise disconnect
void stt::network::WebSocketServer::setStartFunction ( std::function< void(const WebSocketFDInformation &inf, WebSocketServer &k)>  fccc)
inline

Set the callback function to be executed after a websocket connection is successful Register a callback function.

Parameters
fcA function or function object to be executed after a websocket connection is successful
Note
The passed function should have the signature void func(const WebSocketFDInformation &inf, WebSocketServer &k)
void stt::network::WebSocketServer::setTimeOutTime ( const int &  seca)
inline

Set heartbeat time.

Parameters
secaHeartbeat time in minutes. Default is 20 minutes if not set
bool stt::network::WebSocketServer::startListen ( const int &  port,
const int &  threads = 8 
)
inline

Open the Websocket server listening program.

Parameters
portPort to listen on
threadsNumber of consumer threads (default is 8)
Returns
true: Listening started successfully, false: Failed to start listening

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