简体   繁体   中英

Is the HTML5 WebSockets protocol lossless?

Considering it uses TCP/IP, I'm guessing it is, but I'm not well enough versed in the spec (or the reading thereof) to know for sure. As an aside, does it also ensure they are delivered and read in the correct order?

The question derives from a need in an HTML5 games library I'm writing, but I suppose that's beside the point.

Since WebSockets are based on TCP, the messages you send are guaranteed to be received (or an error will be raised).

Messages sent in sequence through the same (websocket) connection by the same client are received in order (think of it as writing everything to a file and reading the same afterwards), but there is no guarantee about the order in which messages will come from different connections established by the same client or connections established by different clients , even if you knew the order in which they were sent.

The usual approach in the latter scenario is embedding a timestamp in each message, so that you can reconstruct the correct temporal sequence on the server.

As an aside, if your server processes requests using threads, then two messages could be processed out of order even if received in order (but this is another story).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM