简体   繁体   English

socket.io中可以发出什么对象

[英]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方法时,我找不到有关以下问题的任何文档:

  • Could I used emit to transmit "any" JavaScript object, say one with methods, complex properties, etc? 我可以使用emit来传输“任何”JavaScript对象,比如说有方法,复杂属性等吗?
  • Should I instead convert my data/object to JSON and then transmit the JSON string. 我应该将我的数据/对象转换为JSON,然后传输JSON字符串。 This would of course put some limitations on what objects could be transferred to the client. 这当然会对可以传输到客户端的对象施加一些限制。
  • Does 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM