How to convert javascript array to binary data and back for websocket?
<p>I want send this example array</p>

<pre><code>[{
    id: 1,
    name: "test",
    position: [1234,850], //random position on the map
    points: 100 //example points
}];
</code></pre>

<p>to my websocket server as binary data. At server side I want decode that binary data back into array, make changes and send back binary data to client. And finally at client side how to decode binary data back into array?</p>

<p>Example screenshot what I mean:</p>

<p><a href="https://i.stack.imgur.com/1XbQE.png" rel="noreferrer"><img src="https://i.stack.imgur.com/1XbQE.png" alt="enter image description here"></a></p>

<p>This is my actual code:</p>

<pre><code>var connection = new WebSocket('wss://my_website.eu:1234');
connection.binaryType = "ArrayBuffer";
connection.onmessage = function (event) {

    // console.log(event);
    if (event.data instanceof window["ArrayBuffer"]) {
        var data3 = JSON.parse(String.fromCharCode.apply(null, new Uint16Array(event.data)));
        console.log(data3);
    } else {
        console.log(event.data); // Blob {size: 0, type: ""}
    }
};

$("body").mousemove(function( event ) {

    var data = {
        name: "lol",
        pos: [event.pageX, event.pageY]
    };

    //convert to binary frame
    var data2 = new Uint16Array(data);
    console.log(data2); // []

    //try to convert back to array
    var data3 = String.fromCharCode.apply(null, new Uint16Array(data2));
    console.log(data3); // empty

    connection.send(data2); // Binary Frame (Opcode 2, mask) | length: 0

});
</code></pre>

<p>Server side code:</p>

<pre><code>connection.on('message', function(message) {
    for (var i = players.length - 1; i &gt;= 0; i--) {
        players[i].connection.send(message.binaryData);
    }
});
</code></pre>

<h2>LATEST EDIT READ FROM HERE</h2>

<p>I now can send message as binary frame to websocket server. <a href="https://stackoverflow.com/a/16365505/2893691">I found functions</a> to convert string to binary type and send it to ws server. </p>

<p>Now I have problem. This functions (below) not working at server-side. Example code:</p>

<pre><code>var data = {
    name: "value"
};

connection.send(JSON.stringify(data));
</code></pre>

<p>This code working good. Now, when I try to send as array buffer:</p>

<pre><code>var data = {
    name: "value"
};

connection.send(StringToArrayBuffer(JSON.stringify(data)));
</code></pre>

<p>output is not Binary frame, is just string "[object ArrayBuffer]":</p>

<p><a href="https://i.stack.imgur.com/bjP3m.png" rel="noreferrer"><img src="https://i.stack.imgur.com/bjP3m.png" alt="enter image description here"></a></p>

<p>I also tried:</p>

<pre><code>connection.send(JSON.stringify(data), {binary: true, mask: false});
</code></pre>

<p>but this sending message as normal string, not binary frame.</p>

<p>So, <strong>how I can send Binary frame from websocket server to client</strong>? When I send back received binary message:</p>

<pre><code>connection.on('message', function(message) {
    for (var i = players.length - 1; i &gt;= 0; i--) {
        playerConnection[i].send(message.binaryData);
    }
}
</code></pre>

<p>only this works.</p>