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. Unable to connect websockets using QT
Qt 6.11 is out! See what's new in the release blog

Unable to connect websockets using QT

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 1.9k 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.
  • G Offline
    G Offline
    gully
    wrote on last edited by
    #1

    Hi,

    I have gone through the code from the following link http://doc.qt.io/qt-5/qtwebsockets-examples.html .Took the same and put it in my code. However I am not able to connect to my websocket. The websocket is not opening on click of the button

    //websocketimpl.h
    #ifndef WEBSOCKETCLIENTIMPL_H
    #define WEBSOCKETCLIENTIMPL_H
    #include <QtCore/QObject>
    #include <QtWebSockets/QWebSocket>
    
    
    class websocketclientimpl : public QObject
    {
        Q_OBJECT
    public:
        explicit websocketclientimpl(const QUrl &url, QObject *parent = Q_NULLPTR);
    
    Q_SIGNALS:
        void closed();
    
    private Q_SLOTS:
        void onConnected();
        void onTextMessageReceived(QString message);
    
    private:
        QWebSocket m_webSocket;
        QUrl m_url;
        bool m_debug;
    };
    
    #endif // WEBSOCKETCLIENTIMPL_H
    
    #include "websocketclientimpl.h"
    #include <QtCore/QDebug>
    #include <QJsonObject>
    #include <QJsonDocument>
    #include <qbytearray.h>
    QT_USE_NAMESPACE
    
    websocketclientimpl::websocketclientimpl(const QUrl &url,QObject *parent) :
        QObject(parent),
        m_url(url)
    
    {
        //if (m_debug)
            qDebug() << "WebSocket server:" << url;
        connect(&m_webSocket, &QWebSocket::connected, this, &websocketclientimpl::onConnected);
        connect(&m_webSocket, &QWebSocket::disconnected, this, &websocketclientimpl::closed);
        m_webSocket.open(QUrl(url));
    }
    
    void websocketclientimpl::onConnected()
    {
    
            qDebug() << "WebSocket connected";
        connect(&m_webSocket, &QWebSocket::textMessageReceived,
                this, &websocketclientimpl::onTextMessageReceived);
        QJsonObject subscriptionObject;
        subscriptionObject.insert("subchannel","1234");
        QJsonDocument doc(subscriptionObject);
        QByteArray ba = doc.toJson();
    
        m_webSocket.sendTextMessage(QString(ba));
    }
    
    void websocketclientimpl::onTextMessageReceived(QString message)
    {
    
            qDebug() << "Message received:" << message;
        //m_webSocket.close();
    }
    
    
    
    #ifndef WEBSOCKETOPERATIONS_H
    #define WEBSOCKETOPERATIONS_H
    #include<QtCore>
    
    class websocketoperations
    {
    public:
        websocketoperations();
        void createWebsocketConnection();
    
    };
    
    #endif // WEBSOCKETOPERATIONS_H
    
    #include "websocketoperations.h"
    #include "websocketclientimpl.h"
    #include <QDebug>
    
    websocketoperations::websocketoperations()
    {
    
    }
    void websocketoperations::createWebsocketConnection()
    {
        websocketclientimpl client(QUrl(QStringLiteral("ws://localhost:8910/zoutboundws")));
    
    }
    
    
    #include "livechart.h"
    #include "ui_livechart.h"
    #include "websocketoperations.h"
    
    livechart::livechart(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::livechart)
    {
        ui->setupUi(this);
    }
    
    livechart::~livechart()
    {
        delete ui;
    }
    
    void livechart::on_buttonStartWebsocketConnection_clicked()
    {
        websocketoperations wsOperationsObj;
        wsOperationsObj.createWebsocketConnection();
    
    }
    
    void livechart::on_stopWebsocketConnection_clicked()
    {
    
    }
    
    

    Can anyone help me,where I am going wrong.

    jsulmJ 1 Reply Last reply
    0
    • G gully

      Hi,

      I have gone through the code from the following link http://doc.qt.io/qt-5/qtwebsockets-examples.html .Took the same and put it in my code. However I am not able to connect to my websocket. The websocket is not opening on click of the button

      //websocketimpl.h
      #ifndef WEBSOCKETCLIENTIMPL_H
      #define WEBSOCKETCLIENTIMPL_H
      #include <QtCore/QObject>
      #include <QtWebSockets/QWebSocket>
      
      
      class websocketclientimpl : public QObject
      {
          Q_OBJECT
      public:
          explicit websocketclientimpl(const QUrl &url, QObject *parent = Q_NULLPTR);
      
      Q_SIGNALS:
          void closed();
      
      private Q_SLOTS:
          void onConnected();
          void onTextMessageReceived(QString message);
      
      private:
          QWebSocket m_webSocket;
          QUrl m_url;
          bool m_debug;
      };
      
      #endif // WEBSOCKETCLIENTIMPL_H
      
      #include "websocketclientimpl.h"
      #include <QtCore/QDebug>
      #include <QJsonObject>
      #include <QJsonDocument>
      #include <qbytearray.h>
      QT_USE_NAMESPACE
      
      websocketclientimpl::websocketclientimpl(const QUrl &url,QObject *parent) :
          QObject(parent),
          m_url(url)
      
      {
          //if (m_debug)
              qDebug() << "WebSocket server:" << url;
          connect(&m_webSocket, &QWebSocket::connected, this, &websocketclientimpl::onConnected);
          connect(&m_webSocket, &QWebSocket::disconnected, this, &websocketclientimpl::closed);
          m_webSocket.open(QUrl(url));
      }
      
      void websocketclientimpl::onConnected()
      {
      
              qDebug() << "WebSocket connected";
          connect(&m_webSocket, &QWebSocket::textMessageReceived,
                  this, &websocketclientimpl::onTextMessageReceived);
          QJsonObject subscriptionObject;
          subscriptionObject.insert("subchannel","1234");
          QJsonDocument doc(subscriptionObject);
          QByteArray ba = doc.toJson();
      
          m_webSocket.sendTextMessage(QString(ba));
      }
      
      void websocketclientimpl::onTextMessageReceived(QString message)
      {
      
              qDebug() << "Message received:" << message;
          //m_webSocket.close();
      }
      
      
      
      #ifndef WEBSOCKETOPERATIONS_H
      #define WEBSOCKETOPERATIONS_H
      #include<QtCore>
      
      class websocketoperations
      {
      public:
          websocketoperations();
          void createWebsocketConnection();
      
      };
      
      #endif // WEBSOCKETOPERATIONS_H
      
      #include "websocketoperations.h"
      #include "websocketclientimpl.h"
      #include <QDebug>
      
      websocketoperations::websocketoperations()
      {
      
      }
      void websocketoperations::createWebsocketConnection()
      {
          websocketclientimpl client(QUrl(QStringLiteral("ws://localhost:8910/zoutboundws")));
      
      }
      
      
      #include "livechart.h"
      #include "ui_livechart.h"
      #include "websocketoperations.h"
      
      livechart::livechart(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::livechart)
      {
          ui->setupUi(this);
      }
      
      livechart::~livechart()
      {
          delete ui;
      }
      
      void livechart::on_buttonStartWebsocketConnection_clicked()
      {
          websocketoperations wsOperationsObj;
          wsOperationsObj.createWebsocketConnection();
      
      }
      
      void livechart::on_stopWebsocketConnection_clicked()
      {
      
      }
      
      

      Can anyone help me,where I am going wrong.

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

      @gully You declare local variables which are destroyed when the method finishes:

      void websocketoperations::createWebsocketConnection()
      {
          websocketclientimpl client(QUrl(QStringLiteral("ws://localhost:8910/zoutboundws"))); // local variable!
      
      }
      
      void livechart::on_buttonStartWebsocketConnection_clicked()
      {
          websocketoperations wsOperationsObj; // local variable
          wsOperationsObj.createWebsocketConnection();
      
      }
      

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

      G 1 Reply Last reply
      0
      • jsulmJ jsulm

        @gully You declare local variables which are destroyed when the method finishes:

        void websocketoperations::createWebsocketConnection()
        {
            websocketclientimpl client(QUrl(QStringLiteral("ws://localhost:8910/zoutboundws"))); // local variable!
        
        }
        
        void livechart::on_buttonStartWebsocketConnection_clicked()
        {
            websocketoperations wsOperationsObj; // local variable
            wsOperationsObj.createWebsocketConnection();
        
        }
        
        G Offline
        G Offline
        gully
        wrote on last edited by gully
        #3

        @jsulm What would be the solution then ?Do I need to wait forever in a infinite loop? I am pretty newbie in C++

        jsulmJ 1 Reply Last reply
        0
        • G gully

          @jsulm What would be the solution then ?Do I need to wait forever in a infinite loop? I am pretty newbie in C++

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

          @gully Declare those variables as class members:

          class livechart
          {
          ...
          private:
              websocketoperations wsOperationsObj
          }
          

          If you're C++ newbie then I would suggest to take time to learn C++, else it could be hard to use Qt.

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

          G 1 Reply Last reply
          3
          • jsulmJ jsulm

            @gully Declare those variables as class members:

            class livechart
            {
            ...
            private:
                websocketoperations wsOperationsObj
            }
            

            If you're C++ newbie then I would suggest to take time to learn C++, else it could be hard to use Qt.

            G Offline
            G Offline
            gully
            wrote on last edited by
            #5

            @jsulm Thanks a lot..it worked.. I would also try to add a pull request to the qt code...the example itself created the confusion.

            mrjjM 1 Reply Last reply
            0
            • G gully

              @jsulm Thanks a lot..it worked.. I would also try to add a pull request to the qt code...the example itself created the confusion.

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

              @gully
              Hi
              Can you show what example?

              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