Communicate with the front and back end of websocket, and the back end sends 64-bit binary to the front end. How does the front end change the binary to the long type defined in the back-end service?

1. The back-end converts a long data into 64-bit binary, which is transmitted to the front-end through websocket. The front-end receives an ArrayBuffer object, reads the data in memory with getFloat64, and converts it into an integer, but it is found to be inconsistent with the data defined by the back-end.

2, for example, the value passed by the back end is 20000000001, and the ArrayBuffer, received by the front end is taken out with Int8Array to get an 8-bit signed integer [0,0,0,46,-112,-19,-48,1], and the value obtained is consistent with the binary provided by the back end.

3. Data conversion is as follows:
clipboard.png

4ArrayBufferInt8ArrayFloat64Array:

clipboard.png

Feb.28,2021

function DecodeFloat64Array (Float64) {

var dv = new DataView(Float64);
var newBuffer = Float64.slice(0, 8);
var udata = Array.from(new Int8Array(dv.buffer, dv.byteOffset, dv.byteLength));
var ans = 0;
for (var i = 0;i < 8;iPP){
    if (udata[i] < 0) udata[i] += 256;
    ans = ans * 256 + udata[i];
}
return ans;

}


currently blogs related to the binary transmission of WebSocket are still being sorted out and are expected to be published this week. Should be able to solve your problem ~
March 28th 2018 update: how to convert digital data into binary data in JavaScript of WebSocket series
https://codeshelper.com/a/11. you can see if this can solve your puzzles. if you have any more problems, just leave me a message. ~

Menu