QWebSocket vs QTcpSocket
-
Hello,
I wrote an application similar to a chat. On the server, I keep track of the connection opened with the clients by collecting the associated QTcpSocket.
Once a client sends a message, the server broadcasts it to all the other clients by retrieving the stored QTcpSocket.In Qt documentation, I found out about the possibility of using QWebSocket. What should be the advantage of using it over a general QTcpSocket?
Maybe it is more a general question other than Qt specific, but I wasn't able to find an answer online, with articles only writing about normal TCP connection being unidirectional through a request/response mechanism. But this doesn't seem true to me since I can just keep the connection open and then send a message from the server to the client whenever I want (without any need for a previous request or polling mechanism).
Is anyone able to help me? Thanks
-
They're simply two different protocols:
QTcpSocket: tcp/ip communication
QWebSocket: websocket communication -
QWebSocket is more high-level - you don't have to care about the packets and add an own layer to know when a telegram ends since it's already handled by the protocol.
-
Thank you, this makes sense.
When I was using QTcpSocket in fact I had to manually handle the distinction between the type of content (Json, QByteArray) when writing the slot for thereadyRead()
. And in the case of binary data, I was using a custom protocol to tell the number of bytes expected and continue reading (calling again theonReadyRead()
) until everything was received.Instead, looking at the methods provided for the QWebSocket class, it seems that there are separate signals for string and binary content and binary data can be dealt with all at once.
Am I right?Now that I look at it, probably mine was a poor design choice.