Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Subscription message not being received in Qt

Subscription message not being received in Qt

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 2 Posters 795 Views 1 Watching
  • 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.
  • X Offline
    X Offline
    xxxcucus
    wrote on last edited by
    #1

    I am testing a STOMP communication between a Spring Boot server and a Qt desktop program.
    The server implementation looks as follows:

    @Controller
    class GreetingController {
    
         @MessageMapping("/hello")
         @SendTo("/topic/greetings")
         @Throws(Exception::class)
         fun greeting(@RequestBody message : HelloMessage) : Greeting  {
             Thread.sleep(1000); // simulated delay
             return Greeting("Hello, " + HtmlUtils.htmlEscape(message.getName()) + "!")
         }
    }
    

    The Qt program looks as follows:

    bool StompClient::sendFrame(std::shared_ptr<StompFrame> stompFrame) {
         if (!stompFrame->isBinaryBody()) {
             QString str = stompFrame->convertToQString();
             m_Socket.sendTextMessage(str);
             qDebug() << "Send stomp frame " << str;
             return true;
        }
    
        QByteArray ba = stompFrame->convertToByteArray();
        m_Socket.sendBinaryMessage(ba);
        return true;
     }
    
     std::shared_ptr<StompFrame> StompFrameCreator::createSubscribeFrame(const QString&   destination) {
         auto stompFrame = std::make_shared<StompFrame>();
         stompFrame->setCommand(StompFrame::HeaderTypes::SUBSCRIBE);
         stompFrame->addHeader("destination", destination);
         return stompFrame;
    }
    
    std::shared_ptr<StompFrame> StompFrameCreator::createSendTextFrame(const QString& destination, const QString& message) {
         auto stompFrame = std::make_shared<StompFrame>();
         stompFrame->setCommand(StompFrame::HeaderTypes::SEND);
         stompFrame->addHeader("destination", destination);
         stompFrame->addHeader("content-type", "application/json");
         stompFrame->addTextBody(message);
         return stompFrame;
     }
    
    void StompDemoView::sendGreeting() {
         qDebug() << "Send greeting";
         StompFrameCreator stompFrameCreator;
    
         QJsonObject jsonObject;
         jsonObject.insert("name", QJsonValue::fromVariant(m_NameLineEdit->text()));
         QJsonDocument doc(jsonObject);
    
         auto publishFrame = stompFrameCreator.createSendTextFrame("/app/hello", doc.toJson());
         qDebug() << "Sending " << doc.toJson();
         m_StompClient.sendFrame(publishFrame);
     }
    
    void StompDemoView::clientConnected() {
        StompFrameCreator stompFrameCreator;
        qDebug() << "Subscribe to topic";
        auto subscribeFrame = stompFrameCreator.createSubscribeFrame(0, "/topic/greetings", "auto");
        m_StompClient.sendFrame(subscribeFrame);
    }
    

    When the client connects to the server I am sending a subscribe request. This is being received by the server:

     Decoded SUBSCRIBE {id=[0], destination=[/topic/greetings], ack=[auto]} session=null
     From client: SUBSCRIBE /topic/greetings id=0 session=6cc3a2b6-48a5-1c1c-2366-d746224d60e8
     Processing SUBSCRIBE /topic/greetings id=0 session=6cc3a2b6-48a5-1c1c-2366-d746224d60e8
    

    Then when a user performs a specific action in the GUI I am sending the message to the Greeting controller which should send back a message to the subscribing client:

      Decoded SEND {destination=[/app/hello], content-type=[application/json]} session=null         application/json payload={
    "name": "Cris"
    

    }

      From client: SEND /app/hello session=6cc3a2b6-48a5-1c1c-2366-d746224d60e8 application/json payload={
     "name": "Cris"
    

    }

      Searching methods to handle SEND /app/hello session=6cc3a2b6-48a5-1c1c-2366-d746224d60e8 application/json payload={
    "name": "Cris"
    

    }
    , lookupDestination='/hello'
    Found 1 handler methods: [{[MESSAGE],[/hello]}]
    Invoking GreetingController#greeting[1 args]
    Arguments: [HelloMessage@3903b632]
    Processing return value with SendToMethodReturnValueHandler [annotationRequired=true]
    Processing MESSAGE destination=/topic/greetings session=6cc3a2b6-48a5-1c1c-2366-d746224d60e8 payload={"content":"Hello, Cris!"}
    Broadcasting to 1 sessions.

    But I am not receiving anything on the client. I created slots for the signals binaryMessageReceived, textMessageReceived, binaryFrameReceived, textFrameReceived, errorOccured for the QWebSocket used in the communication.

    Can anyone point me please in the right direction here ?

    1 Reply Last reply
    0
    • X xxxcucus

      @SGaist
      I used wireshark to view the websocket packages exchanged between the Spring Boot server and the Qt client. It seems that the server does not send a qwebsocket message to the subscribing Qt client.

      X Offline
      X Offline
      xxxcucus
      wrote on last edited by
      #9

      After using wireshark and a javascript stomp client to compare the websocket messages exchanged between the clients and the server I reached the conclusion that the Qt client did not send a CONNECT Stomp Frame to the server. That is why the server did not send a message corresponding to the subscription, although the client had previously sent a SUBSCRIBE package.

      Here is the code in the slot connect to the socketConnected slot of the QWebSocket:

       StompFrameCreator stompFrameCreator;
       auto connectFrame = stompFrameCreator.createConnectFrame("1.2", "", "", "", 10000, 10000);
       m_StompClient.sendFrame(connectFrame); 
      

      After adding that before sending the subscription to the server, the client receives messages pertaining to the subscription from the server.

      1 Reply Last reply
      1
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Hi,

        You should also add the setup code you use for the web socket.

        In the absolute, have a minimal compilable example triggering the issue would be even better.

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

        X 1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

          You should also add the setup code you use for the web socket.

          In the absolute, have a minimal compilable example triggering the issue would be even better.

          X Offline
          X Offline
          xxxcucus
          wrote on last edited by
          #3

          @SGaist
          This is the code used for the opening the QWebSocket:

          void StompDemoView::connectToServer() {
          qDebug() << "Connect to server";
          m_StompClient.setUrl("ws://localhost:8080/gs-guide-websocket");
          m_StompClient.connectToServer();
          }

          void StompClient::setUrl(const QString& url) {
          m_Url = url;
          }

          void StompClient::connectToServer() {
          if (m_Url.isEmpty())
          return;

          qDebug() << "Connect to " << m_Url;
          m_Socket.open(m_Url);
          

          }

          I could add an archive with the whole project but I am not sure how to do this on this forum.

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #4

            Did you also connect the error management related signals ?

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

            X 2 Replies Last reply
            0
            • SGaistS SGaist

              Did you also connect the error management related signals ?

              X Offline
              X Offline
              xxxcucus
              wrote on last edited by xxxcucus
              #5

              @SGaist
              I connected errorOccured. Are there other signals for error management ? I see there are some other error messages concerning handshake and authorization. I will connect those as well and see what I receive.

              1 Reply Last reply
              0
              • SGaistS SGaist

                Did you also connect the error management related signals ?

                X Offline
                X Offline
                xxxcucus
                wrote on last edited by
                #6

                @SGaist
                I connected the following signals to slots in my class:
                errorOccurred, alertReceived, authenticationRequired, handshakeInterruptedOnError, peerVerifyError, proxyAuthenticationRequired,sslErrors, stateChanged,

                Except for the obvious state changes when connecting and closing the connection and the connect and disconnect signals I am not receiving anything.

                For me it seems that the server (running on localhost as well) is sending the messages but somehow they do not arrive to the socket. Is there a way how I can debug this ?

                SGaistS 1 Reply Last reply
                0
                • X xxxcucus

                  @SGaist
                  I connected the following signals to slots in my class:
                  errorOccurred, alertReceived, authenticationRequired, handshakeInterruptedOnError, peerVerifyError, proxyAuthenticationRequired,sslErrors, stateChanged,

                  Except for the obvious state changes when connecting and closing the connection and the connect and disconnect signals I am not receiving anything.

                  For me it seems that the server (running on localhost as well) is sending the messages but somehow they do not arrive to the socket. Is there a way how I can debug this ?

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

                  The next step would be to use Wireshark to check what happens between your server and client.

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

                  X 1 Reply Last reply
                  0
                  • SGaistS SGaist

                    The next step would be to use Wireshark to check what happens between your server and client.

                    X Offline
                    X Offline
                    xxxcucus
                    wrote on last edited by
                    #8

                    @SGaist
                    I used wireshark to view the websocket packages exchanged between the Spring Boot server and the Qt client. It seems that the server does not send a qwebsocket message to the subscribing Qt client.

                    X 1 Reply Last reply
                    0
                    • X xxxcucus

                      @SGaist
                      I used wireshark to view the websocket packages exchanged between the Spring Boot server and the Qt client. It seems that the server does not send a qwebsocket message to the subscribing Qt client.

                      X Offline
                      X Offline
                      xxxcucus
                      wrote on last edited by
                      #9

                      After using wireshark and a javascript stomp client to compare the websocket messages exchanged between the clients and the server I reached the conclusion that the Qt client did not send a CONNECT Stomp Frame to the server. That is why the server did not send a message corresponding to the subscription, although the client had previously sent a SUBSCRIBE package.

                      Here is the code in the slot connect to the socketConnected slot of the QWebSocket:

                       StompFrameCreator stompFrameCreator;
                       auto connectFrame = stompFrameCreator.createConnectFrame("1.2", "", "", "", 10000, 10000);
                       m_StompClient.sendFrame(connectFrame); 
                      

                      After adding that before sending the subscription to the server, the client receives messages pertaining to the subscription from the server.

                      1 Reply Last reply
                      1
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by SGaist
                        #10

                        Nice !

                        Glad you found out and thanks for sharing :-)

                        Don't forget to mark the thread as solved :-)

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

                        1 Reply Last reply
                        1
                        • X xxxcucus has marked this topic as solved on

                        • Login

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