Note that there are some explanatory texts on larger screens.

plurals
  1. POJavascript: Send Array of ints through node.js binary websockets
    text
    copied!<p>I have the following case. I capture a webcam frame via a canvas with <code>context.getImageData().data</code>. That gives me a nice Array (or UInt8ClampedArray says Bidelman) of ints like <code>[r, g, b, a, r, g, b, a, r, g, b, a, etc.]</code> when I <code>console.log()</code> them. Now I want to send this to a node.js server via binaryjs and of course the data get's scrambled like hell: <a href="https://gist.github.com/thgie/6550638" rel="nofollow">https://gist.github.com/thgie/6550638</a></p> <p>How can I convert the Array to something binary first or how do I get the binary back to a readable array?</p> <p><strong>Code examples</strong></p> <p>Client:</p> <pre><code>client.send(ctx.getImageData(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT).data, {'id': 'bin', 'frame': frame}); </code></pre> <p>Server:</p> <pre><code>client.on('stream', function(stream, meta){ var bufs = []; stream.on('data', function(data){ bufs.push(data); }); stream.on('end', function(){ var buf = Buffer.concat(bufs); fs.writeFile(__dirname + "/tmp/"+meta.frame+".dat", buf, "binary", function(err) { if(err) { console.log(err); } else { console.log("The file was saved!"); } }); }); } </code></pre> <p>It will look like this:</p> <pre><code>H&gt;DÿNDJÿL@FÿPCJÿR@KÿQ?IÿLDPÿKCOÿMCKÿLAIÿL@DÿL@DÿLBGÿKAFÿIAHÿLDLÿLAIÿLAIÿO@IÿO@IÿIAJÿJBKÿMCIÿNDJÿQEIÿQEIÿPGHÿOFGÿMIHÿMIHÿPDFÿNCEÿIBDÿLEHÿPDFÿPDFÿMHLÿLGKÿMHLÿNIMÿOGLÿNFKÿOEMÿPFNÿRFSÿPERÿPERÿPERÿRGOÿSHPÿSGMÿSGMÿRJRÿQIPÿQJNÿRKPÿRKPÿQJNÿSGMÿSGMÿOHJÿSLOÿQJLÿRKMÿSIJÿTKKÿQLPÿPJNÿSLQÿRKPÿSLQÿUMRÿSKSÿUMTÿVMWÿSKUÿUJUÿRGQÿUMTÿUMTÿSKUÿVMWÿSMTÿSMTÿQNVÿQNVÿSMVÿTNWÿZMVÿXKTÿUJUÿVKVÿTOSÿTOSÿWQUÿWQUÿYTVÿYTVÿXRVÿUPTÿYSZÿYSZÿXRVÿZUYÿWPRÿYRTÿWRQÿXSRÿVOOÿWPPÿXRVÿXRVÿZOYÿ[P[ÿZO[ÿ\Q^ÿZT_ÿZT_ÿZV`ÿZV`ÿ]T^ÿ]T^ÿ^TZÿ`V[ÿ[VXÿ[VXÿ]UZÿ^V[ÿZV`ÿ[Waÿ_Xdÿ_Xdÿ]X`ÿ]X`ÿaZ_ÿaZ_ÿ_Y]ÿ`[_ÿ`Y] </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload