c++ - 令人困惑的 websocket tls 功能
问题描述
我今天尝试了一个名为 uwebsockets 的 websocket 库。我的代码只是他们 github 上提供的示例之一。
int main() {
struct PerSocketData {
};
std::vector<std::thread*> threads(std::thread::hardware_concurrency());
std::transform(threads.begin(), threads.end(), threads.begin(), [](std::thread* t) {
return new std::thread([]() {
uWS::SSLApp({
/* There are example certificates in uWebSockets.js repo */
.key_file_name = "",
.cert_file_name = "",
}).ws<PerSocketData>("/*", {
/* Settings */
.compression = uWS::SHARED_COMPRESSOR,
.maxPayloadLength = 16 * 1024,
.idleTimeout = 30,
.maxBackpressure = 1 * 1024 * 1024,
/* Handlers */
.open = [](auto* ws) {
std::cout << "connected" << std::endl;
},
.message = [](auto* ws, std::string_view message, uWS::OpCode opCode) {
ws->send(message, opCode);
},
.drain = [](auto* ws) {
/* Check getBufferedAmount here */
},
.ping = [](auto* ws) {
},
.pong = [](auto* ws) {
},
.close = [](auto* ws, int code, std::string_view message) {
}
}).listen(8443, [](auto* token) {
if (token) {
std::cout << "Thread " << std::this_thread::get_id() << " listening on port " << 8443 << std::endl;
}
else {
std::cout << "Thread " << std::this_thread::get_id() << " failed to listen on port 9001" << std::endl;
}
}).run();
});
});
std::for_each(threads.begin(), threads.end(), [](std::thread* t) {
t->join();
});}
如您所见,我没有提供密钥目录或证书目录,但在我的服务器(Windows Server 2016 IIS)上运行此示例,我能够使用https://www.websocket.org等测试站点在我的浏览器中连接到此示例/echo.html。
我认为甚至需要证书才能连接,但我没有错误并且能够连接。为什么是这样?这是我的服务器端的某种问题吗?
解决方案
确保您构建了支持 SSL 的 uWebSockets:
make -e WITH_OPENSSL=1
默认情况下,它是在没有 SSL 的情况下构建的,在这种情况下,据我所知,uWS::SSLApp 就像 uWS::App 一样工作。证书选项被静默忽略。
推荐阅读
- binary-tree - 二叉树的最小叶子数是多少?
- scala - 如何解决 Spark 中的 HadoopFsRelationProvider?
- apache-spark - UserWarning:pyarrow.open_stream 已弃用,请使用 pyarrow.ipc.open_stream 警告
- php - 增加一个月至今 2020-01-30 取消下个月
- amazon-web-services - 无法在 SAM 模板资源中获取策略定义的语法(无服务器功能)
- google-apps-script - 如果单元格等于某个值,如何保护某个范围
- java - 类未在 spring-boot 中实例化(j2EE 到 spring-boot 迁移)
- uno-platform - Xaml Grid Column Width Auto 被除 UWP 之外的所有平台忽略
- linux - 如何使用bash对第一列中具有共享子字符串的项目的列值求和
- c# - 模型字段属性未在视图中显示值