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. QWebSocket wont connect or give error
Qt 6.11 is out! See what's new in the release blog

QWebSocket wont connect or give error

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 3 Posters 4.4k Views 1 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.
  • K Offline
    K Offline
    Kris Revi
    wrote on last edited by
    #1

    MySocket.cpp -> https://hastebin.com/dohoqixolo.cpp

    // =========================================================================================================== //
    // INCLUDE
    // =========================================================================================================== //
    #include "MySocket.h"
    #include <QDebug>
    
    QT_USE_NAMESPACE
    
    // =========================================================================================================== //
    // CONSTRUCTOR
    // =========================================================================================================== //
    MySocket::MySocket(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, &QWebSocket::connected, this, &MySocket::onConnected);
        connect(&m_webSocket, &QWebSocket::disconnected, this, &MySocket::closed);
        connect(&m_webSocket, QOverload<QAbstractSocket::SocketError>::of(&QWebSocket::error), [=](QAbstractSocket::SocketError error)
        {
           qDebug() << "[ERROR][SOCKET] " << error;
        });
        m_webSocket.open(QUrl(url));
    }
    
    // =========================================================================================================== //
    // Socket On Connected
    // =========================================================================================================== //
    void MySocket::onConnected()
    {
        if (m_debug)
            qDebug() << "WebSocket connected";
    }
    // =========================================================================================================== //
    // Send Message (For Strips)
    // =========================================================================================================== //
    void MySocket::sendCommandStrip(const QString &bName, int bValue)
    {
        m_webSocket.sendTextMessage(QStringLiteral("{%0:%1}").arg(bName).arg(bValue));
    }
    // =========================================================================================================== //
    // Send Message (For Matrix)
    // =========================================================================================================== //
    void MySocket::sendCommandMatrix(QString bValue)
    {
        m_webSocket.sendTextMessage(bValue);
    }
    // =========================================================================================================== //
    // Socket Close
    // =========================================================================================================== //
    void MySocket::close()
    {
        m_webSocket.close();
    }
    

    MySocket.h -> https://hastebin.com/cacucovasi.cpp

    #ifndef MYSOCKET_H
    #define MYSOCKET_H
    
    // =========================================================================================================== //
    // INCLUDE
    // =========================================================================================================== //
    #include <QObject>
    #include <QtWebSockets/QWebSocket>
    
    // =========================================================================================================== //
    // Socket CLASS
    // =========================================================================================================== //
    class MySocket : public QObject
    {
        Q_OBJECT
    public:
        explicit MySocket(const QUrl &url, bool debug = false, QObject *parent = nullptr);
    
    Q_SIGNALS:
        void closed();
    
    private Q_SLOTS:
        void onConnected();
        void sendCommandStrip(const QString &bName, int bValue);
        void sendCommandMatrix(QString bValue);
        void close();
    
    private:
        QWebSocket m_webSocket;
        QUrl m_url;
        bool m_debug;
    };
    
    #endif // MYSOCKET_H
    

    Then i connect using this function in mainwindow.cpp

    void connectTo()
    {
        MySocket client(QUrl(QStringLiteral("ws://192.168.10.122:80")), true);
    }
    

    only output i get is WebSocket server: QUrl("ws://192.168.10.122:80") nothing else :S it does not connect or give any error! what am i doing wrong?

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

      @Kris-Revi said in QWebSocket wont connect or give error:

      void connectTo()
      {
      MySocket client(QUrl(QStringLiteral("ws://192.168.10.122:80")), true);
      }

      Hi
      But wont the client die very fast ?
      Since its async, it might not get a chance to report anything as it runs out of scope and gets deleted.

      K 1 Reply Last reply
      1
      • mrjjM mrjj

        @Kris-Revi said in QWebSocket wont connect or give error:

        void connectTo()
        {
        MySocket client(QUrl(QStringLiteral("ws://192.168.10.122:80")), true);
        }

        Hi
        But wont the client die very fast ?
        Since its async, it might not get a chance to report anything as it runs out of scope and gets deleted.

        K Offline
        K Offline
        Kris Revi
        wrote on last edited by
        #3

        @mrjj i am not sure! first time touching Qt C++ ! i've done this in PyQt5 Python but yea

        it doesn't seem to connect at all! it just spits out the qDebug message WebSocket server: QUrl("ws://192.168.10.122:80")

        mrjjM JonBJ 2 Replies Last reply
        0
        • K Kris Revi

          @mrjj i am not sure! first time touching Qt C++ ! i've done this in PyQt5 Python but yea

          it doesn't seem to connect at all! it just spits out the qDebug message WebSocket server: QUrl("ws://192.168.10.122:80")

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          @Kris-Revi
          Hi
          well since its a variable in a function it will be deleted as soon as function ends.
          So that might happen really fast before it get chance to signal anything else.

          Try having
          MySocket client; as a member and not as a local variable.
          and see if that helps.

          you can also do
          void connectTo()
          {
          MySocket* client= new MySocket (QUrl(QStringLiteral("ws://192.168.10.122:80")), true);
          }

          but its a memory leak but fine to test if that is indeed the issue.
          (killed by scope)

          K 1 Reply Last reply
          0
          • K Kris Revi

            @mrjj i am not sure! first time touching Qt C++ ! i've done this in PyQt5 Python but yea

            it doesn't seem to connect at all! it just spits out the qDebug message WebSocket server: QUrl("ws://192.168.10.122:80")

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

            @Kris-Revi said in QWebSocket wont connect or give error:

            @mrjj i am not sure! first time touching Qt C++ ! i've done this in PyQt5 Python but yea

            In your PyQt5, did you do same client = MySocket (...) where client was just a local label in a method; or did you do a self.client = MySocket (...), or pass a parent? Because @mrjj is right, and you will have to be even more careful about scope in C++ than in Python....

            K 1 Reply Last reply
            1
            • JonBJ JonB

              @Kris-Revi said in QWebSocket wont connect or give error:

              @mrjj i am not sure! first time touching Qt C++ ! i've done this in PyQt5 Python but yea

              In your PyQt5, did you do same client = MySocket (...) where client was just a local label in a method; or did you do a self.client = MySocket (...), or pass a parent? Because @mrjj is right, and you will have to be even more careful about scope in C++ than in Python....

              K Offline
              K Offline
              Kris Revi
              wrote on last edited by
              #6

              @JonB yea i did self.client so yea difference here is local vs class member :P

              so i added MySocket Socket; to mainwindow.h header file

              but how do i now call that

              MySocket client(QUrl(QStringLiteral("ws://%1:80").arg(selected_board)), true);
              

              if i do

              Socket(QUrl(QStringLiteral("ws://%1:80").arg(selected_board)), true);
              

              i get type 'MySocket' does not provide a call operator

              1 Reply Last reply
              0
              • K Offline
                K Offline
                Kris Revi
                wrote on last edited by
                #7

                so i did it like this

                // =========================================================================================================== //
                // CONSTRUCTOR
                // =========================================================================================================== //
                MySocket::MySocket(QObject *parent) : QObject(parent)
                {
                    connect(&m_webSocket, &QWebSocket::connected, this, &MySocket::onConnected);
                    connect(&m_webSocket, &QWebSocket::disconnected, this, &MySocket::closed);
                    connect(&m_webSocket, QOverload<QAbstractSocket::SocketError>::of(&QWebSocket::error), [=](QAbstractSocket::SocketError error)
                    {
                       qDebug() << "[ERROR][SOCKET] " << error;
                    });
                    
                }
                
                // =========================================================================================================== //
                // Socket Connected
                // =========================================================================================================== //
                void MySocket::doConnect(QString URL)
                {
                    qDebug() << "WebSocket server:" << URL;
                    m_webSocket.open(QUrl(URL));
                }
                

                i moved the .open part to it's own function and called it from Socket.doConnect(selected_board);

                but i get an error (atleast something)

                WebSocket server: "192.168.10.122"
                [ERROR][SOCKET]  QAbstractSocket::UnsupportedSocketOperationError
                
                1 Reply Last reply
                0
                • mrjjM mrjj

                  @Kris-Revi
                  Hi
                  well since its a variable in a function it will be deleted as soon as function ends.
                  So that might happen really fast before it get chance to signal anything else.

                  Try having
                  MySocket client; as a member and not as a local variable.
                  and see if that helps.

                  you can also do
                  void connectTo()
                  {
                  MySocket* client= new MySocket (QUrl(QStringLiteral("ws://192.168.10.122:80")), true);
                  }

                  but its a memory leak but fine to test if that is indeed the issue.
                  (killed by scope)

                  K Offline
                  K Offline
                  Kris Revi
                  wrote on last edited by
                  #8

                  @mrjj you know? :)

                  mrjjM 1 Reply Last reply
                  0
                  • K Kris Revi

                    @mrjj you know? :)

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @Kris-Revi
                    Hi
                    It can say this error if you ask it to do something while connecting state but i dont see code that would do it.

                    K 1 Reply Last reply
                    0
                    • mrjjM mrjj

                      @Kris-Revi
                      Hi
                      It can say this error if you ask it to do something while connecting state but i dont see code that would do it.

                      K Offline
                      K Offline
                      Kris Revi
                      wrote on last edited by
                      #10

                      @mrjj so what i do is this (keep in mind the code above)

                      void MainWindow::findIP(QString Key)
                      {
                          auto key = Key;
                              if ( board.count(key) ) {
                                  DeviceInfo &di = board[key];
                                  connectTo( di.ip );
                              }
                              else
                              {
                                  qDebug() << "[ERROR][STRUCT] Did not find key using the word : " << Key;
                              }
                      }
                      
                      // This is a button bind on clicked (one of the Devices) triggers the function above
                      void MainWindow::on_BUTTON_STUDIOLIGHT_clicked() { findIP("Studio Lights"); }
                      
                      // This is function that gets called from findIP function todo the connection with socket
                      void MainWindow::connectTo(QString selected_board)
                      {
                          Socket.doConnect(selected_board);
                      }
                      

                      and this is the MySocket.cpp file

                      // =========================================================================================================== //
                      // INCLUDE
                      // =========================================================================================================== //
                      #include "MySocket.h"
                      #include <QDebug>
                      
                      QT_USE_NAMESPACE
                      
                      // =========================================================================================================== //
                      // CONSTRUCTOR
                      // =========================================================================================================== //
                      MySocket::MySocket(QObject *parent) : QObject(parent)
                      {
                          connect(&m_webSocket, &QWebSocket::connected, this, &MySocket::onConnected);
                          connect(&m_webSocket, &QWebSocket::disconnected, this, &MySocket::closed);
                          connect(&m_webSocket, QOverload<QAbstractSocket::SocketError>::of(&QWebSocket::error), [=](QAbstractSocket::SocketError error)
                          {
                             qDebug() << "[ERROR][SOCKET] " << error;
                          });
                          
                      }
                      
                      // =========================================================================================================== //
                      // Socket Connected
                      // =========================================================================================================== //
                      void MySocket::doConnect(QString URL)
                      {
                          qDebug() << "WebSocket server:" << URL;
                          m_webSocket.open(QUrl(URL));
                      }
                      // =========================================================================================================== //
                      // Socket On Connected
                      // =========================================================================================================== //
                      void MySocket::onConnected()
                      {
                          if (m_debug)
                              qDebug() << "WebSocket connected";
                      }
                      // =========================================================================================================== //
                      // Send Message (For Strips)
                      // =========================================================================================================== //
                      void MySocket::sendCommandStrip(const QString &bName, int bValue)
                      {
                          m_webSocket.sendTextMessage(QStringLiteral("{%0:%1}").arg(bName).arg(bValue));
                      }
                      // =========================================================================================================== //
                      // Send Message (For Matrix)
                      // =========================================================================================================== //
                      void MySocket::sendCommandMatrix(QString bValue)
                      {
                          m_webSocket.sendTextMessage(bValue);
                      }
                      // =========================================================================================================== //
                      // Socket Close
                      // =========================================================================================================== //
                      void MySocket::close()
                      {
                          m_webSocket.close();
                      }
                      
                      1 Reply Last reply
                      0
                      • mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        Hi
                        It looks fine but i wonder i QUrl likes an IP as that is what you give it.
                        Also those IPs. could that request HTTPS so that you need SSL compiled and enabled ?

                        K 2 Replies Last reply
                        0
                        • mrjjM mrjj

                          Hi
                          It looks fine but i wonder i QUrl likes an IP as that is what you give it.
                          Also those IPs. could that request HTTPS so that you need SSL compiled and enabled ?

                          K Offline
                          K Offline
                          Kris Revi
                          wrote on last edited by
                          #12

                          @mrjj i found out why!

                          Original :

                          Socket.doConnect(selected_board);
                          

                          Now :

                          Socket.doConnect(QStringLiteral("ws://%1:80").arg(selected_board));
                          
                          1 Reply Last reply
                          1
                          • mrjjM mrjj

                            Hi
                            It looks fine but i wonder i QUrl likes an IP as that is what you give it.
                            Also those IPs. could that request HTTPS so that you need SSL compiled and enabled ?

                            K Offline
                            K Offline
                            Kris Revi
                            wrote on last edited by
                            #13

                            @mrjj how do i do this in Qt C++

                            Python (custom Qt Signal)

                            error = pyqtSignal(QtNetwork.QAbstractSocket.SocketError)
                            

                            so that i can hookup error and connected signal to slots in mainwindow.cpp :)

                            1 Reply Last reply
                            0
                            • K Offline
                              K Offline
                              Kris Revi
                              wrote on last edited by
                              #14

                              I've put onError and onConnected under Q_SIGNALS: in the MySocket.h in the class!

                              now what?

                              mrjjM 1 Reply Last reply
                              0
                              • K Kris Revi

                                I've put onError and onConnected under Q_SIGNALS: in the MySocket.h in the class!

                                now what?

                                mrjjM Offline
                                mrjjM Offline
                                mrjj
                                Lifetime Qt Champion
                                wrote on last edited by
                                #15

                                @Kris-Revi

                                Hi
                                Then you have defined 3 new signals for your class.
                                Another class could now use the connect statement to
                                hook up a slot to your new signals.

                                However, for something to happen.
                                You must use

                                emit onConnected(); 
                                

                                somewhere in your code to actually "send" the signal
                                at the right times.

                                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