Issue building C++ example provided by uWebsockets using CMake

180 views Asked by At

I have the following directory structure for my project that utilizes uWebsockets and I am trying to run the example provided by in uWebsockets examples folder. Interestingly, uWebsockets does not have any CMakeLists.txt in its root folder as to how to build the examples.

I am able to configure and generate successfully with CMake in Windows. However, I have issues building target uwebsocketsExample1.

I'd like to be able to use FetchContent and bring in uWebsockets and its submodules uSockets, etc., by external\uWebsockets\CMakeLists.txt. Could someone help me build EchoServer.cpp?

my-app
│   CMakeLists.txt
│
├───examples
│       CMakeLists.txt
│       uwebsockets.example.cpp
│
└───external
    └───uwebsockets
            CMakeLists.txt

Contents of my-app\CMakeLists.txt:

cmake_minimum_required(VERSION 3.24)
project(my-app VERSION 1.0.0)

set(CMAKE_CXX_STANDARD 20)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)

add_subdirectory(external/uWebsockets)
add_subdirectory(examples)

Contents of external\uwebsockets\CMakeLists.txt:

cmake_minimum_required(VERSION 3.24)
project(libuWebsockets VERSION 1.0.0)

set(CMAKE_CXX_STANDARD 20)

include(FetchContent)
include(CMakePrintHelpers)

FetchContent_Declare(uwebsockets
        GIT_REPOSITORY https://github.com/uNetworking/uWebSockets.git)
FetchContent_MakeAvailable(uwebsockets)

add_library(uwebsockets INTERFACE)
target_include_directories(uwebsockets INTERFACE ${uwebsockets_SOURCE_DIR}/src)
add_library(uwebsockets::uwebsockets ALIAS uwebsockets)

Contents of examples\CMakeLists.txt:

cmake_minimum_required(VERSION 3.24)
project(projExamples VERSION 1.0.0)

set(CMAKE_CXX_STANDARD 20)

add_executable(uwebsocketsExample1 uwebsockets.example.cpp)
target_link_libraries(uwebsocketsExample1 PRIVATE uwebsockets::uwebsockets)

Contents of examples\uwebsockets.example.cpp:

/* We simply call the root header file "App.h", giving you uWS::App and uWS::SSLApp */
#include "App.h"

/* This is a simple WebSocket echo server example.
 * You may compile it with "WITH_OPENSSL=1 make" or with "make" */

int main() {
    /* ws->getUserData returns one of these */
    struct PerSocketData {
        /* Fill with user data */
    };

    /* Keep in mind that uWS::SSLApp({options}) is the same as uWS::App() when compiled without SSL support.
     * You may swap to using uWS:App() if you don't need SSL */
    uWS::App({
                     /* There are example certificates in uWebSockets.js repo */
                     .key_file_name = "misc/key.pem",
                     .cert_file_name = "misc/cert.pem",
                     .passphrase = "1234"
             }).ws<PerSocketData>("/*", {
            /* Settings */
            .compression = uWS::CompressOptions(uWS::DEDICATED_COMPRESSOR_4KB | uWS::DEDICATED_DECOMPRESSOR),
            .maxPayloadLength = 100 * 1024 * 1024,
            .idleTimeout = 16,
            .maxBackpressure = 100 * 1024 * 1024,
            .closeOnBackpressureLimit = false,
            .resetIdleTimeoutOnSend = false,
            .sendPingsAutomatically = true,
            /* Handlers */
            .upgrade = nullptr,
            .open = [](auto */*ws*/) {
                /* Open event here, you may access ws->getUserData() which points to a PerSocketData struct */

            },
            .message = [](auto *ws, std::string_view message, uWS::OpCode opCode) {
                /* This is the opposite of what you probably want; compress if message is LARGER than 16 kb
                 * the reason we do the opposite here; compress if SMALLER than 16 kb is to allow for
                 * benchmarking of large message sending without compression */
                ws->send(message, opCode, message.length() < 16 * 1024);
            },
            .drain = [](auto */*ws*/) {
                /* Check ws->getBufferedAmount() here */
            },
            .ping = [](auto */*ws*/, std::string_view) {
                /* Not implemented yet */
            },
            .pong = [](auto */*ws*/, std::string_view) {
                /* Not implemented yet */
            },
            .close = [](auto */*ws*/, int /*code*/, std::string_view /*message*/) {
                /* You may access ws->getUserData() here */
            }
    }).listen(9001, [](auto *listen_socket) {
        if (listen_socket) {
            std::cout << "Listening on port " << 9001 << std::endl;
        }
    }).run();
}
0

There are 0 answers