I'm using uWebSockets in my C++ project, where I have my own custom event loop. It's a while loop, with a variable delay between each execution. It looks something like this:
while (true) {
std::this_thread::sleep_for (variableTime);
// Execute logic
}
I've previously been using another thread to execute logic, but I want to integrate the uWebSockets loop with my loop. Something like this:
#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
}
}
How would I go about doing this?
Today I had the same question. After some digging around the source code I think I found the answer.
It seems what you're looking for is a recently added (https://github.com/uNetworking/uWebSockets/pull/762) Node::Poll (Hub inherits Node) function that is non blocking for use within the programs main loop. I think it should work exactly as the getMessages you had in mind.