NexusForce 1.0.0
A rigorously engineered full-stack C++ backend library.
载入中...
搜索中...
未找到
neforce::http::websocket_session类 参考

WebSocket会话类 更多...

#include <websocket.hpp>

类 neforce::http::websocket_session 继承关系图:
[图例]

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_configdeflate_config () const noexcept
 获取permessage-deflate协商配置
bool has_deflate_config () const noexcept
 检查是否已启用deflate压缩
void set_io_context (io_context &ctx) noexcept
 设置 io_context(启用事件驱动模式)
tcp_socketsocket () noexcept
 获取底层socket引用
const tcp_socketsocket () const noexcept
 获取底层socket常量引用
ssl_socketssl_socket_ptr () noexcept
 获取SSL socket指针
const ssl_socketssl_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连接,负责帧的解析、发送、心跳和生命周期管理。

使用示例:

auto session = make_shared<websocket_session>(move(sock));
session->set_message_handler([](const string& msg, websocket_opcode opcode) {
if (opcode == websocket_opcode::TEXT) {
println("Text: ", msg);
} else {
// 处理二进制消息
}
});
session->set_close_handler([](websocket_status status, const string& reason) {
println("Closed: ", static_cast<int>(status), " - ", reason);
});
session->start();
void println(Args &&... args)
打印多个值并换行
enable_if_t<!is_unbounded_array_v< T > &&is_constructible_v< T, Args... >, shared_ptr< T > > make_shared(Args &&... args)
融合分配创建共享指针
constexpr Iterator2 move(Iterator1 first, Iterator1 last, Iterator2 result) noexcept(noexcept(inner::__move_aux(first, last, result)))
移动范围元素
websocket_opcode
WebSocket帧操作码
websocket_status
WebSocket关闭状态码

在文件 websocket.hpp229 行定义.

构造及析构函数说明

◆ websocket_session()

neforce::http::websocket_session::websocket_session ( unique_ptr< tcp_socket > sock,
websocket_server * server = nullptr )
explicit

构造函数

参数
sockTCP/SSL socket
server所属服务器

被这些函数引用 ~websocket_session().

成员函数说明

◆ close()

void neforce::http::websocket_session::close ( websocket_status status = websocket_status::NORMAL_CLOSURE,
const string & reason = "" )

关闭连接

参数
status关闭状态码(默认NORMAL_CLOSURE)
reason关闭原因

引用了 NORMAL_CLOSURE.

◆ deflate_config()

const websocket_deflate_config & neforce::http::websocket_session::deflate_config ( ) const
inlinenoexcept

获取permessage-deflate协商配置

返回
配置的常量引用

在文件 websocket.hpp384 行定义.

◆ has_deflate_config()

bool neforce::http::websocket_session::has_deflate_config ( ) const
inlinenoexcept

检查是否已启用deflate压缩

返回
启用返回true

在文件 websocket.hpp390 行定义.

◆ is_open()

bool neforce::http::websocket_session::is_open ( ) const
inlinenoexcept

检查连接是否开启

返回
开启返回true

在文件 websocket.hpp351 行定义.

◆ send()

bool neforce::http::websocket_session::send ( const string & data,
websocket_opcode opcode = websocket_opcode::TEXT )

发送文本/二进制消息

参数
data消息数据
opcode操作码(默认TEXT)
返回
发送成功返回true

引用了 neforce::data() , 以及 TEXT.

被这些函数引用 send_binary().

◆ send_binary()

bool neforce::http::websocket_session::send_binary ( const string & data)
inline

发送二进制消息

参数
data消息数据
返回
发送成功返回true

在文件 websocket.hpp345 行定义.

引用了 BINARY, neforce::data() , 以及 send().

◆ set_close_handler()

void neforce::http::websocket_session::set_close_handler ( close_handler handler)
inline

设置关闭处理器

参数
handler处理函数

在文件 websocket.hpp363 行定义.

引用了 neforce::move().

◆ set_deflate_config()

void neforce::http::websocket_session::set_deflate_config ( const websocket_deflate_config & cfg)

设置permessage-deflate配置

参数
cfg协商后的压缩配置

必须在 start() 之前调用。若 cfg.active 为 true, 则创建压缩/解压器用于消息的permessage-deflate处理。

◆ set_error_handler()

void neforce::http::websocket_session::set_error_handler ( error_handler handler)
inline

设置错误处理器

参数
handler处理函数

在文件 websocket.hpp369 行定义.

引用了 neforce::move().

◆ set_io_context()

void neforce::http::websocket_session::set_io_context ( io_context & ctx)
inlinenoexcept

设置 io_context(启用事件驱动模式)

参数
ctxio_context 引用

必须在 start() 之前调用。设置后会话将以事件驱动模式运行, 使用 io_context::add_fd / schedule_timer 替代 3 个专用线程。

在文件 websocket.hpp399 行定义.

◆ set_message_handler()

void neforce::http::websocket_session::set_message_handler ( message_handler handler)
inline

设置消息处理器

参数
handler处理函数

在文件 websocket.hpp357 行定义.

引用了 neforce::move().

◆ socket() [1/2]

const tcp_socket & neforce::http::websocket_session::socket ( ) const
inlinenoexcept

获取底层socket常量引用

返回
socket常量引用

在文件 websocket.hpp414 行定义.

◆ socket() [2/2]

tcp_socket & neforce::http::websocket_session::socket ( )
inlinenoexcept

获取底层socket引用

返回
socket引用

在文件 websocket.hpp408 行定义.

◆ ssl_socket_ptr() [1/2]

const ssl_socket * neforce::http::websocket_session::ssl_socket_ptr ( ) const
inlinenoexcept

获取SSL socket常量指针

返回
SSL socket常量指针,若非SSL连接返回nullptr

在文件 websocket.hpp426 行定义.

◆ ssl_socket_ptr() [2/2]

ssl_socket * neforce::http::websocket_session::ssl_socket_ptr ( )
inlinenoexcept

获取SSL socket指针

返回
SSL socket指针,若非SSL连接返回nullptr

在文件 websocket.hpp420 行定义.

◆ start()

void neforce::http::websocket_session::start ( )

启动会话

启动读、写、心跳三个线程。


该类的文档由以下文件生成: