Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How send arraybuffer as binary via Websocket?

I am working on a project with Mozilla Europe. In this project, I use websocket by Worlize (server-side) and Mozilla (client side), Node.js to try to upload files from a client to a server.
My present goal is to send a arraybuffer of the file to the server. Create the arraybuffer and send it is fine.
But my server tells me that arraybuffer is a utf8 message and not a binary message.

Do I misunderstand something? If not, how can i correct that?

Client side:

    reader = new FileReader();     reader.readAsArrayBuffer(file);     reader.onload = function(e) {         connection.send(e.target.result);      }; 

Server side:

ws.on('message', function(message,flags) { if (!flags.binary) {     //some code } else {     console.log('It\'s a binary'); } 

I try with Blob too, same result. The binary part is invisible.

like image 944
Chris Avatar asked Mar 03 '12 13:03

Chris


People also ask

How do I send binary data over WebSocket?

WebSockets support sending binary messages, too. To send binary data, one can use either Blob or ArrayBuffer object. Instead of calling the send method with string, you can simply pass an ArrayBuffer or a Blob .

Can WebSockets contain binary data?

WebSocket is the only transport that allows bidirectional communication over the same TCP connection (Figure 17-2): the client and server can exchange messages at will. As a result, WebSocket provides low latency delivery of text and binary application data in both directions.

Can we send file through WebSocket?

First, APIs are needed to load the file from the file system, then the file is transformed into a suitable payload to be sent over web sockets, and finally, server-side code is required to receive the file.

How do I send a WebSocket message?

To send a message through the WebSocket connection you call the send() method on your WebSocket instance; passing in the data you want to transfer. socket. send(data); You can send both text and binary data through a WebSocket.


2 Answers

Gecko11.0 ArrayBuffer send and receive support for binary data has been implemented.

connection = new WebSocket( 'ws://localhost:1740' ); connection.binaryType = "arraybuffer"; connection.onopen = onopen; connection.onmessage = onmessage; connection.onclose = onclose; connection.onerror = onerror; 

sending Binary data:

function sendphoto() {     imagedata = context.getImageData( 0, 0, imagewidth, imageheight );     var canvaspixelarray = imagedata.data;     var canvaspixellen = canvaspixelarray.length;     var bytearray = new Uint8Array( canvaspixellen );     for ( var i = 0; i < canvaspixellen; ++i ) {         bytearray[i] = canvaspixelarray[i];     }     connection.send( bytearray.buffer );     context.fillStyle = '#ffffff';     context.fillRect( 0, 0, imagewidth, imageheight ); } 

Recieving Binary Data:

if ( event.data instanceof ArrayBuffer ) {     var bytearray = new Uint8Array( event.data );     var tempcanvas = document.createElement( 'canvas' );     tempcanvas.height = imageheight;     tempcanvas.width = imagewidth;     var tempcontext = tempcanvas.getContext( '2d' );     var imgdata = tempcontext.getImageData( 0, 0, imagewidth, imageheight );     var imgdatalen = imgdata.data.length;     for ( var i = 8; i < imgdatalen; i++ ) {         imgdata.data[i] = bytearray[i];     }     tempcontext.putImageData( imgdata, 0, 0 );     var img = document.createElement( 'img' );     img.height = imageheight;     img.width = imagewidth;     img.src = tempcanvas.toDataURL();     chatdiv.appendChild( img );     chatdiv.innerHTML = chatdiv.innerHTML + "<br />"; } 
like image 121
kongaraju Avatar answered Sep 23 '22 12:09

kongaraju


Note: Prior to version 11, Firefox only supported sending data as a string. 

Source: https://developer.mozilla.org/en/WebSockets/Writing_WebSocket_client_applications

like image 31
Dennis Avatar answered Sep 25 '22 12:09

Dennis