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?