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. QWebSocketServer stop working after first message
QtWS25 Last Chance

QWebSocketServer stop working after first message

Scheduled Pinned Locked Moved General and Desktop
qwebsocketserveqwebsocket
2 Posts 1 Posters 1.3k 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.
  • A Offline
    A Offline
    ageblade
    wrote on last edited by ageblade
    #1

    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

    1 Reply Last reply
    0
    • A Offline
      A Offline
      ageblade
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      1

      • Login

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