简体   繁体   English

socket.io:如何从对象动态调用套接字方法?

[英]socket.io : How do I call upon my socket methods dynamically from an object?

I have this: 我有这个:

io.sockets.on('connection', function(socket) {
    socket.on("action1", function(data){
        socket.emit("action1", data);
    });
    socket.on("action2", function(data){
        socket.emit("action2", data);
    });
    socket.on("action3", function(data){
        socket.emit("action3", data);
    });
});

I want this: 我要这个:

var socketMethods = {
    action1:function(data, socket){
        socket.emit("action1", data);
    },
    action2:function(data, socket){
        socket.emit("action2", data);
    },
    action3:function(data, socket){
        socket.emit("action3", data);
    }
}

io.sockets.on('connection', function(socket) {
    for(key in socketMethods){
        socket.on(key, socketMethods[key]);
    }
});

However, I can't pass the socket into the method and I end up with 但是,我无法将套接字传递给方法,最终我得到了

C:\__server\nodejs.js:217
    socket.emit('action1', {msg:"No account"}
               ^
TypeError: Cannot call method 'emit' of undefined

I have tried passing the socket through the function : 我试图通过函数传递套接字:

io.sockets.on('connection', function(socket) {
    for(key in socketMethods){
        socket.on(key, function(data, socket){
            socketMethods[key](data, socket);
        });
    }
});

The socket doesn't get passed into the function. 套接字不会传递给函数。

How can I call the methods dynamically, and still be able to use socket within the method? 如何动态调用方法,并且仍然能够在方法内使用套接字?

UPDATE: 更新:

With this EXACT code: 使用此精确代码:

io.sockets.on('connection', function(socket) {
    for(key in socketMethods){
        socket.on(key, function(data){
            socketMethods[key](data, socket);
        });
    }
});

I've found something very interesting. 我发现了一些非常有趣的东西。

It DOES work, and I can run socket.emits from the method! 它确实起作用,并且我可以从该方法中运行socket.emits!

HOWEVER, now, it just runs the LAST method. 但是,现在,它只运行LAST方法。 In this case, action3 is called no matter what's asked for. 在这种情况下,无论要求什么,都将调用action3。 If you ask for action1, or action 2, action 3 is still called. 如果要求动作1或动作2,则仍将调用动作3。

UPDATE: 更新:

Running a console.log(socket._events), I see that the functions error:[function], action1:[function], action2:[function], action3:[function] ARE attached. 运行console.log(socket._events),我发现附加了以下函数错误:[function],action1:[function],action2:[function],action3:[function]。 When I try to see what's in each of those functions, all I get are. 当我尝试查看每个功能的内容时,我所得到的只是。

function ack() {
      self.packet({
          type: 'ack'
        , args: io.util.toArray(arguments)
        , ackId: packet.id
      });
    }

Regardless of the function I send back to myself. 不管我发回什么功能。 I've also set action2, to be the last thing in the object, and action2 is what is called. 我还将action2设置为对象中的最后一件事,而action2就是所谓的。 Yet. 然而。 When I attached socket.on methods the -normal- way, they look exactly the same in socket._events. 当我以正常方式附加socket.on方法时,它们在socket._events中看起来完全一样。

I had a theory that all the methods are being overwritten by the "last" action(action3). 我有一个理论,所有方法都被“最后一个” action(action3)覆盖。

Can't sleep 无法入睡

The large mistake I made was that I was trying to send the function in assuming that the loop data would always persist. 我犯的一个大错误是,我在假定循环数据将始终存在的情况下尝试发送该函数。 I was instead receiving a reference of the key data. 相反,我收到了关键数据的参考。

I'll have to do more research but instead, we needed to create a function 我将需要做更多的研究,但相反,我们需要创建一个函数

Figured this out because of this post, thanks to ebohlman 由于有这篇文章,所以想通了这个,多亏了ebohlman

socket.io Using a for loop to add socket.on methods socket.io使用for循环添加socket.on方法

function makeOn(key) {
  return function(data) {
    dummy.socketMethods[key].call(socket, data);
  }
}

for(key in dummy.socketMethods){
  socket.on(key, makeOn(key));
}

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

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