Qt6 QWebSocket - how to send message to a different endpoint?
-
I am trying to implement a client application for a third-party WebSockets server which uses the STOMP protocol. I got to the point where I get a "CONNECTED" (i.e., successful) response from the server. Now I need to start sending messages, starting from the custom authentication frame/message, and then the normal payload.
The problem is: the QWebSocket's current URL is
wss://thirdpartydomain.com/cache/stream/connect
but I need to send the next message to a different endpoint (on the same host and port):
wss://thirdpartydomain.com/cache/stream/message
How can I redirect the QWebSocket to a different URL path?
I tried the following, but the URL does not seem to change:
QString m_stream_base_url = "wss://thirdpartydomain.com/cache/stream"; ... QString connect_url = m_stream_base_url + "/connect"; ... *[here, the socket is opened and successfully negotiates the CONNECT flow]* ... QString message_url = m_stream_base_url + "/message"; QNetworkRequest auth_request(QUrl(message_url)); m_webSocket.request() = auth_request; qDebug() << "Updated web socket:" << m_webSocket.request().url();
How can I change the target endpoint of an already-open QWebSocket?
-
Hi,
To the best of my knowledge, you can't.
It seems you would either need two sockets or to open a new connection once the original exchange is done.
-
@D_V_G
Since the request() is const, I also - just in case - tried to do the following, which also does not work for the same reason:QUrl message_endpoint_url = QUrl(m_stream_base_url + "/message"); m_webSocket.request().setUrl(message_endpoint_url);
I dug into the Qt 6.6.1 code for QWebSocket and QWebSocketPrivate classes
It looks like there is no way to change the URL path dynamically, short of modifying the source code or creating custom classes.
Am I missing a better solution? -
Hi,
To the best of my knowledge, you can't.
It seems you would either need two sockets or to open a new connection once the original exchange is done.
-