2

I am trying to send a image (.png) as a binary blob to a javascript client:

  static Byte[] imageResponse(string pngPath)
  {
        Byte[] image = System.IO.File.ReadAllBytes(pngPath);
        ulong n = (ulong) image.Length;
        byte[] data = new byte[n + 10];
        data[0] = 130; // binary ?
        data[1] = 127;
        Byte[] na = BitConverter.GetBytes(n); // correct endinaness?
        Array.Copy(na, 0, data, 2, 8);
        Array.Copy(image, 0, data, 10, (int) n);
        return data;
  }

 string path = "C:/tmp/crl3400.png";
 Byte[] bresponse = imageResponse(path);
 stream.Write(bresponse, 0, bresponse.Length);

However in the client I am not receiving any message:

var socket = new WebSocket( "ws://127.0.0.1:80" );
socket.binaryType = "arraybuffer"; // allow both text and binary data to be transfered.
socket.onopen = function ( event ) {
    alert( "WebSocket opened!" );
    socket.send( "Hello server!" );
};
socket.onmessage = function ( event ) {
    alert( "onmessage" );
    var arrayBufferView = new Uint16Array( event.data );
    var blob = new Blob( [arrayBufferView], { type: "image/png" } );
    var urlCreator = window.URL || window.webkitURL;
    var imageUrl = urlCreator.createObjectURL( blob );
    var img = document.querySelector( "#image" );
    img.src = imageUrl;
};
socket.onerror = function ( event ) {
    alert( "Error" );
}

What could be wrong? Things worked when I sent the text "Hello client!". I am not getting an Error alert. On the server I am receiving "Hello server!" fine.

1
  • Should it be Uint8Array and not Uint16? Commented Nov 18, 2015 at 15:17

1 Answer 1

1

enter image description here

The msg.data is type of Blob, so you just use urlCreator.createObjectURL(blob); method bla bla bla....

Sign up to request clarification or add additional context in comments.

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.