简体   繁体   English

socket.io在回调中获取套接字

[英]socket.io get socket inside a callback

I'm using socket.io for node.js. 我正在使用socket.io作为node.js. If you add an event handler like this: 如果你添加这样的事件处理程序:

io = require('socket.io')(http);
io.on('connect',function(socket){
    socket.on('some event',function(arg1,arg2){
       /// using socket to emit events for example
    }
}

Then I can access socket inside the callback for the 'some event' However what if I use it like this 然后我可以在'some event'的回调中访问socket但是如果我像这样使用它

io = require('socket.io')(http);
io.on('connect',function){
    socket.on('some event',myfunction);
}

function myFunction(arg1,arg2)
{
    //I want to use calling socket here.
}

How do I access socket in the later case? 在后面的例子中如何访问套接字? I need the socket to get the socket.id so I can know who called this event. 我需要socket来获取socket.id所以我可以知道谁调用了这个事件。 Thanks 谢谢

Well, if I understand what you want to do, you can simply: 好吧,如果我明白你想做什么,你可以简单地说:

io = require('socket.io')(http);
io.on('connect',function){
    socket.on('some event',myfunction);
}

function myFunction(arg1,arg2)
{
    var socketId = this.id; //here you go, you can refer the socket with "this"
}

This is what I usually do to keep the code clean: 这是我通常做的保持代码清洁的方法:

var on_potato = function(potatoData){

     var socket = this;
     var id = socket.id;

     //use potatoData and socket here
};

var on_tomato = function(tomatoData){

     var socket = this;
     var id = socket.id;

     //use tomatoData and socket here
};

var handleClientConnection = function (client) {

    client.on('potato', on_potato);
    client.on('tomato', on_tomato);
};

io.on('connection', handleClientConnection)

Alright, so after discussing this a potential solution is to just invoke your named function from within the anoymous callback function passed to the on method. 好吧,所以在讨论之后,一个潜在的解决方案就是从传递给on方法的anoymous回调函数中调用你的命名函数。

io.on('connect', function(socket){

 socket.on('someEvent', function(username, date){

    // If you emitted an object, you'll need to parse the incoming data. So say
    // You emitted {username: 'SomeName', date: 'SomeDate' }
    // You could just pass data.username and data.date directly
    // or put them into local variables, like:
    //var username = data.username, date = data.date;

    // Invoke your named function here and you can pass 
    // whatever you want to it, along with the socket
    myFunction(username, date, socket)
  })
})

myFunction(username, date, socket){
 // Do whatever you're doing with the passed paramaters
}

I've used Lodash's partial function quite often to solve problems like this ( Underscore has one as well, which does the same thing). 我经常使用Lodash的部分功能来解决这样的问题( Underscore也有一个,它做同样的事情)。 Basically what it does is create a new function that has some of the original function's arguments filled in. So what you would do is something like this: 基本上它所做的是创建一个新函数,其中填充了一些原始函数的参数。所以你要做的是这样的:

io = require('socket.io')(http);
io.on('connect', function(socket) {
  socket.on('some event', _.partial(myfunction, socket));
});

function myFunction(socket, ...args) {
  // whatever you wanna do
}

Then, when the new curried function is returned from the partial execution, it has socket prefilled as the first param and you can use it how you'd like. 然后,当从部分执行返回新的curried函数时,它将socket预填充为第一个参数,您可以按照自己的喜好使用它。

Just a note that the ...args is just a placeholder for whatever you want to put there. 只是注意...args只是一个占位符,无论你想放在那里。 Also, I'm not sure if socket.io passes anything into the function on firing the callback, which may affect the placement of the arguments into the curried function. 另外,我不确定socket.io是否在触发回调时将任何内容传递给函数,这可能会影响参数在curried函数中的位置。 If socket, shouldn't be the first argument, you can make it the second thusly: 如果socket不应该是第一个参数,那么你可以将它作为第二个参数:

io = require('socket.io')(http);
io.on('connect', function(socket)){
  socket.on('some event', _.partial(myfunction, _, socket));
}

function myFunction(arg1, socket, ...args) {
  // whatever you wanna do
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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