uNetworking / uWebSockets

Simple, secure & standards compliant web server for the most demanding of applications
Apache License 2.0
17.24k stars 1.75k forks source link

Storing ws object for later use compiler error #1227

Closed lolirelia closed 3 years ago

lolirelia commented 3 years ago

Hello, I have a class in which I'm trying to store a pointer to the ws object, but it throws template errors. Here's the code, the example is from EchoSocket

#include "App.h"
struct PerSocketData {
    /* Fill with user data */
};
class Base{
private:
    uWS::WebSocket<bool,bool,PerSocketData>* ws;
public:
    Base(uWS::WebSocket<bool,bool,PerSocketData>* ws){
        this->ws = ws;
    }
    virtual uWS::WebSocket<bool,bool,PerSocketData>* getws() = 0;
}

int main() {
    /* ws->getUserData returns one of these */

    /* 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::SSLApp({
        /* There are example certificates in uWebSockets.js repo */
        .key_file_name = "key.pem",
        .cert_file_name = "cert.pem",
    }).ws<PerSocketData>("/*", {
        /* Settings */
        .compression = uWS::SHARED_COMPRESSOR,
        .maxPayloadLength = 16 * 1024 * 1024,
        .idleTimeout = 16,
        .maxBackpressure = 1 * 1024 * 1024,
        .closeOnBackpressureLimit = false,
        .resetIdleTimeoutOnSend = false,
        .sendPingsAutomatically = true,
        /* Handlers */
        .upgrade = nullptr,
        .open = [](auto *ws/*ws*/) {
            Base b(ws);
        },
        .message = [](auto* ws, std::string_view message, uWS::OpCode opCode) {
            ws->send(message, opCode, true);

        },
        .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(443, [](auto *listen_socket) {
        if (listen_socket) {
            std::cout << "Listening on port " << 443 << std::endl;
        }
    }).run();
}

Am I deducing the type of ws wrong?

ghost commented 3 years ago

You have to replace bool with the actual value. You can also just make your base take T to be whatever

lolirelia commented 3 years ago

Ah okay, thank you. That works.