简体   繁体   中英

socket.io get socket inside a callback

I'm using socket.io for 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

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. 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.

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). 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.

Just a note that the ...args is just a placeholder for whatever you want to put there. 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. If socket, shouldn't be the first argument, you can make it the second thusly:

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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