Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-55506

Unexpected behaviour QWebSocket textMessageReceived

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P2: Important
    • Resolution: Done
    • Affects Version/s: 5.6.1
    • Fix Version/s: 5.8.0
    • Component/s: WebSockets
    • Labels:
      None
    • Environment:
      Windows 7, Qt5.6.1, MinGw32; QtCreator 4.0.1
    • Commits:
      2eeee81b67ce4fc81793c51bafd2cc1b33076c05

      Description

      It seems that the internal buffer of the QWebsocket is not cleared when we show a QMessageBox.

      When we use a QueuedConnection all is fine.

      The QWebSocketServer:

      #include <QCoreApplication>
      #include <QWebSocketServer>
      #include <QWebSocket>
      
      int main(int argc, char *argv[])
      {
          QCoreApplication a(argc, argv);
      
          QWebSocketServer webServer("Echo Server", QWebSocketServer::NonSecureMode);
          webServer.listen(QHostAddress::Any, 1234);
      
          QObject::connect(
              &webServer, &QWebSocketServer::newConnection, &webServer, [&webServer]()
              {
                QWebSocket *pSocket = webServer.nextPendingConnection();
      
                QObject::connect(pSocket, &QWebSocket::textMessageReceived, &webServer,
                                 [pSocket](const QString &message)
                                 {
                                   pSocket->sendTextMessage(message);
                                 });
      
                QObject::connect(&webServer, &QObject::destroyed, pSocket,
                                 &QObject::deleteLater);
              });
      
          return a.exec();
      }
      

      QWebSocketClient:

      #include "mainwindow.h"
      #include <QApplication>
      #include <QMessageBox>
      #include <QWebSocket>
      #include <QTimer>
      #include <QDebug>
      
      int main(int argc, char *argv[])
      {
        QApplication a(argc, argv);
      
        QWebSocket webSocket;
      
        QObject::connect(&webSocket, &QWebSocket::connected, &webSocket,
                         [&webSocket]()
                         {
                           qDebug() << "CONNECTED";
                           webSocket.sendTextMessage("T;");
                         });
      
        QTimer heartbeat;
        heartbeat.setInterval(1000);
        heartbeat.setSingleShot(false);
      
        QObject::connect(&heartbeat, &QTimer::timeout, &webSocket, [&webSocket]()
                         {
                           if (webSocket.state() == QAbstractSocket::ConnectedState)
                           {
                             qDebug() << "SEND T;";
                             webSocket.sendTextMessage("T;");
                           }
                         });
      
        QObject::connect(&webSocket, &QWebSocket::textMessageReceived, &webSocket,
                         [](const QString &message)
                         {
                           qDebug() << "RECEIVED ECHO" << message;
                           QMessageBox box;
                           box.setText("This message box blocks the Websockets?");
                           box.exec();
                         }); //with Qt::QueuedConnection all is fine.
      
        heartbeat.start();
        webSocket.open(QUrl("ws://localhost:1234"));
      
        return a.exec();
      }
      
      

      Expected output (and output with Qt::QueuedConnection):

      CONNECTED
      RECEIVED ECHO "T;"
      SEND T;
      RECEIVED ECHO "T;"
      SEND T;
      RECEIVED ECHO "T;"
      SEND T;
      RECEIVED ECHO "T;"
      SEND T;

      Real output:

      CONNECTED
      RECEIVED ECHO "T;"
      SEND T;
      RECEIVED ECHO "T;T;"
      SEND T;
      RECEIVED ECHO "T;T;T;"
      SEND T;
      RECEIVED ECHO "T;T;T;T;"
      SEND T;
      RECEIVED ECHO "T;T;T;T;T;"

        Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

          Activity

            People

            Assignee:
            aleksey_lysenko Aleksey Lysenko
            Reporter:
            g.schoeffl Georg
            Votes:
            1 Vote for this issue
            Watchers:
            1 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:

                Gerrit Reviews

                There are no open Gerrit changes