在javascript数组中存储套接字连接并在数组上使用Array.find()会出现意外行为

时间:2015-02-07 19:50:35

标签: javascript node.js socket.io socket.io-1.0

sockets.find()语句后面的代码中,未执行console.log(" AFTER")语句

  io.sockets.on('connection', function (socket) {
    var this_socket = socket;
    socket.username = null;
    socket.gameSessionName = null;
    socket.update_usernames = false;
    sockets.push(socket);  // sockets is AN ARRAY

    socket.on('loaded', function (data) {
        var previous_socket;
        socket.username = data.username;
        socket.status = 'BROWSE_PLAYERS'; // WAIT_FOR_ACCEPT, PLAYING
        console.log("BEFORE");    

        previous_socket = sockets.find (function(s) {           
            return false;
        });

        console.log("AFTER"); // THIS IS STATEMENT IS NOT EXECUTED
        ....

如果socket.find()代码如下所示进行注释,则会打印console.log(" AFTER")语句。

        // previous_socket = sockets.find (function(s) {            
        //  return false;
        // });

看起来 sockets.find(...)语句有副作用。如何解决这个问题。

0 个答案:

没有答案