Search code examples
c++typesc++17uwebsockets

uWebsocket sending messages via WebSocket outside of the .message Behavior context


I have to open with the obligatory, I'm pretty trash at c++, fresh out of college.

I setup uWebsocket as a server, it's currently just echoing responses back to the client.

I am trying to setup a queue on a separate thread that can respond to the client at times OTHER than when I recieve a message. I'm killing myself over here though because I cannot find the appropriate type for a function outside of the context of the main thread.

void RelaySocket(){
    struct SocketData{
        //Empty because we don't need any currently.
    };
    uWS::App()
    .listen(8766, [](auto *listen_socket){
        if(listen_socket){
            std::cout<< "Listening on port" << 8766<< std::endl;
        };
    })
    .ws<SocketData>("/*",uWS::TemplatedApp<false>::WebSocketBehavior<SocketData> {//I have to explicitly declare the type of this struct.
    .open = [](auto *ws){
        std::cout<< "test"<< std::endl;
    },
    .message = [](auto *ws, std::string_view message, uWS::OpCode opCode){
    
    //The docs show how to send messages from this context, but no other method is demonstrated.
    ws->send("My message");// This works fine enough. 

    //What I'm trying to do.
    outerFunction(ws);
    }

    }).run();
}

void outerFunction([Unknown Type] *ws){// I have NO idea what type would play nice in this spot. I've tried uWS::Websocket<false>, and others to no avail.
//Processes information before replying
...
//sends n amount of reply's.
ws->send("sick Data.");//the outcome I'm looking for. 
}

I've tried using the Type_def feature, and it still isn't playing nice.

std::cout << typeid(ws).name() << std::endl;

returned

PN3uWS9WebSocketILb0ELb1EZ11RelaySocketvE10SocketDataEE

My first post, so my bad if I didn't post all applicable information.

EDIT:

. the code below worked for me. Thanks again.

struct SocketData{
//Empty because we don't need any currently.
};
void SocketResponse(uWS::WebSocket<false,true,SocketData> *ws ){
ws->send("test");
std::cout<< "test"<<std::endl;
}; 


Solution

  • From the uWebsocket docs, the type of ws is: WebSocket<SSL, true, int> * Thus, you can try to use the following. Note: You probably need to forward-declare outerFunction as well.

    void outerFunction(uWS::WebSocket<SSL, true, int> *ws);
    
    void RelaySocket(){
        struct SocketData{
            //Empty because we don't need any currently.
        };
        uWS::App()
        .listen(8766, [](auto *listen_socket){
            if(listen_socket){
                std::cout<< "Listening on port" << 8766<< std::endl;
            };
        })
        .ws<SocketData>("/*",uWS::TemplatedApp<false>::WebSocketBehavior<SocketData> {//I have to explicitly declare the type of this struct.
        .open = [](auto *ws){
            std::cout<< "test"<< std::endl;
        },
        .message = [](auto *ws, std::string_view message, uWS::OpCode opCode){
        
            //The docs show how to send messages from this context, but no other method is demonstrated.
            ws->send("My message");// This works fine enough. 
    
            //What I'm trying to do.
            outerFunction(ws);
        }
    
        }).run();
    }
    
    void outerFunction(uWS::WebSocket<SSL, true, int> *ws){// I have NO idea what type would play nice in this spot. I've tried uWS::Websocket<false>, and others to no avail.
        //Processes information before replying
        ...
        //sends n amount of reply's.
        ws->send("sick Data.");//the outcome I'm looking for. 
    }
    
    int main()
    {
        RelaySocket();
        return 0;
    }
    

    Also, the uWS::Websocket is from WebSocket.h:

    template <bool SSL, bool isServer, typename USERDATA>
    struct WebSocket : AsyncSocket<SSL> {
        template <bool> friend struct TemplatedApp;
        template <bool> friend struct HttpResponse;
    private:
        typedef AsyncSocket<SSL> Super;
    
        void *init(bool perMessageDeflate, CompressOptions compressOptions, BackPressure &&backpressure) {
            new (us_socket_ext(SSL, (us_socket_t *) this)) WebSocketData(perMessageDeflate, compressOptions, std::move(backpressure));
            return this;
        }
    }