How to send binary data from a Node.js socket.io server to a browser client?

前端 未结 2 2005
攒了一身酷
攒了一身酷 2020-12-14 18:30

I\'ve been looking through the entire Socket.IO docs, but, even though they promise it is there, I can\'t find a simple, minimal example, of how one would send binary data b

相关标签:
2条回答
  • 2020-12-14 19:19

    Starting from socket.io 1.0 it is possible to send binary data. http://socket.io/blog/introducing-socket-io-1-0/

    How ever the way of sending and receiving binary data is not clear in the official documentation. The only documentation is:

    var socket = new WebSocket('ws://localhost');
    socket.binaryType = 'arraybuffer';
    socket.send(new ArrayBuffer);
    

    I suggest you to take a look at this answer, where you can find basic example with code implementation for server and client (javascript and java too):

    How to send binary data with socket.io?

    The good part is that it also works on Android! (if you wish)

    Cheers

    0 讨论(0)
  • 2020-12-14 19:23

    It is in fact in the documentation. The current documentation for Socket.io says under Socket.emit:

    [...] Emits an event to the socket identified by the string name. Any other parameters can be included. All datastructures are supported, including Buffer [...]

    So, if you can send a buffer, you can send binary data. All you have to do is to pack your data into a Buffer object.

    You may want to read Socket.io Binary Support and Sending and Receiving Binary

    0 讨论(0)
提交回复
热议问题