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. QWebSocketServer does not accept new connections

QWebSocketServer does not accept new connections

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 2 Posters 611 Views 2 Watching
  • 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
    Mark81
    wrote on last edited by
    #1

    Here my own implementation of a wrapper for a web socket communication:

    #include "wsserver.h"
    #include <QDebug>
    
    #define DEBUG
    const QString MODULE_ID = "[WSS]";
    
    WsServer::WsServer(QObject *parent) : QObject(parent)
    {
    }
    
    WsServer::~WsServer()
    {
        qInfo() << MODULE_ID << "Closing connections";
        _wsServer->close();
    }
    
    void WsServer::listen(quint16 port)
    {
        _wsServer = new QWebSocketServer("websocket", QWebSocketServer::NonSecureMode, this);
        if (_wsServer->listen(QHostAddress::Any, port))
        {
            qInfo() << MODULE_ID << "WebSocket server is listening on port" << port;
            connect(_wsServer, &QWebSocketServer::newConnection, this, &WsServer::newConnection);
            connect(_wsServer, &QWebSocketServer::closed, this, &WsServer::closed);
        }
    }
    
    void WsServer::sendTextMessage(QString message)
    {
        foreach (QWebSocket *client, _listClients)
        {
            client->sendTextMessage(message);
        }
    }
    
    void WsServer::newConnection()
    {
        QWebSocket *socket = _wsServer->nextPendingConnection();
    
        connect(socket, &QWebSocket::textMessageReceived, this, &WsServer::processTextMessage);
        connect(socket, &QWebSocket::binaryMessageReceived, this, &WsServer::processBinaryMessage);
        connect(socket, &QWebSocket::disconnected, this, &WsServer::socketDisconnected);
        _listClients.append(socket);
        emit clientConnected();
    
    #ifdef DEBUG
        qInfo() << MODULE_ID << "New client connected" << socket << socket->peerAddress().toString() << QString("(%1)").arg(_listClients.size());
    #endif
    }
    
    void WsServer::socketDisconnected()
    {
        QWebSocket *socket = qobject_cast<QWebSocket *>(sender());
    #ifdef DEBUG
        qInfo() << MODULE_ID << "Socket disconnected" << socket;
    #endif
        if (socket)
        {
            _listClients.removeAll(socket);
            socket->deleteLater();
        }
    }
    
    void WsServer::processTextMessage(QString message)
    {
    #ifdef DEBUG
        qInfo() << MODULE_ID << "Text message received:" << message;
    #endif
        emit textMessageReceived(message);
    }
    
    void WsServer::processBinaryMessage(QByteArray message)
    {
    #ifdef DEBUG
        qInfo() << MODULE_ID << "Binary message received:" << message;
    #endif
        emit binaryMessageReceived(message);
    }
    

    It works fine and every time a client connects I get the related debug message.
    When the browser changes (or reloads) the page I get the socket disconnected and just after the socket connected message.

    As you can see, when a socket connects I print the number of clients currently connected:

    qInfo() << MODULE_ID << "New client connected" << socket << socket->peerAddress().toString() << QString("(%1)").arg(_listClients.size());
    

    and it's always 1, since I'm doing my tests with one browser only.
    After changing/reloading the page some times the javascript websocket is not able to connect anymore.

    In this situation if I close the application I get a long list of messages about new connections and disconnections. I guess there are the failed connections I've described above.

    What's wrong here? What should I change in order to keep clean the socket list and allow the browser to always connect?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      One thing that is missing is the error handling. You should add that to see if something else is going on.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      M 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        One thing that is missing is the error handling. You should add that to see if something else is going on.

        M Offline
        M Offline
        Mark81
        wrote on last edited by Mark81
        #3

        @SGaist I catch the following signals:

        • WebSocket::error
        • QWebSocketServer::acceptError
        • QWebSocketServer::acceptError

        and I was able to detect a CloseCodeBadOperation only when I refreshed the browser page very fast, nothing else.

        This morning I launched my application and I connected a device to the websocket. Got the notification. Then I closed the browser. Got the notification.

        Nothing else happened, but after few hours I tried again to connect but the javascript code timed out like above. No errors server side.

        When I closed the application I got all the pending connection/disconnection notifications.

        Please note that the browser and the server are on the same machine (I'm connecting as localhost).

        Any hint would be very appreciated!

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Are you using multiple threads ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          M 1 Reply Last reply
          0
          • SGaistS SGaist

            Are you using multiple threads ?

            M Offline
            M Offline
            Mark81
            wrote on last edited by
            #5

            @SGaist not at all. At least, not in my code. I use these other classes in my application:

            • QOpcUaProvider
            • QOpcUaClient
            • QOpcUaNode
            • QSqlDatabase
            • QSqlRecord

            I don't know if they can interfere in any way with QWebSocketServer.

            SGaistS 1 Reply Last reply
            0
            • M Mark81

              @SGaist not at all. At least, not in my code. I use these other classes in my application:

              • QOpcUaProvider
              • QOpcUaClient
              • QOpcUaNode
              • QSqlDatabase
              • QSqlRecord

              I don't know if they can interfere in any way with QWebSocketServer.

              SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              I don't think they should.

              Can you reduce your code a minimal example that still exhibits this behaviour ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              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