0

have built a websocket server in C and can send/receive text ok, have changed the opcode to binary (130) and am trying to send a small image (574 bytes). client side binary type is set to "blob".

ws.binaryType = "blob";

javascript client;

function onMessage(evt) {
  if(evt.data instanceof Blob){         
    console.log(evt.data.size);//574 bytes
    var reader = new FileReader();
    var img = document.querySelector("#img_test");

    reader.onload = function(e) {
      img.src = reader.result;
      console.log(img.src);
    }

    reader.readAsDataURL(evt.data);
  }
  else{
    $("#txt_window").append(evt.data+'<br/>');  
  }     
}

the output from the file reader is; (data:;base64,/9j/4AAAAAA etc... etc... lots of A's).

the size of evt.data matches the file sent from the server (574 bytes) but reader.result doesn't appear to have any content (hence the A's). does anyone know what i am doing wrong?

robr
  • 1
  • 2
  • my linux box appears to be little-endian, i'm wondering if i need to convert binary to big-endian, then send? – robr Feb 06 '16 at 10:55
  • You should check your net tab in dev tools to see if the correct data is being sent, it looks like the first 4 bytes are correct and the rest are just nulls. – Musa Feb 06 '16 at 13:37
  • cheers @musa, you are on the money! i was using strlen to determine the ssl_write frame length (4 bytes is the data_index, which sparked some thoughts) which works ok for null terminated C strings, not so good on binary :) – robr Feb 06 '16 at 16:25

0 Answers0