如何从Node.js socket.io服务器向浏览器客户端发送二进制数据?

时间:2022-11-13 09:53:16

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 between server/client.

我一直在查看整个Socket.IO文档,但是,即使他们保证它存在,我也找不到一个简单的,最简单的例子,说明如何在服务器/客户端之间发送二进制数据。

How is it done?

怎么做?

2 个解决方案

#1


10  

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

它实际上在文档中。 Socket.io的当前文档在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 [...]

[...]将事件发送到由字符串名称标识的套接字。可以包括任何其他参数。支持所有数据结构,包括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.

因此,如果您可以发送缓冲区,则可以发送二进制数据。您所要做的就是将数据打包到Buffer对象中。

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

您可能希望阅读Socket.io二进制支持和发送和接收二进制文件

#2


2  

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

从socket.io 1.0开始,可以发送二进制数据。 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):

我建议你看看这个答案,在那里你可以找到服务器和客户端代码实现的基本例子(javascript和java):

How to send binary data with socket.io?

如何使用socket.io发送二进制数据?

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

好的一面是它也适用于Android! (如果你希望)

Cheers

干杯

#1


10  

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

它实际上在文档中。 Socket.io的当前文档在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 [...]

[...]将事件发送到由字符串名称标识的套接字。可以包括任何其他参数。支持所有数据结构,包括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.

因此,如果您可以发送缓冲区,则可以发送二进制数据。您所要做的就是将数据打包到Buffer对象中。

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

您可能希望阅读Socket.io二进制支持和发送和接收二进制文件

#2


2  

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

从socket.io 1.0开始,可以发送二进制数据。 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):

我建议你看看这个答案,在那里你可以找到服务器和客户端代码实现的基本例子(javascript和java):

How to send binary data with socket.io?

如何使用socket.io发送二进制数据?

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

好的一面是它也适用于Android! (如果你希望)

Cheers

干杯