Websocket客户端操作的类 -如果需要重新设置TLS/Https加密的证书,目前需要销毁对象后重新构造 底层TCP默认是阻塞的 更多...
#include <sttnet.h>
Public 成员函数 | |
| WebSocketClient (const bool &TLS=false, const char *ca="", const char *cert="", const char *key="", const char *passwd="") | |
| WebSocketClient类的构造函数 更多... | |
| void | setFunction (std::function< bool(const std::string &message, WebSocketClient &k)> fc) |
| 设置收到服务端消息后的回调函数 注册一个回调函数 更多... | |
| bool | connect (const std::string &url, const int &min=20) |
| 连接到websocket服务器 更多... | |
| bool | sendMessage (const std::string &message, const std::string &type="0001") |
| 发送 WebSocket 消息 更多... | |
| void | close (const std::string &closeCodeAndMessage, const bool &wait=true) |
| 发送关闭帧并关闭 WebSocket 连接(简化方式) 更多... | |
| void | close (const short &code=1000, const std::string &message="bye", const bool &wait=true) |
| 发送关闭帧并关闭 WebSocket 连接(标准方式) 更多... | |
| bool | isConnect () |
| 返回连接状态 更多... | |
| std::string | getUrl () |
| 如果连接到了服务器 返回url 更多... | |
| std::string | getServerIp () |
| 如果连接到了服务器 返回服务器ip 更多... | |
| std::string | getServerPort () |
| 如果连接到了服务器 返回服务器端口 更多... | |
| ~WebSocketClient () | |
| WebSocketClient类的析构函数,销毁对象时候会优雅退出断开连接 更多... | |
Websocket客户端操作的类 -如果需要重新设置TLS/Https加密的证书,目前需要销毁对象后重新构造 底层TCP默认是阻塞的
|
inline |
WebSocketClient类的构造函数
| TLS | true:启用wss加密 false:不启用wss加密 (默认为false不启用) |
| ca | CA 根证书路径(若启用TLS加密则必须填这个 默认空) |
| cert | 客户端证书路径(可选 默认空) |
| key | 客户端私钥路径(可选 默认空) |
| passwd | 私钥解密密码(可选 默认空) |
| stt::network::WebSocketClient::~WebSocketClient | ( | ) |
WebSocketClient类的析构函数,销毁对象时候会优雅退出断开连接
| void stt::network::WebSocketClient::close | ( | const std::string & | closeCodeAndMessage, |
| const bool & | wait = true |
||
| ) |
发送关闭帧并关闭 WebSocket 连接(简化方式)
直接传入编码后的关闭 payload,其中前两字节为关闭码(big-endian), 后续为 UTF-8 编码的关闭原因描述,用于简化调用。
| closeCodeAndMessage | 编码后的关闭帧 payload(2 字节关闭码 + 可选消息) |
| wait | 是否等待底层监听线程/epoll事件处理后退出 |
| void stt::network::WebSocketClient::close | ( | const short & | code = 1000, |
| const std::string & | message = "bye", |
||
| const bool & | wait = true |
||
| ) |
发送关闭帧并关闭 WebSocket 连接(标准方式)
构建符合 RFC 6455 的关闭帧(opcode = 0x8),帧 payload 包含关闭码(2 字节)与可选关闭原因字符串。
| code | WebSocket 关闭码,常见包括:
|
| message | 可选关闭原因,供调试或日志记录用 |
| wait | 是否等待底层监听线程/epoll事件处理后退出 |
| bool stt::network::WebSocketClient::connect | ( | const std::string & | url, |
| const int & | min = 20 |
||
| ) |
连接到websocket服务器
| url | ws/wss的完整url(注意需要显式指定端口和路径) 如:wss://google.com 要写成wss://google.com:443/ (补全:443和/) |
| min | 心跳时间,单位分钟 (默认为20分钟) |
|
inline |
如果连接到了服务器 返回服务器ip
|
inline |
如果连接到了服务器 返回服务器端口
|
inline |
如果连接到了服务器 返回url
|
inline |
返回连接状态
| bool stt::network::WebSocketClient::sendMessage | ( | const std::string & | message, |
| const std::string & | type = "0001" |
||
| ) |
发送 WebSocket 消息
根据 WebSocket 协议,封装并发送一条带掩码的数据帧(客户端必须使用掩码), 支持根据 payload 长度自动选择帧格式:
| message | 要发送的消息内容(已编码为文本或二进制) |
| type | 指定消息类型的自定义字段(通常是 WebSocket 帧的 opcode) 约定格式为 "1000" + type,其中:
|
|
inline |
设置收到服务端消息后的回调函数 注册一个回调函数
| fc | 一个函数或函数对象,用于收到服务端消息后处理逻辑 -参数:string &message - 要处理的套接字 WebsocketClient &k - 当前对象的引用 -返回: bool - 返回true处理成功,返回false处理失败 |
1.8.5