Socket.io began to support binary stream from 1.0, is there a complete example especially for image

后端 未结 3 806
伪装坚强ぢ
伪装坚强ぢ 2020-12-13 05:03

I\'m a beginner on node.js and socket.io. Socket.io began to support binary stream from 1.0, is there a complete example especially for image push to client and show in canv

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-13 05:52

    thanks, @sovente, in this 1.0 introduction http://socket.io/blog/introducing-socket-io-1-0/ , this is code snippet on binary support.

    var fs = require('fs');
    var io = require('socket.io')(3000);
    io.on('connection', function(socket){
      fs.readFile('image.png', function(err, buf){
        // it's possible to embed binary data
        // within arbitrarily-complex objects
        socket.emit('image', { image: true, buffer: buf });
      });
    });
    

    i want to know how to handle the buffer on client side, codes are like:

     socket.on("image", function(image, buffer) {
         if(image)
         {
             console.log(" image: ");
             **// code to handle buffer like drawing with canvas**
         }
    
     });
    

提交回复
热议问题