我必须以强制性打开,我在 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;
};