QWebSocketServer stop working after first message
-
Hi everyone,
I am experiencing some weird behavior by QWebSocketServer.
If messages are sent from different threads they won't get to the client and the server is defected.Anyone knows how to solve it ?
I have used the example server as my reference and the following code is the send message method:
for (int i = 0; i < m_clients.length(); i++) { QWebSocket *pClient = m_clients.at(i); if (pClient->state() == QAbstractSocket::SocketState::ConnectedState) { qint64 bytesSent = pClient->sendTextMessage(message); } }
Thanks in advance,
Idan -
Ok issue is solved.
From my investigation it appears that the QWebSocketServer works properly only when communicating with the main thread.
Therefore, any messages sent by other threads will not be received at the other side and the socket server will be corrupted from that moment.In order to fix this issue I actually forced any other thread in my application to send their messages using the main thread.
I did it by using QMetaObject::invokeMethod() with Qt:QueuedConnection as the connection type.Hope this can help anyone in the future,
Idan