Display image from blob using javascript and websockets

后端 未结 6 1373
春和景丽
春和景丽 2020-11-29 22:05

I\'m currently working on a WebSocket application that is displaying images send by a C++ server. I\'ve seen a couple of topics around there but I can\'t seem to get rid of

6条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-29 22:38

    You may write it much simpler:

    socket.onmessage = function(msg) {
       var arrayBuffer = msg.data;
       var bytes = new Uint8Array(arrayBuffer);
       var blob = new Blob([bytes.buffer]);
    
       var image = document.getElementById('image');
    
       var reader = new FileReader();
       reader.onload = function(e) {
           image.src = e.target.result;
       };
       reader.readAsDataURL(blob);
    };
    

提交回复
热议问题