WebSocketServer服务端操作类 更多...
#include <sttnet.h>
Public 成员函数 | |
| 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) | |
| 构造函数,默认是启用安全模块。限制一个ip最大连接为20;同一个ip每秒最快连接速度为6 更多... | |
| void | setStartFunction (std::function< void(const WebSocketFDInformation &inf, WebSocketServer &k)> fccc) |
| 设置websocket连接成功后就执行的回调函数 注册一个回调函数 更多... | |
| void | setJudgeFunction (std::function< bool(const WebSocketFDInformation &k)> fcc) |
| 设置websocket握手阶段的检查函数,只有检查通过才执行后续握手 注册一个回调函数 更多... | |
| void | setFunction (std::function< bool(const std::string &msg, WebSocketServer &k, const WebSocketFDInformation &inf)> fc) |
| 设置收到客户端消息后的回调函数 注册一个回调函数 更多... | |
| void | setTimeOutTime (const int &seca) |
| 设置心跳时间 更多... | |
| void | setHBTimeOutTime (const int &secb) |
| 设置发送心跳后的等待时间 更多... | |
| bool | close (const int &fd, const std::string &closeCodeAndMessage) |
| 发送关闭帧关闭对应套接字的 WebSocket 连接(简化方式) 更多... | |
| bool | close (const int &fd, const short &code=1000, const std::string &message="bye") |
| 发送关闭帧关闭对应套接字的 WebSocket 连接(标准方式) 更多... | |
| bool | sendMessage (const int &fd, const std::string &msg, const std::string &type="0001") |
| 发送 WebSocket 消息给某一个客户端 更多... | |
| bool | close () |
| 关闭监听和所有连接 更多... | |
| bool | startListen (const int &port, const int &threads=8) |
| 打开Websocket服务器监听程序 更多... | |
| void | sendMessage (const std::string &msg, const std::string &type="0001") |
| 广播发送 WebSocket 消息 更多... | |
| ~WebSocketServer () | |
| WebSocketServer的析构函数 更多... | |
Public 成员函数 继承自 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) | |
| 构造函数,默认是启用安全模块。限制一个ip最大连接为20;同一个ip每秒最快连接速度为6 更多... | |
| bool | startListen (const int &port, const int &threads=8) |
| 打开Tcp服务器监听程序 更多... | |
| bool | setTLS (const char *cert, const char *key, const char *passwd, const char *ca) |
| 启用 TLS 加密并配置服务器端证书与密钥 更多... | |
| void | redrawTLS () |
| 撤销TLS加密,ca证书等 更多... | |
| bool | setFunction (std::function< bool(TcpFDHandler &k, TcpFDInf &inf)> fc) |
| 设置收到客户端消息后的回调函数 注册一个回调函数 更多... | |
| bool | stopListen () |
| 停止监听 更多... | |
| bool | close () |
| 关闭监听和所有已连接的套接字 更多... | |
| bool | close (const int &fd) |
| 关闭某个套接字的连接 更多... | |
| bool | isListen () |
| 返回对象的监听状态 更多... | |
| SSL * | getSSL (const int &fd) |
| 查询和服务端的连接,传入套接字,返回加密的SSL句柄 更多... | |
| ~TcpServer () | |
| TcpServer 类的析构函数 更多... | |
额外继承的成员函数 | |
Protected 成员函数 继承自 stt::network::TcpServer | |
| bool | allowRequest (const int &cclientfd) |
| void | connectionDetect () |
Protected 属性 继承自 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服务端操作类
|
inline |
构造函数,默认是启用安全模块。限制一个ip最大连接为20;同一个ip每秒最快连接速度为6
| maxFD | 服务对象的最大接受连接数 |
| security_open | true:开启安全模块 false:关闭安全模块 (默认为开启) |
| connectionNumLimit | 同一个ip连接数目的上限 |
| connectionRateLimit | 同一个ip每秒钟连接数目的上限 |
| buffer_size | 同一个连接允许传输的最大数据量(单位为kb) 默认为8kb |
| requestRatte | 同一个连接一秒内允许的最大请求数量 (默认为12次) |
|
inline |
WebSocketServer的析构函数
| bool stt::network::WebSocketServer::close | ( | const int & | fd, |
| const std::string & | closeCodeAndMessage | ||
| ) |
发送关闭帧关闭对应套接字的 WebSocket 连接(简化方式)
传入套接字fd然后关闭连接 直接传入编码后的关闭 payload,其中前两字节为关闭码(big-endian), 后续为 UTF-8 编码的关闭原因描述,用于简化调用。
| fd | 套接字fd |
| closeCodeAndMessage | 编码后的关闭帧 payload(2 字节关闭码 + 可选消息) |
| bool stt::network::WebSocketServer::close | ( | const int & | fd, |
| const short & | code = 1000, |
||
| const std::string & | message = "bye" |
||
| ) |
发送关闭帧关闭对应套接字的 WebSocket 连接(标准方式)
传入套接字fd然后关闭连接 构建符合 RFC 6455 的关闭帧(opcode = 0x8),帧 payload 包含关闭码(2 字节)与可选关闭原因字符串。
| fd | 套接字fd |
| code | WebSocket 关闭码,常见包括:
|
| message | 可选关闭原因,供调试或日志记录用 |
| bool stt::network::WebSocketServer::close | ( | ) |
关闭监听和所有连接
|
inline |
发送 WebSocket 消息给某一个客户端
根据 WebSocket 协议,封装并发送一条带掩码的数据帧(客户端必须使用掩码), 支持根据 payload 长度自动选择帧格式:
| fd | 和客户端连接的套接字 |
| msg | 要发送的消息内容(已编码为文本或二进制) |
| type | 指定消息类型的自定义字段(通常是 WebSocket 帧的 opcode) 约定格式为 "1000" + type,其中:
|
| void stt::network::WebSocketServer::sendMessage | ( | const std::string & | msg, |
| const std::string & | type = "0001" |
||
| ) |
广播发送 WebSocket 消息
给全体客户端广播发送消息 根据 WebSocket 协议,封装并发送一条带掩码的数据帧(客户端必须使用掩码), 支持根据 payload 长度自动选择帧格式:
| msg | 要发送的消息内容(已编码为文本或二进制) |
| type | 指定消息类型的自定义字段(通常是 WebSocket 帧的 opcode) 约定格式为 "1000" + type,其中:
|
|
inline |
设置收到客户端消息后的回调函数 注册一个回调函数
| fc | 一个函数或函数对象,用于收到客户端消息后处理逻辑 -参数:string &message - 要处理的套接字 WebsocketClient &k - 当前对象的引用 WebSocketFDInformation &inf - 客户端信息 -返回: bool - 返回true处理成功,返回false处理失败 |
|
inline |
设置发送心跳后的等待时间
| secb | 发送心跳后的等待时间 单位为秒 不设置默认为30秒 |
|
inline |
设置websocket握手阶段的检查函数,只有检查通过才执行后续握手 注册一个回调函数
| fc | 一个函数或函数对象,websocket连接成功后就执行 -参数:const WebSocketFDInformation &inf - Websocket服务端的信息 -返回值 : true:检查通过 false:检查不通过 |
|
inline |
设置websocket连接成功后就执行的回调函数 注册一个回调函数
| fc | 一个函数或函数对象,websocket连接成功后就执行 -参数:const WebSocketFDInformation &inf - Websocket服务端的信息 WebSocketServer &k - 服务端对象的引用 |
|
inline |
设置心跳时间
| seca | 心跳时间 单位为分钟。不设置默认为20分钟 |
|
inline |
打开Websocket服务器监听程序
| port | 监听的端口 |
| threads | 消费者线程的数量 (默认为8) |
1.8.5