websocket onConnected slot is not fired up when connection is started from button onclick slot
-
I am trying to open a dynamic websocket connection when a button is clicked. Using the EchoClient example to setup the WS i have a user interface with a button, which once clicked should let the code handle the connection.
This is a part of what i have until now:
void MainWindow::on_btn_add_usr_clicked()
{
bool debug = true;
ClientUI client(QUrl(QStringLiteral("ws://127.0.0.1:8020/ws/te/")), debug);
}ClientUI::ClientUI(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, QOverloadQAbstractSocket::SocketError::of(&QWebSocket::error),[=](QAbstractSocket::SocketError error){
qDebug()<< error;
});
connect(&m_webSocket, &QWebSocket::connected, this, &ClientUI::onConnected);
qDebug()<< m_webSocket.state();
m_webSocket.open(QUrl(url));
qDebug()<< m_webSocket.state();
}void ClientUI::onConnected()
{
qDebug()<< m_webSocket.state();
if (m_debug)
qDebug() << "WebSocket connected";
connect(&m_webSocket, &QWebSocket::textMessageReceived, this, &ClientUI::onTextMessageReceived);
m_webSocket.sendTextMessage(QStringLiteral("yayayayayya"));
}My problem is that my program never passes in the onConnected method, i can see that WS is in the QAbstractSocket::ConnectingState but al ends there.
-
@epoepo said in websocket onConnected slot is not fired up when connection is started from button onclick slot:
void MainWindow::on_btn_add_usr_clicked()
{
bool debug = true;
ClientUI client(QUrl(QStringLiteral("ws://127.0.0.1:8020/ws/te/")), debug);
}client is a local variable and is destroyed as soon as on_btn_add_usr_clicked() finishes...
-
@epoepo
Don't know what you mean. As @jsulm saysclient
will disappear at the end ofMainWindow::on_btn_add_usr_clicked()
. So e.g. theconnect(&m_webSocket, ...)
will be undone.I don't know, maybe you intend
ClientUI client
to be a member variable inMainWindow
so that it persists as long as theMainWindow
instance does. -
@epoepo said in websocket onConnected slot is not fired up when connection is started from button onclick slot:
my c++ skills are a bit rusty so i might not be able to logic properly, but since i am calling the constructor in the on_btn_add_usr_clicked shouldn't the program go through all the functions called in its implementation?
Please, start with learning about variable scope and lifetime, this is a very basic C/C++ knowledge!
There are so many C++ guides/tutorial available on internet, take time to learn language basics or you will only get frustrated with C++/Qt. -
thank you @KroMignon yes, you are right i need to start learning c++ again. i am already frustrated and it is only the beginning of what i have to do
thank you @JonB, i will try what you said after i go back to the basics, hopefully it will make sense after i finish studying
-
@epoepo said in websocket onConnected slot is not fired up when connection is started from button onclick slot:
but since i am calling the constructor in the on_btn_add_usr_clicked shouldn't the program go through all the functions called in its implementation?
It goes through the constructor. But just after that client is deleted, so the slot will never be called (connect does not call the slot, it only connects the signal to the slot and m_webSocket.open(QUrl(url)) is an asynchronous call).