Socket.io, difference between socket.set() and socket property?

后端 未结 2 1591
天涯浪人
天涯浪人 2021-02-13 14:07

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

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

Variables can also be set and accessed on the

2条回答
  •  你的背包
    2021-02-13 15:07

    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;
    };
    

提交回复
热议问题