首页 > 解决方案 > uWebsocket 在 .message Behavior 上下文之外通过 WebSocket 发送消息

问题描述

我必须以强制性打开,我在 C++ 方面很垃圾,刚从大学毕业。

我将 uWebsocket 设置为服务器,它目前只是将响应回显给客户端。

我正在尝试在单独的线程上设置一个队列,该线程有时可以响应客户端,而不是收到消息时。我在这里自杀是因为我在主线程的上下文之外找不到合适的函数类型。

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. 
}

我已经尝试使用Type_def功能,但它仍然表现不佳。

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

回来

PN3uWS9WebSocketILb0ELb1EZ11RelaySocketvE10SocketDataEE

我的第一篇文章,如果我没有发布所有适用的信息,那就太糟糕了。

编辑:

. 下面的代码对我有用。再次感谢。

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;
}; 

标签: c++typesc++17uwebsockets

解决方案


从 uWebsocket 文档中,类型ws是:WebSocket<SSL, true, int> * 因此,您可以尝试使用以下内容。注意:您可能还需要前向声明outerFunction

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;
}

此外,uWS::Websocket来自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;
    }
}

推荐阅读