Unable to connect websockets using QT
Solved
General and Desktop
-
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.
-
@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(); }
-