QWebSocket wont connect or give error
-
@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.