Skip to content

QWebSocketServer

ModuleWebSockets
Include
#include <QCoroWebSocketServer>
CMake
target_link_libraries(myapp QCoro::WebSockets)
QMake
QT += QCoroWebSockets

QCoro provides a wrapper for the QWebSocketServer class which allows user to asynchronously co_await incoming connections. To wrap a QWebSocketServer instance, use the qCoro() overload from the QCoroWebSocketServer include header.

QCoroWebSocketServer qCoro(QWebSocketServer &);
QCoroWebSocketServer qCoro(QWebSocketServer *);

nextPendingConnection()

Suspends the awaiter until a new incoming connection becomes available or until the specified timeout. If the specified timeout is -1, the operation will never time out.

Returns a pointer to QWebSocket representing the new connection, or a null pointer if the operation times out, the server is not listen()ining for incoming connections.

QCoro::Task<QWebSocket *> QCoroWebSocketServer::nextPendingConnection(std::chrono::milliseconds timeout);

Note that pauseAccepting() doesn't resume any awaiting coroutines.

QCoro::Task<> listen(QWebSocketServer *server) {
    server->listen();
    while (auto socket = std::unique_ptr<QWebSocket>(co_await qCoro(server).nextPendingConnection());
           socket != nullptr)
    {
        handleConnection(std::move(socket));
    }
}