Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt WebKit
  4. Data via Qt Creator Console from WebSocket

Data via Qt Creator Console from WebSocket

Scheduled Pinned Locked Moved Unsolved Qt WebKit
7 Posts 3 Posters 1.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Samuel123
    wrote on last edited by
    #1

    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?

    ? 2 Replies Last reply
    0
    • S Samuel123

      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?

      ? Offline
      ? Offline
      A Former User
      wrote on last edited by A Former User
      #2

      @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.

      S 1 Reply Last reply
      0
      • ? A Former User

        @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.

        S Offline
        S Offline
        Samuel123
        wrote on last edited by
        #3

        @ankou29666 Thank you for your answer. Can you provide an example of how to integrate that into a code?

        1 Reply Last reply
        0
        • S Samuel123

          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?

          ? Offline
          ? Offline
          A Former User
          wrote on last edited by A Former User
          #4

          @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;
          });
          
          S 1 Reply Last reply
          0
          • ? A Former User

            @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;
            });
            
            S Offline
            S Offline
            Samuel123
            wrote on last edited by
            #5

            @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"?

            SGaistS 1 Reply Last reply
            0
            • S Samuel123

              @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"?

              SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Hi,

              Why are you closing the socket before calling ˋapp.exec();` ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              S 1 Reply Last reply
              1
              • SGaistS SGaist

                Hi,

                Why are you closing the socket before calling ˋapp.exec();` ?

                S Offline
                S Offline
                Samuel123
                wrote on last edited by
                #7

                @SGaist Now it worked. Thanks very much.

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved