Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QWebSocket wont connect or give error



  • 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?


  • Lifetime Qt Champion

    @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.



  • @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")


  • Lifetime Qt Champion

    @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)



  • @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....



  • @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



  • 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
    


  • @mrjj you know? :)


  • Lifetime Qt Champion

    @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.



  • @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();
    }
    

  • Lifetime Qt Champion

    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 ?



  • @mrjj i found out why!

    Original :

    Socket.doConnect(selected_board);
    

    Now :

    Socket.doConnect(QStringLiteral("ws://%1:80").arg(selected_board));
    


  • @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 :)



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

    now what?


  • Lifetime Qt Champion

    @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.


Log in to reply