Socket.io,socket.set()和socket属性之间的区别? [英] Socket.io, difference between socket.set() and socket property?

查看:222
本文介绍了Socket.io,socket.set()和socket属性之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Socket.io建议设置每插槽变量,如下所示:

Socket.io recommends settings per-socket variables like so:

socket.set('foo',bar,function(){ });

也可以在套接字上设置和访问变量:

Variables can also be set and accessed on the socket:

socket.foo = bar

使用提供的set()函数有什么好处?

Is there a benefit to using the provided set() function?

推荐答案

调用 socket.foo 在套接字对象上设置属性。建议不要这样做,因为您可能会覆盖套接字使用和依赖的内部属性。当你调用 socket.set()时,它存储在一个不会与内部属性冲突的内部数据结构中。

Calling socket.foo sets your property on the socket object itself. This isn't recommended because you could be overriding an internal property that socket uses and depends upon. When you call socket.set() this is stored in an internal data structure that won't clash with internal properties.

https://github.com/LearnBoost/socket.io/blob /master/lib/socket.js#L246

Socket.prototype.set = function (key, value, fn) {
  this.store.set(key, value, fn);
  return this;
};

这篇关于Socket.io,socket.set()和socket属性之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆