A
A
Adamn2020-05-18 16:13:27
JavaScript
Adamn, 2020-05-18 16:13:27

How to send Binary Message via Websocket?

Through which command can you send information so that the browser receives the following?5ec289e1515bc621085478.jpeg

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
myxomor469, 2020-05-18
@myxomor469

it turns out that we first convert String to Bin with this function:

function str2ab(str) {
  var buf = new ArrayBuffer(str.length*2); // 2 bytes for each char
  var bufView = new Uint16Array(buf);
  for (var i=0, strLen=str.length; i < strLen; i++) {
    bufView[i] = str.charCodeAt(i);
  }
  return buf;
}

and then send via WebSocket, but you need to set the binary message type in the connection on the client
ws.binaryType = 'arraybuffer';

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question