[英]What objects can be emitted in socket.io
Using the socket.io
library, what objects can I transmit to the client using the socket.emit
method on the server side? 使用
socket.io
库,我可以使用服务器端的socket.emit
方法将哪些对象传输到客户端? For example, the socket.io
site provides an example like the following 例如,
socket.io
站点提供了如下示例
socket.emit('news', { hello: 'world' });
However, I couldn't find any documentation on the following questions when using the emit
method: 但是,在使用
emit
方法时,我找不到有关以下问题的任何文档:
emit
to transmit "any" JavaScript object, say one with methods, complex properties, etc? emit
来传输“任何”JavaScript对象,比如说有方法,复杂属性等吗? socket.io
use JSON conversion when it actually does the transfer to the clients. socket.io
在实际传输到客户端时是否使用JSON转换。 From their documentation : 从他们的文件 :
All datastructures are supported, including Buffer.
支持所有数据结构,包括Buffer。 JavaScript functions can't be serialized/deserialized.
JavaScript函数无法序列化/反序列化。
This means that you can emit a lot including JavaScript object literals. 这意味着您可以发出很多,包括JavaScript对象文字。 The only things you can't emit are functions because they are not serializable, so you will not be able to emit objects containing methods and have those methods be usable by the recipient.
您无法发出的唯一内容是函数,因为它们不可序列化,因此您将无法发出包含方法的对象,并且这些方法可供收件人使用。 Nested properties would be allowed, though.
但是,允许嵌套属性。
You don't have to convert the data ahead of time with emit
, but you would with send
. 您不必使用
emit
提前转换数据,但您可以使用send
。 It stands to reason that socket.io
does some conversion when it transfers the data to the client. 按理说,
socket.io
在将数据传输到客户端时会进行一些转换。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.