c++ - 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;
};
解决方案
从 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;
}
}
推荐阅读
- python - 使用 Python 将列表 l 中以“S”开头的项目保存到 l2
- html - 自定义光标不会在 Chrome 中显示,但在 Safari 中有效
- rust - 在 Diesel 中引用同一个表的多个外键
- java - 导入java.io.*;行不通,怎么办,没办法?
- puppetlabs-aws - 我需要将 aws_session_token 与 puppetlabs-aws 一起使用
- javascript - 具有增量和减量输入的jquery数据表
- scopus - 如何检索查询中每个 Scopus 记录的所有作者
- android - 如何将protobuf添加到flutter/kotlin android项目中
- ruby-on-rails - (Rails 5) LoadError: cannot load such file -- sass ...当部署到 Heroku
- testing - 如何使用 cypress 登录 salesforce?