在尝试以二进制编码发射图像时,不会发射整个图像。我只收到部分图像和客户端。另外,在接收到的图像的某些部分,我可以看到白线。
服务器端NodeJs代码
let readStream = fs.createReadStream('images/logo.png'),{encoding:'binary'});
let chunks = []
readStream.on('readable', function () {
console.log('Image loading');
});
readStream.on('data', function (chunk) {
chunks.push(chunk);
request.socket.emit("response",chunks)
});
readStream.on('end', function () {
console.log('Image loaded');
});
readStream.on('error',function () {
console.log("ERROR")
})
客户端HTML代码
<body>
<img id="img-stream" src="">
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io();
socket.on('connect', () => {
console.log('Connected to server2');
socket.on('response',(data)=>{
var img = document.getElementById("img-stream");
img.setAttribute('src','data:image/jpeg;base64,'+ window.btoa(data))
})
});
socket.on('disconnect', () => {
console.log('Disconnected from server');
});
</script>
</body>