I want to share my trick that may be useful for someone.
My java server sends for all clients positions of players with long timestamp from System.currentTimeMillis()
via binary websocket message. On client side to parse this message I am using DataView
.
But DataView dont have method getInt64()
. And my solution was to send long in two pieces.
On server side:
long time = System.currentTimeMillis();
int firstPiece = (int)(time / 1000);
short secondPiece = (short)(time % 1000);
On client side:
let data = {};
let view = new DataView(binary);
data.t = view.getInt32(offset) * 1000 + view.getInt16(offset + 4);
And that also saves 2 bytes (long 8 bytes > int 4 bytes + short 2 bytes), but it will work until 2038 year https://en.wikipedia.org/wiki/Year_2038_problem.