Data via Qt Creator Console from WebSocket
-
Hello,
I would like to know how to output information from a WebSocket via Qt Creator Console. This is my code, which is not working:#include <QCoreApplication>
#include <QWebSocket>
#include <QtDebug>int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);//WebSocket: QWebSocket *socket = new QWebSocket(); socket->open(QUrl("ws://IP-Adresse")); QObject::connect(socket, &QWebSocket::connected, [] () { qDebug() << "WebSocket connected"; }); QObject::connect(socket, &QWebSocket::connected, [] (const QString &message) { qDebug() << Nachricht: " << message; }); QObject::connect(socket, &QWebSocket::disconnected, [] () { qDebug() << "WebSocket disconnected"; }); socket->close(); return a.exec();
}
Does someone have solutions?
-
@Samuel123 yes easy one : connect your lambda function to the appropriate signal : use textMessageReceived or binaryMessageReceived which are both intended to be emitted upon message reception on the contrary of connected which wont ever be triggered in that case.
-
@ankou29666 Thank you for your answer. Can you provide an example of how to integrate that into a code?
-
@Samuel123 said in Data via Qt Creator Console from WebSocket:
QObject::connect(socket, &QWebSocket::connected, [] (const QString &message) {
qDebug() << Nachricht: " << message;
});becomes
QObject::connect(socket, &QWebSocket::textMessageReceived, [] (const QString &message) { qDebug() << Nachricht: " << message; });
-
@ankou29666 I changed it, but it didn't make any difference. Do you know if it's enough to just enter the IP address of the WebSocket after "socket->open"?