如何识别与 websocketpp 的连接
How to identify a connection with websocketpp
我有一段使用 websocketpp 运行服务器的代码。我想确定进入服务器的不同连接。为此,似乎应该使用websocketpp::connection_hdl hdl
。
namespace websocketpp {
/// A handle to uniquely identify a connection.
/**
* This type uniquely identifies a connection. It is implemented as a weak
* pointer to the connection in question. This provides uniqueness across
* multiple endpoints and ensures that IDs never conflict or run out.
*
* It is safe to make copies of this handle, store those copies in containers,
* and use them from other threads.
*
* This handle can be upgraded to a full shared_ptr using
* `endpoint::get_con_from_hdl()` from within a handler fired by the connection
* that owns the handler.
*/
typedef lib::weak_ptr<void> connection_hdl;
但正如你所看到的,这是一个我不知道如何与其他人相比的weak_ptr<void>
。
我有一张以websocketpp::connection_hdl
为索引的地图,当我尝试查看是否有具有以下索引时:
std::map<websocketpp::connection_hdl, asio::ip::tcp::socket> active_connections;
if (active_connections.count(con->get_socket()) > 0) {}
编译器抱怨:
错误 C2678:二进制"<":找不到左手的运算符 类型为"const _Ty"的操作数(或没有可接受的转换(
有什么方法可以从连接(原始整数套接字(中获取套接字。如果可以的话,我可以将其用作索引并解决问题。
你能看到任何其他方法来解决它吗?
有两个问题:
- 您使用weak_ptr作为密钥而不使用 std::owner_less。详细信息:如何使用 std::map 和 std::weak_ptr 作为键?
- 在您的示例中,您使用套接字作为键而不是connection_hdl。
溶液:
std::map<websocketpp::connection_hdl, boost::asio::ip::tcp::socket, std::owner_less<websocketpp::connection_hdl>> active_connections;
if (active_connections.count(con) > 0) {}
但是,映射没有多大意义:如果您有connection_hdl,则可以使用 get_socket(( 方法获取连接的套接字。我从未使用过这种方法,但我认为它应该有效吗?如果您只想存储所有打开的连接及其套接字,则包含连接句柄的 std::vector 可能会更好。
相关文章:
- 当套接字连接断开时检测C/C++Unix
- 无法在windows上使用mingw将sqlite3与c连接
- 到连接组件算法的问题(递归)
- QTcpSocket在不阻塞GUI的情况下重新连接到服务器
- 无法在C++中建立与MySQL数据库的连接
- PC中的程序和PHONE中的本机描述应用程序之间的数据连接
- 在Qt Creator中,如何在连接到正在运行的进程后查看控制台输出
- 连接 dockerized 模型和 dockerized 数据库时出现"无法 SQLConnect"错误
- 使用 bfs 解决连接组件问题时得到错误的答案
- 在C++的两个字符串中连接以逗号分隔的唯一值
- 如何修复valgrind启动时的致命错误(与libc6-dbg和libc6-dbg:i386连接)
- C++应用程序 MySQL odbc 数据库连接错误:在引发"otl_tmpl_exception<>"实例后终止调用
- OSX蓝牙打开RFCOMMChannelAsync声称已连接,但未建立连接,并且从未调用过委托
- 在 libcurl 连接池中预创建连接
- 套接字连接"Operation not permitted"错误,甚至使用升压/平发器根.cpp
- libcurl 和 DNS ttl 中的内部连接管理
- 如何应用 libcurl 的持久连接选项
- 如何识别与 websocketpp 的连接
- 使用WebSocketpp启用了Websocketpp接受连接的问题
- 使用websocketpp库连接到一个c++ websocket服务器