0

嗨,伙计们有一个问题,我怎样才能从套接字中获取所有用户AddUsers并将其转移到套接字get winners?我试图将它直接调用到函数中,但它并没有显示出来..我需要将它保存到 cookie 中吗?

var usernames = [];
io.sockets.on('connection', function (socket){



     socket.on('addUsers', function (data) {

     var users   = {
         data : data.box,
         socket_id : socket.id
     }

      socket.username = data.username;
          usernames.push({username : data.username, itemid : data.itemid,  box : data.box,socket_id : socket.id});

         socket.emit('displayUsers', {users: usernames});
     });


     socket.on('get winners', function (data) {
     socket.emit('display winners', {data: usernames});

     });




});
4

1 回答 1

0

只需声明另一个变量以使其保持不变:

var usernames = [];
var sockets = {};
io.sockets.on('connection', function (socket){
    sockets[socket.id] = socket;
    socket.on('addUsers', function (data) {
        ...
    }
}

就是这样。当你想使用它时,只需输入“sockets[yourSocketId].emit()”

于 2013-11-15T06:47:51.687 回答