uWebsocket 在 .message 行为上下文之外通过 WebSocket 发送消息
uWebsocket sending messages via WebSocket outside of the .message Behavior context
我必须打开必修课,我在 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
我的第一个 post,如果我没有 post 所有适用的信息,我会很糟糕。
编辑:
。下面的代码对我有用。再次感谢。
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;
};
从 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;
}
}
我必须打开必修课,我在 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
我的第一个 post,如果我没有 post 所有适用的信息,我会很糟糕。
编辑:
。下面的代码对我有用。再次感谢。
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;
};
从 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;
}
}