我在我的C++项目中使用uWebSockets,在这个项目中我有自己的自定义事件循环。这是一个while循环,每次执行之间都有一个可变的延迟。它看起来像这样:
while (true) {
std::this_thread::sleep_for (variableTime);
// Execute logic
}我以前一直使用另一个线程来执行逻辑,但我想将uWebSockets循环与我的循环集成在一起。如下所示:
#include <iostream>
#include <uWS/uWS.h>
using namespace std;
int main () {
uWS::Hub h;
h.onMessage([](uWS::WebSocket<uWS::SERVER> *ws, char *message, size_t length, uWS::OpCode opCode) {
ws->send(message, length, opCode);
});
if (h.listen(3000)) {
h.run();
}
while (true) {
std::this_thread::sleep_for (variableTime);
h.getMessages(); // <-- doesn't call `onMessage` until this is executed
// Execute logic
}
}我该怎么做呢?
发布于 2018-03-04 18:09:32
今天我也有同样的问题。经过对源代码的挖掘,我想我找到了答案。
您正在寻找的似乎是最近添加的(https://github.com/uNetworking/uWebSockets/pull/762) Node::Poll (集线器继承节点)函数,它是非阻塞的,可在程序主循环中使用。我认为它应该和你脑海中的getMessages一模一样。
https://stackoverflow.com/questions/45889889
复制相似问题