socket.io socket.set 和 socket.get - 回调参数是什么? [英] socket.io socket.set and socket.get - what is the callback argument for?

查看:65
本文介绍了socket.io socket.set 和 socket.get - 回调参数是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想将一些变量附加到我的套接字,但我不理解 .get 和 .set 方法.它们都有用于回调的第三个参数.这个可以省略吗?这是为了什么?!回调本身是否继承参数?

I just want to attach some variables to my sockets, but I don't understand the .get and .set methods. They both have a third argument for a callback. Can this be omitted? What is it for?! Does the callback itself intherit arguments?

我最好的猜测是,当 socket.io 不使用内存存储但将这些东西保存在其他地方并且需要错误检查回调时,它就在那里.

My best guess is that it's there for when socket.io isn't using memory store but is saving this stuff somewhere else and needs an error checking callback.

socket.on('set nickname', function (name) {
    socket.set('nickname', name, function (arguments) {
      doSomethingMysterious();
    });

我使用的是 socket.io 0.9.x

I'm using socket.io 0.9.x

函数在socket.js中定义

The function is defined in socket.js

/**
 * Stores data for the client.
 *
 * @api public
 */

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

推荐答案

setget 是异步的.当这些操作完成时将调用回调.你可以省略它,或者你可以将它用于任何你喜欢的目的,而不仅仅是错误处理.

set and get are asynchronous. The callback will be called when these operations are completed. You can omit it, or you can use it for whatever purpose you like, not just error handling.

这篇关于socket.io socket.set 和 socket.get - 回调参数是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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