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. Multithreading and WebSocket
Forum Updated to NodeBB v4.3 + New Features

Multithreading and WebSocket

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 2 Posters 713 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.
  • T Offline
    T Offline
    tux_brazilian
    wrote on last edited by
    #1

    I'm trying to connect two or more esp32s that send their sensory information via websocket, but only one is communicating at a time, thus being an asynchronous connection, the opposite of what was supposed to be happening.

    Server startup:

    WebSocketServer::WebSocketServer(QObject *parent): QObject(parent), websocketserver(new QWebSocketServer(QStringLiteral("ComTrolAcao Server"),QWebSocketServer::NonSecureMode, this))
      {
      if(websocketserver->listen(QHostAddress::Any, 1234))
        {
        qDebug() << "Started server";
        connect(websocketserver, &QWebSocketServer::newConnection, this, &WebSocketServer::WebSocketNovo);
        }
    

    WebSocket handling:

    void
    WebSocketServer::WebSocketNovo()
      { 
      QWebSocket *pSocket = websocketserver->nextPendingConnection();
      qDebug() << pSocket->peerAddress().toString() << ":" << QString::number(pSocket->peerPort()) << " CONECTADO!\n";
      pSocket->setParent(this);
      
      connect(pSocket, &QWebSocket::textMessageReceived, this, &WebSocketServer::WebSocketProcessaTexto);
      connect(pSocket, &QWebSocket::disconnected, this, &WebSocketServer::WebSocketDesconecta);
      
      clients << pSocket;
      qDebug() << clients;
      }
    void
    WebSocketServer::WebSocketProcessaTexto(QString mensagem)
      {
      QStringList dados;
      QWebSocket *pClient = qobject_cast<QWebSocket *>(sender());
      
      //qDebug() << "MENSAGE RECEVEID:" << mensagem;
      }   
      
    void
    WebSocketServer::WebSocketDesconecta()
      {
        QWebSocket *pClient = qobject_cast<QWebSocket *>(sender());
        if (pClient)
        {
            clients.removeAll(pClient);
            pClient->deleteLater();
        }
      }
    

    As a solution I thought of using threads to solve this, opening a thread for each connection so that they can communicate all at once through the websocket. I know it's wrong, but something like:

      if(websocketserver->listen(QHostAddress::Any, 1234))
        {
        qDebug() << "Started Server";
        QtConcurrent::run(connect(websocketserver, &QWebSocketServer::newConnection, this, &WebSocketServer::WebSocketNovo));
        }
        
    

    How can I use QTConcurrent to open independent threads for each Websocket connection or how can I use threads for this without QTConcurrent?

    Thanks, Lima.

    Christian EhrlicherC 1 Reply Last reply
    0
    • T tux_brazilian

      I'm trying to connect two or more esp32s that send their sensory information via websocket, but only one is communicating at a time, thus being an asynchronous connection, the opposite of what was supposed to be happening.

      Server startup:

      WebSocketServer::WebSocketServer(QObject *parent): QObject(parent), websocketserver(new QWebSocketServer(QStringLiteral("ComTrolAcao Server"),QWebSocketServer::NonSecureMode, this))
        {
        if(websocketserver->listen(QHostAddress::Any, 1234))
          {
          qDebug() << "Started server";
          connect(websocketserver, &QWebSocketServer::newConnection, this, &WebSocketServer::WebSocketNovo);
          }
      

      WebSocket handling:

      void
      WebSocketServer::WebSocketNovo()
        { 
        QWebSocket *pSocket = websocketserver->nextPendingConnection();
        qDebug() << pSocket->peerAddress().toString() << ":" << QString::number(pSocket->peerPort()) << " CONECTADO!\n";
        pSocket->setParent(this);
        
        connect(pSocket, &QWebSocket::textMessageReceived, this, &WebSocketServer::WebSocketProcessaTexto);
        connect(pSocket, &QWebSocket::disconnected, this, &WebSocketServer::WebSocketDesconecta);
        
        clients << pSocket;
        qDebug() << clients;
        }
      void
      WebSocketServer::WebSocketProcessaTexto(QString mensagem)
        {
        QStringList dados;
        QWebSocket *pClient = qobject_cast<QWebSocket *>(sender());
        
        //qDebug() << "MENSAGE RECEVEID:" << mensagem;
        }   
        
      void
      WebSocketServer::WebSocketDesconecta()
        {
          QWebSocket *pClient = qobject_cast<QWebSocket *>(sender());
          if (pClient)
          {
              clients.removeAll(pClient);
              pClient->deleteLater();
          }
        }
      

      As a solution I thought of using threads to solve this, opening a thread for each connection so that they can communicate all at once through the websocket. I know it's wrong, but something like:

        if(websocketserver->listen(QHostAddress::Any, 1234))
          {
          qDebug() << "Started Server";
          QtConcurrent::run(connect(websocketserver, &QWebSocketServer::newConnection, this, &WebSocketServer::WebSocketNovo));
          }
          
      

      How can I use QTConcurrent to open independent threads for each Websocket connection or how can I use threads for this without QTConcurrent?

      Thanks, Lima.

      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @tux_brazilian said in Multithreading and WebSocket:

      but only one is communicating at a time,

      What does this mean? Does the second one not connect to your WebSocket? Or do you remove the second connection or what exactly does not work?
      No need for a thread here - will not help at all.

      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
      • T Offline
        T Offline
        tux_brazilian
        wrote on last edited by
        #3

        @Christian-Ehrlicher said in Multithreading and WebSocket:

        What does this mean? Does the second one not connect to your WebSocket? Or do you remove the second connection or what exactly does not work?

        As the esp32 is constantly sending information from the sensors, the first one connects and starts transmitting this data via websocket, but the second esp32 even connects to the server, but since the first one is already transmitting, the second one cannot transmit, in case I disconnect the second esp32, will soon start transmitting through the websocket

        Christian EhrlicherC 1 Reply Last reply
        0
        • T tux_brazilian

          @Christian-Ehrlicher said in Multithreading and WebSocket:

          What does this mean? Does the second one not connect to your WebSocket? Or do you remove the second connection or what exactly does not work?

          As the esp32 is constantly sending information from the sensors, the first one connects and starts transmitting this data via websocket, but the second esp32 even connects to the server, but since the first one is already transmitting, the second one cannot transmit, in case I disconnect the second esp32, will soon start transmitting through the websocket

          Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @tux_brazilian said in Multithreading and WebSocket:

          to the server, but since the first one is already transmitting, the second one cannot transmit,

          Why should this not be possible? A server can handle more than one client connection and your code you show us already does it correctly

          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
          • T Offline
            T Offline
            tux_brazilian
            wrote on last edited by
            #5

            Yes, but when you tried the clients at the same time, the server couldn't handle it and disconnected

            Christian EhrlicherC 1 Reply Last reply
            0
            • T tux_brazilian

              Yes, but when you tried the clients at the same time, the server couldn't handle it and disconnected

              Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @tux_brazilian said in Multithreading and WebSocket:

              the server couldn't handle it and disconnected

              Since you program the server I would guess you're doing something wrong. The server does not disconnect a connection by itself. You must be doing it somehow.

              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

              • Login

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