【问题标题】:confusing websocket tls functionality令人困惑的 websocket tls 功能
【发布时间】:2020-11-15 23:13:43
【问题描述】:

我今天试用了一个名为 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 等测试站点在我的浏览器中连接到此示例。

我认为甚至需要证书才能使其连接,但我没有收到任何错误并且能够连接。为什么是这样? 这是我服务器端的某种问题吗?

【问题讨论】:

  • 如何连接到 websocket 主机很重要 - Yauheni 的回答可能是正确的,但知道您是否能够连接到 ws:// 或 wss:// 在诊断上很有用。它也有助于提供网络内容的客户端!

标签: c++ ssl websocket tls1.2 uwebsockets


【解决方案1】:

确保您构建的 uWebSockets 支持 SSL:

make -e WITH_OPENSSL=1

默认情况下,它是在没有 SSL 的情况下构建的,在这种情况下,据我所知,uWS::SSLApp 就像 uWS::App 一样工作。证书选项被静默忽略。

【讨论】:

    猜你喜欢
    • 2011-03-09
    • 2022-01-21
    • 2022-01-17
    • 2018-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-15
    • 1970-01-01
    相关资源
    最近更新 更多