|
NexusForce 1.0.0
A rigorously engineered full-stack C++ backend library.
|
WebSocket会话类 更多...
#include <websocket.hpp>
Public 类型 | |
| using | message_handler = function<void(const string&, websocket_opcode)> |
| 消息处理器类型 | |
| using | close_handler = function<void(websocket_status, const string&)> |
| 关闭处理器类型 | |
| using | error_handler = function<void(const exception&)> |
| 错误处理器类型 | |
Public 成员函数 | |
| websocket_session (unique_ptr< tcp_socket > sock, websocket_server *server=nullptr) | |
| 构造函数 | |
| ~websocket_session () | |
| 析构函数 | |
| void | start () |
| 启动会话 | |
| void | close (websocket_status status=websocket_status::NORMAL_CLOSURE, const string &reason="") |
| 关闭连接 | |
| void | stop () |
| 停止会话 | |
| bool | send (const string &data, websocket_opcode opcode=websocket_opcode::TEXT) |
| 发送文本/二进制消息 | |
| bool | send_binary (const string &data) |
| 发送二进制消息 | |
| bool | is_open () const noexcept |
| 检查连接是否开启 | |
| void | set_message_handler (message_handler handler) |
| 设置消息处理器 | |
| void | set_close_handler (close_handler handler) |
| 设置关闭处理器 | |
| void | set_error_handler (error_handler handler) |
| 设置错误处理器 | |
| void | set_deflate_config (const websocket_deflate_config &cfg) |
| 设置permessage-deflate配置 | |
| const websocket_deflate_config & | deflate_config () const noexcept |
| 获取permessage-deflate协商配置 | |
| bool | has_deflate_config () const noexcept |
| 检查是否已启用deflate压缩 | |
| void | set_io_context (io_context &ctx) noexcept |
| 设置 io_context(启用事件驱动模式) | |
| tcp_socket & | socket () noexcept |
| 获取底层socket引用 | |
| const tcp_socket & | socket () const noexcept |
| 获取底层socket常量引用 | |
| ssl_socket * | ssl_socket_ptr () noexcept |
| 获取SSL socket指针 | |
| const ssl_socket * | ssl_socket_ptr () const noexcept |
| 获取SSL socket常量指针 | |
| Public 成员函数 继承自 neforce::enable_shared_from_this< websocket_session > | |
| shared_ptr< websocket_session > | shared_from_this () |
| 获取指向自身的共享指针 | |
| weak_ptr< websocket_session > | weak_from_this () noexcept |
| 获取指向 this 的弱指针 | |
额外继承的成员函数 | |
| Protected 成员函数 继承自 neforce::enable_shared_from_this< websocket_session > | |
| enable_shared_from_this () noexcept=default | |
| 构造函数 | |
WebSocket会话类
表示单个WebSocket连接,负责帧的解析、发送、心跳和生命周期管理。
使用示例:
在文件 websocket.hpp 第 229 行定义.
|
explicit |
| void neforce::http::websocket_session::close | ( | websocket_status | status = websocket_status::NORMAL_CLOSURE, |
| const string & | reason = "" ) |
|
inlinenoexcept |
|
inlinenoexcept |
|
inlinenoexcept |
| bool neforce::http::websocket_session::send | ( | const string & | data, |
| websocket_opcode | opcode = websocket_opcode::TEXT ) |
发送文本/二进制消息
| data | 消息数据 |
| opcode | 操作码(默认TEXT) |
引用了 neforce::data() , 以及 TEXT.
被这些函数引用 send_binary().
|
inline |
发送二进制消息
| data | 消息数据 |
在文件 websocket.hpp 第 345 行定义.
引用了 BINARY, neforce::data() , 以及 send().
|
inline |
| void neforce::http::websocket_session::set_deflate_config | ( | const websocket_deflate_config & | cfg | ) |
设置permessage-deflate配置
| cfg | 协商后的压缩配置 |
必须在 start() 之前调用。若 cfg.active 为 true, 则创建压缩/解压器用于消息的permessage-deflate处理。
|
inline |
|
inlinenoexcept |
设置 io_context(启用事件驱动模式)
| ctx | io_context 引用 |
必须在 start() 之前调用。设置后会话将以事件驱动模式运行, 使用 io_context::add_fd / schedule_timer 替代 3 个专用线程。
在文件 websocket.hpp 第 399 行定义.
|
inline |
|
inlinenoexcept |
|
inlinenoexcept |
|
inlinenoexcept |
|
inlinenoexcept |
| void neforce::http::websocket_session::start | ( | ) |
启动会话
启动读、写、心跳三个线程。