Receiving websocketstream C++ using uWebSockets

103 Views Asked by At

uWebSockets has an example client.cpp that is using ClientApp.h. However, the example is broken currently and by looking into ClientApp.h its quite clear why.

For reference, this is their github: https://github.com/uNetworking/uWebSockets/

It is unclear to me how to continue as their docs are very limited.

What I tried with their Client.cpp

int main() {
    uWS::ClientApp app({
        .open = [](/*auto *ws*/) {
            std::cout << "Hello and welcome to client" << std::endl;
        },
        .message = [](/*auto *ws, auto message*/) {

        },
        .close = [](/*auto *ws*/) {
            std::cout << "bye" << std::endl;
        }
        });

    app.connect("wss://stream.thisisatestaddress.com");

    app.run();
}

It build ofcourse but its broken so nothing happens.

The only thing i need is to receive the websocketstream, nothing else is needed.

So I tried the following in different ways:

int main() {
    struct PerSocketData {
        /* Fill with user data */
    };
    uWS::App().ws<PerSocketData>("wss://dummyurl.com", {
        .open = [](auto* ws) {
            std::cout << "Connected to WebSocket server" << std::endl;
        },
        .message = [](auto* ws, std::string_view message, uWS::OpCode opCode) {
            std::cout << "Received message: " << message << std::endl;
            // Process the received message as needed
        }
        }).run();

        return 0;
}

and i tried:

int main() {
    struct PerSocketData {
        /* Fill with user data */
    };
    uWS::App().ws<PerSocketData>("/*", {
        /* Settings */
        .compression = uWS::SHARED_COMPRESSOR,
        .maxPayloadLength = 16 * 1024 * 1024,
        .idleTimeout = 240,
        /* Handlers */
        .open = [](auto* ws) {
            std::cout << "open" << std::endl; // does not print so it doesnt run
            ws->subscribe("wss://dunnyurl.com");
            // Perform WebSocket handshake
            ws->send("WebSocket handshake message", uWS::OpCode::TEXT);
        },
        .message = [](auto* ws, std::string_view message, uWS::OpCode opCode) {
            // Print out the received message
            std::cout << "Received message: " << message << std::endl;
        }
        }).run();
    return 0;
}

In both cases, the "Handlers" part is not executing at all but also no errors are given.

What is the correct way moving forward?

Thank you for your time in advance.

0

There are 0 best solutions below