最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - socket.io get socket inside a callback - Stack Overflow

programmeradmin5浏览0评论

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

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

Share Improve this question edited Jun 17, 2015 at 22:05 Michael P asked Jun 17, 2015 at 22:01 Michael PMichael P 2,0873 gold badges29 silver badges34 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

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 ining 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
}
发布评论

评论列表(0)

  1. 暂无评论