Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QHttpServer and WebSockets
Forum Update on Monday, May 27th 2025

QHttpServer and WebSockets

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 3 Posters 1.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mr.Betatester
    wrote on 21 Dec 2022, 11:42 last edited by
    #1

    I try to use the QHttpServer and setup a WebSocket connection for realtime updates (because SSE ist not implemented at the QHttpServer at the moment).

    When I start the connection the WebSocket will be closed directly after the newWebSocketConnectionSlot was processed.

    Server startup:

        m_httpServer.route(m_frontendDir + "/app.js", [this]() { return QHttpServerResponse(m_frontendJS); });
        m_httpServer.route("/", [this](const QHttpServerRequest& request) { return getPage(request); });
        m_httpServer.route("/data/", [this](const QString& table, const QHttpServerRequest& request) { return getData(request, table); });
        connect(&m_httpServer, SIGNAL(newWebSocketConnection()), this, SLOT(newWebSocketConnection()));
    

    WebSocket handling:

    
    void AGVWebServer::newWebSocketConnection()
    {
        if (m_httpServer.hasPendingWebSocketConnections() == true) {
            std::unique_ptr<QWebSocket> up_socket = m_httpServer.nextPendingWebSocketConnection();
            m_clients.push_back(std::move(up_socket));
    
            QWebSocket* socket = m_clients[m_clients.size() - 1].get();
    
            connect(socket, &QWebSocket::textMessageReceived, this, &AGVWebServer::processTextMessage);
            connect(socket, &QWebSocket::disconnected, this, &AGVWebServer::socketDisconnected);
    
        }
    }
    
    
    void AGVWebServer::processTextMessage(QString message)
    {
        QWebSocket *pClient = qobject_cast<QWebSocket *>(sender());
    
        if (pClient) {
            pClient->sendTextMessage(message);
        }
    }
    
    
    void AGVWebServer::socketDisconnected()
    {
        QWebSocket *pClient = qobject_cast<QWebSocket *>(sender());
    
        if (pClient) {
            //TODO m_clients.removeAll(pClient);
            pClient->deleteLater();
        }
    }
    

    My current workaround is to start a feature task that will never end to keep the connection open:

        m_httpServer.route("/notification", [this]() {
            return QtConcurrent::run([this] () {
                QEventLoop loop;
                connect(this, &AGVWebServer::stopWebSockets, &loop, &QEventLoop::quit);
                loop.exec();
    
                return QHttpServerResponse("");
            });
        });
    

    How can I use the WebSocket Connection without the QtConcurrent or how to keep the Websocket Connection open?

    Thanks
    Ronald

    1 Reply Last reply
    0
    • M Offline
      M Offline
      Mr.Betatester
      wrote on 21 Dec 2022, 13:33 last edited by
      #7

      I got it.

      I need add a empty route that doesn't return a QHttpServerResponse. If i try to open a WS on a route that returns http data then the connection will be closed. Also if I use a non existing route.

      Adding this route will do the magic:

      m_httpServer.route("/notification", [](QHttpServerResponder&&) {});
      
      I 1 Reply Last reply 24 May 2023, 14:52
      0
      • C Online
        C Online
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 21 Dec 2022, 12:13 last edited by
        #2

        I don't see directly why it should be disconnected - apart from the strange (and imho wrong) std::unique_ptr<> stuff it looks fine.
        Use a debugger to see why the disconect happens.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        0
        • M Offline
          M Offline
          Mr.Betatester
          wrote on 21 Dec 2022, 12:29 last edited by
          #3

          @Mr-Betatester said in QHttpServer and WebSockets:

          nextPendingWebSocketConnection

          How can I use the WebSocket without the unique_ptr?
          nextPendingWebSocketConnection returns a unique_ptr and I have to store it as unique_ptr otherwise it will be delete at function end.

          https://doc-snapshots.qt.io/qt6-6.4/qabstracthttpserver.html#nextPendingWebSocketConnection

          Thanks

          1 Reply Last reply
          0
          • C Online
            C Online
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 21 Dec 2022, 12:30 last edited by
            #4

            Sorry, I wasn't aware that they change it this way. Don't know why this was done though. So you have to work with it this way.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            0
            • M Offline
              M Offline
              Mr.Betatester
              wrote on 21 Dec 2022, 12:46 last edited by
              #5

              I can use

              QWebSocket* socket = m_httpServer.nextPendingWebSocketConnection().release();
              

              and store a QWebSocket* in my m_clients.
              But this doesn't fix the closed connection.

              Without using the QtConcurrent Chrome close the connection with Code 1006.

              C 1 Reply Last reply 21 Dec 2022, 12:50
              0
              • M Mr.Betatester
                21 Dec 2022, 12:46

                I can use

                QWebSocket* socket = m_httpServer.nextPendingWebSocketConnection().release();
                

                and store a QWebSocket* in my m_clients.
                But this doesn't fix the closed connection.

                Without using the QtConcurrent Chrome close the connection with Code 1006.

                C Online
                C Online
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on 21 Dec 2022, 12:50 last edited by
                #6

                @Mr-Betatester said in QHttpServer and WebSockets:

                and store a QWebSocket* in my m_clients.
                But this doesn't fix the closed connection.

                As I said it was my fault.

                Use a debugger and see why the close happens.

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  Mr.Betatester
                  wrote on 21 Dec 2022, 13:33 last edited by
                  #7

                  I got it.

                  I need add a empty route that doesn't return a QHttpServerResponse. If i try to open a WS on a route that returns http data then the connection will be closed. Also if I use a non existing route.

                  Adding this route will do the magic:

                  m_httpServer.route("/notification", [](QHttpServerResponder&&) {});
                  
                  I 1 Reply Last reply 24 May 2023, 14:52
                  0
                  • M Mr.Betatester
                    21 Dec 2022, 13:33

                    I got it.

                    I need add a empty route that doesn't return a QHttpServerResponse. If i try to open a WS on a route that returns http data then the connection will be closed. Also if I use a non existing route.

                    Adding this route will do the magic:

                    m_httpServer.route("/notification", [](QHttpServerResponder&&) {});
                    
                    I Offline
                    I Offline
                    ivanov.ivan
                    wrote on 24 May 2023, 14:52 last edited by
                    #8

                    @Mr-Betatester In Qt 6.5 webSocket with httpserver are not working. It inited but can't send messages.

                    I 1 Reply Last reply 24 Jul 2023, 12:30
                    0
                    • I ivanov.ivan
                      24 May 2023, 14:52

                      @Mr-Betatester In Qt 6.5 webSocket with httpserver are not working. It inited but can't send messages.

                      I Offline
                      I Offline
                      ivanov.ivan
                      wrote on 24 Jul 2023, 12:30 last edited by
                      #9

                      @ivanov-ivan At Qt 6.5.0 works code like this:

                      m_pHttpServer->route("/websocket/<arg>", [this](const QString &arg, const QHttpServerRequest) {
                        qDebug() << __func__ << request  << arg;
                        return QFuture<QHttpServerResponse>();
                      });
                      
                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved