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? -
@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. -
@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 aself.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 :Pso i added
MySocket Socket;
tomainwindow.h
header filebut 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 fromSocket.doConnect(selected_board);
but i get an error (atleast something)
WebSocket server: "192.168.10.122" [ERROR][SOCKET] QAbstractSocket::UnsupportedSocketOperationError
-
@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(); }
-
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 ? -
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 useemit onConnected();
somewhere in your code to actually "send" the signal
at the right times.