To keep things simple, let's assume that my server is running just one uWebSockets instance:
struct UserData
{
uWS::WebSocket<true, uWS::SERVER> *ws;
bool logged_in = false;
ID user_id;
};
uWS::SSLApp()
.ws<UserData>(
"/*",
{
.open =
[](auto *ws, auto *req) {
std::cout << "user with ip: " << ws->getRemoteAddress()
<< " connected" << std::endl;
},
.message =
[](auto *ws, std::string_view message,
uWS::OpCode opCode) {
auto userData = static_cast<UserData *>(ws->getUserData());
// give websocket pointer to a session
userData->ws = ws;
Session session;
session.process_message(userData, message);
})
.listen(9001,
[](auto *token) {
if (token)
std::cout << "listening on port 9001" << std::endl;
else
std::cout << "failed to listen on port 9001" << std::endl;
})
.run();
});
Suppose we have an implementation of Session like this:
class Session {
process_message(UserData &userData, const std::string_view &message) {
std::this_thread::sleep_for(std::chrono::seconds(1));
}
}
Within the Session::process_message
function, there is some code that takes a while to complete.
How can I hand control back to the event loop to handle other sessions concurrently?
In essence, how do I make sure the program is fully asynchronous and can run sessions concurrently?
The library is designed to be asynchronous. Does this mean that the library will manage other connections concurrently, leaving us without any worries?