Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt Creator and other tools
  4. websocket onConnected slot is not fired up when connection is started from button onclick slot
Forum Updated to NodeBB v4.3 + New Features

websocket onConnected slot is not fired up when connection is started from button onclick slot

Scheduled Pinned Locked Moved Unsolved Qt Creator and other tools
8 Posts 4 Posters 537 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.
  • E Offline
    E Offline
    epoepo
    wrote on last edited by epoepo
    #1

    I am trying to open a dynamic websocket connection when a button is clicked. Using the EchoClient example to setup the WS i have a user interface with a button, which once clicked should let the code handle the connection.

    This is a part of what i have until now:
    void MainWindow::on_btn_add_usr_clicked()
    {
    bool debug = true;
    ClientUI client(QUrl(QStringLiteral("ws://127.0.0.1:8020/ws/te/")), debug);
    }

    ClientUI::ClientUI(const QUrl &url, bool debug, QObject *parent)
    : QObject(parent)
    , m_url(url)
    , m_debug(debug)
    { if (m_debug)
    qDebug() << "WebSocket server:" << url;
    connect(&m_webSocket, QOverloadQAbstractSocket::SocketError::of(&QWebSocket::error),[=](QAbstractSocket::SocketError error){
    qDebug()<< error;
    });
    connect(&m_webSocket, &QWebSocket::connected, this, &ClientUI::onConnected);
    qDebug()<< m_webSocket.state();
    m_webSocket.open(QUrl(url));
    qDebug()<< m_webSocket.state();
    }

    void ClientUI::onConnected()
    {
    qDebug()<< m_webSocket.state();
    if (m_debug)
    qDebug() << "WebSocket connected";
    connect(&m_webSocket, &QWebSocket::textMessageReceived, this, &ClientUI::onTextMessageReceived);
    m_webSocket.sendTextMessage(QStringLiteral("yayayayayya"));
    }

    My problem is that my program never passes in the onConnected method, i can see that WS is in the QAbstractSocket::ConnectingState but al ends there.

    jsulmJ 1 Reply Last reply
    0
    • E epoepo

      I am trying to open a dynamic websocket connection when a button is clicked. Using the EchoClient example to setup the WS i have a user interface with a button, which once clicked should let the code handle the connection.

      This is a part of what i have until now:
      void MainWindow::on_btn_add_usr_clicked()
      {
      bool debug = true;
      ClientUI client(QUrl(QStringLiteral("ws://127.0.0.1:8020/ws/te/")), debug);
      }

      ClientUI::ClientUI(const QUrl &url, bool debug, QObject *parent)
      : QObject(parent)
      , m_url(url)
      , m_debug(debug)
      { if (m_debug)
      qDebug() << "WebSocket server:" << url;
      connect(&m_webSocket, QOverloadQAbstractSocket::SocketError::of(&QWebSocket::error),[=](QAbstractSocket::SocketError error){
      qDebug()<< error;
      });
      connect(&m_webSocket, &QWebSocket::connected, this, &ClientUI::onConnected);
      qDebug()<< m_webSocket.state();
      m_webSocket.open(QUrl(url));
      qDebug()<< m_webSocket.state();
      }

      void ClientUI::onConnected()
      {
      qDebug()<< m_webSocket.state();
      if (m_debug)
      qDebug() << "WebSocket connected";
      connect(&m_webSocket, &QWebSocket::textMessageReceived, this, &ClientUI::onTextMessageReceived);
      m_webSocket.sendTextMessage(QStringLiteral("yayayayayya"));
      }

      My problem is that my program never passes in the onConnected method, i can see that WS is in the QAbstractSocket::ConnectingState but al ends there.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @epoepo said in websocket onConnected slot is not fired up when connection is started from button onclick slot:

      void MainWindow::on_btn_add_usr_clicked()
      {
      bool debug = true;
      ClientUI client(QUrl(QStringLiteral("ws://127.0.0.1:8020/ws/te/")), debug);
      }

      client is a local variable and is destroyed as soon as on_btn_add_usr_clicked() finishes...

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • E Offline
        E Offline
        epoepo
        wrote on last edited by
        #3

        thanks for the reply.
        my c++ skills are a bit rusty so i might not be able to logic properly, but since i am calling the constructor in the on_btn_add_usr_clicked shouldn't the program go through all the functions called in its implementation?

        JonBJ KroMignonK jsulmJ 3 Replies Last reply
        0
        • E epoepo

          thanks for the reply.
          my c++ skills are a bit rusty so i might not be able to logic properly, but since i am calling the constructor in the on_btn_add_usr_clicked shouldn't the program go through all the functions called in its implementation?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @epoepo
          Don't know what you mean. As @jsulm says client will disappear at the end of MainWindow::on_btn_add_usr_clicked(). So e.g. the connect(&m_webSocket, ...) will be undone.

          I don't know, maybe you intend ClientUI client to be a member variable in MainWindow so that it persists as long as the MainWindow instance does.

          1 Reply Last reply
          2
          • E epoepo

            thanks for the reply.
            my c++ skills are a bit rusty so i might not be able to logic properly, but since i am calling the constructor in the on_btn_add_usr_clicked shouldn't the program go through all the functions called in its implementation?

            KroMignonK Offline
            KroMignonK Offline
            KroMignon
            wrote on last edited by
            #5

            @epoepo said in websocket onConnected slot is not fired up when connection is started from button onclick slot:

            my c++ skills are a bit rusty so i might not be able to logic properly, but since i am calling the constructor in the on_btn_add_usr_clicked shouldn't the program go through all the functions called in its implementation?

            Please, start with learning about variable scope and lifetime, this is a very basic C/C++ knowledge!
            There are so many C++ guides/tutorial available on internet, take time to learn language basics or you will only get frustrated with C++/Qt.

            It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

            1 Reply Last reply
            1
            • E Offline
              E Offline
              epoepo
              wrote on last edited by
              #6

              thank you @KroMignon yes, you are right i need to start learning c++ again. i am already frustrated and it is only the beginning of what i have to do

              thank you @JonB, i will try what you said after i go back to the basics, hopefully it will make sense after i finish studying

              1 Reply Last reply
              0
              • E epoepo

                thanks for the reply.
                my c++ skills are a bit rusty so i might not be able to logic properly, but since i am calling the constructor in the on_btn_add_usr_clicked shouldn't the program go through all the functions called in its implementation?

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @epoepo said in websocket onConnected slot is not fired up when connection is started from button onclick slot:

                but since i am calling the constructor in the on_btn_add_usr_clicked shouldn't the program go through all the functions called in its implementation?

                It goes through the constructor. But just after that client is deleted, so the slot will never be called (connect does not call the slot, it only connects the signal to the slot and m_webSocket.open(QUrl(url)) is an asynchronous call).

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                2
                • E Offline
                  E Offline
                  epoepo
                  wrote on last edited by
                  #8

                  @jsulm it is starting to make sense now, thank you

                  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