Reputation: 13987
Im making a HTML 5 chess game. I represent the board with an unsigned 8bit binary array with a length (size) of 64. As there are 64 positions on a chess bord.
var board = Uint8Array(64);
each chess piece is represented on the board via an (obviously) 8 bit unsigned integer. Then on clients there is a map relating the binary representation of a piece to a textual string e.g:
var pieceMap = { 01: 'BlackKnight', 112: 'WhitePawn', 6: 'BlackRook' ... }
The data is sent over a socket.io connection (lets assume we are using websockets), TCP i think? So the Question is: Will TCP send my packets as small as the data i feed to it (with TCP overheads) or does TCP have a min packet size and send say my 200byte packet as a 1024byte packet?
why so obsessed with size? i know it does not matter but i use this for the learning experiance, in future games i will use this to lower latency... i know chess does not need to be low latency.
Upvotes: 5
Views: 2300
Reputation: 6273
Packets as framed, as per indications of the RFC:
https://www.rfc-editor.org/rfc/rfc6455#section-5
The overhead is of up to 10 bytes per message or less PER message fragment. How many bytes are sent per fragment are determined by the payload length (part of the framing). Fragments can be pretty big, though. Unless you're sending very large messages, I don't think it will be an issue.
According to the way framing and fragmentation works, you shouldn't be sending/receiving 1024 bytes of data if the framing + data are just at around 100 bytes.
My guess is that some implementations might do it with minor differences, but you shouldn't concern about a detail like that too much unless you start seeing stress problems withy our application.
Another detail: if you really wanted to go for very low latency, you would have to start looking into things like using UDP or multicasting. Problem is: websockets does not support these. Reducing the packet sizes is not the only important thing for networking, but only a part of it... and from my experience with socket.io, the CPU becomes the bottleneck with small messages before bandwidth, and RAM might take a big hit too before bandwidth as well when messages are in the low kilobites. Of course, this depends entirely on your application and I'm just talking about empirical experiences.
Upvotes: 5