5

I've tried sending a Float32Array and an ArrayBuffer a few different ways but the buffers I receive on the client side always seem empty. How should I send the data? How can I access it?

Here's my latest attempt:

server.js

const WebSocket = require('ws');

const wss = new WebSocket.Server({ port: 8000 });

wss.on('connection', function connection(ws) {

    var arr1 = new Float32Array(4);
    for (var i = 0; i < arr1.length; ++i)
        arr1[i] = (i + 100) / 7;

//    ws.send(arr1.buffer);


    var arr2 = new ArrayBuffer(8);
    for(var i = 0; i < 8; i++)
        arr2[i] = i * 17;

    ws.send(arr2);    
});

client.html

<!DOCTYPE html>
<html>
<head></head>
<body>

<script>

var gData = null;

var ws = new WebSocket("ws://192.168.0.7:8000");
ws.binaryType = 'arraybuffer';

ws.onmessage = function(message) {
    console.log(message.data);
    gData = message.data;
};

</script>

</body>
</html>

Console Output

ArrayBuffer(8) {}

Also, just because I'm curious, how can I send binary data back to the server?

2
  • Jason, did you solved this problem? Commented Mar 24, 2021 at 15:12
  • @Derzu I can't remember exactly, but I think the answer from emax (below) helped me enough. Commented Mar 24, 2021 at 15:20

2 Answers 2

2

You need to create the WebSocket and set its type to arraybuffer (binaryType = 'arraybuffer'). I think you forgot that on the server-side.

In this example, the bytes are obtained from a base64 string info.

    // https://stackoverflow.com/questions/21797299/convert-base64-string-to-arraybuffer
    function base64ToArrayBuffer(base64) {
        var binary_string = Buffer.from(base64, 'base64').toString('binary');
        var len = binary_string.length;
        var bytes = new Uint8Array(len);
        for (var i = 0; i < len; i++) {
            bytes[i] = binary_string.charCodeAt(i);
        }    

        return bytes.buffer;
    }

    // websocket server
    const WebSocket = require("ws"); // websocket server
    const wss = new WebSocket.Server({ port: 8082 });
    console.log("WebSocket Server Started on port 8082");

    wss.binaryType = 'arraybuffer';
    const content_base64 = "c3RyZWFtIGV2ZW50"; // Place your base64 content here.
    const binaryData = base64ToArrayBuffer(content_base64);

    wss.on("connection", (ws) => {
        console.log("WebSocket sending msg");
        ws.send(binaryData);
    });

Don't forget to install the npm websocket module through the command:

$ npm install websocket
Sign up to request clarification or add additional context in comments.

Comments

1

ArrayBuffer it's just a buffer, in order to access the memory contained in a buffer, you need to use a view:

var arr2 = new ArrayBuffer(8);
var view = new Uint8Array(arr2);
for(var i = 0; i < 8; i++)
    view[i] = i * 17;
ws.send(arr2);  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.