NexusForce 1.0.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
websocket_session类 参考

WebSocket会话类 更多...

#include <websocket.hpp>

类 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)
 设置错误处理器
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常量指针

额外继承的成员函数

Protected 成员函数 继承自 enable_shared_from_this< websocket_session >
 enable_shared_from_this () noexcept
 构造函数
shared_ptr< websocket_sessionshared_from_this ()
 获取指向自身的共享指针

详细描述

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.hpp211 行定义.

构造及析构函数说明

◆ websocket_session()

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

构造函数

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

引用了 websocket_session().

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

成员函数说明

◆ close()

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

关闭连接

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

引用了 close().

被这些函数引用 close().

◆ is_open()

bool websocket_session::is_open ( ) const
inlinenoexcept

检查连接是否开启

返回
开启返回true

在文件 websocket.hpp314 行定义.

◆ send()

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

发送文本/二进制消息

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

引用了 data() , 以及 send().

被这些函数引用 send() , 以及 send_binary().

◆ send_binary()

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

发送二进制消息

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

在文件 websocket.hpp308 行定义.

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

被这些函数引用 send_binary().

◆ set_close_handler()

void websocket_session::set_close_handler ( close_handler handler)
inline

设置关闭处理器

参数
handler处理函数

在文件 websocket.hpp326 行定义.

引用了 move().

◆ set_error_handler()

void websocket_session::set_error_handler ( error_handler handler)
inline

设置错误处理器

参数
handler处理函数

在文件 websocket.hpp332 行定义.

引用了 move().

◆ set_message_handler()

void websocket_session::set_message_handler ( message_handler handler)
inline

设置消息处理器

参数
handler处理函数

在文件 websocket.hpp320 行定义.

引用了 move().

◆ socket() [1/2]

const tcp_socket & websocket_session::socket ( ) const
inlinenoexcept

获取底层socket常量引用

返回
socket常量引用

在文件 websocket.hpp344 行定义.

◆ socket() [2/2]

tcp_socket & websocket_session::socket ( )
inlinenoexcept

获取底层socket引用

返回
socket引用

在文件 websocket.hpp338 行定义.

◆ ssl_socket_ptr() [1/2]

const ssl_socket * websocket_session::ssl_socket_ptr ( ) const
inlinenoexcept

获取SSL socket常量指针

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

在文件 websocket.hpp356 行定义.

◆ ssl_socket_ptr() [2/2]

ssl_socket * websocket_session::ssl_socket_ptr ( )
inlinenoexcept

获取SSL socket指针

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

在文件 websocket.hpp350 行定义.

◆ start()

void websocket_session::start ( )

启动会话

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

引用了 start().

被这些函数引用 start().


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