Node.js socket.io-TypeError:无法读取属性';握手';未定义的

Node.js socket.io-TypeError:无法读取属性';握手';未定义的,node.js,express,socket.io,socket.io-1.0,Node.js,Express,Socket.io,Socket.io 1.0,由于安装了socket.io,我在使用我的应用程序时遇到错误“TypeError:无法读取未定义的属性'handshaken' 似乎是helpers/utils.js文件中的checkUserHaveConnect方法导致了错误,因为在错误发生之前调用了my console.log(“io 2”,io”)(这可以在下面的屏幕截图中看到) 奇怪的是,这个错误并没有发生在我的开发环境(笔记本电脑)上,而只是在部署到生产环境(nodejitsu)时才会发生 首先,这里是启动socket.io连接的索引

由于安装了socket.io,我在使用我的应用程序时遇到错误“TypeError:无法读取未定义的属性'handshaken'

似乎是
helpers/utils.js
文件中的checkUserHaveConnect方法导致了错误,因为在错误发生之前调用了my console.log(“io 2”,io”)(这可以在下面的屏幕截图中看到)

奇怪的是,这个错误并没有发生在我的开发环境(笔记本电脑)上,而只是在部署到生产环境(nodejitsu)时才会发生

首先,这里是启动socket.io连接的索引文件

index.js

var server = http.createServer(app);

var io = require('socket.io').listen(server);
GLOBAL.io = io;

// launch
server.listen(port);

//With Socket.io >= 1.0
io.use(passportSocketIo.authorize({
    cookieParser: express.cookieParser,
    key:         EXPRESS_SID_KEY,       // the name of the cookie where express/connect stores its session_id
    secret:      SESSION_SECRET,    // the session_secret to parse the cookie
    store:       sessionStore,        // we NEED to use a sessionstore. no memorystore please
    success:     onAuthorizeSuccess,  // *optional* callback on success - read more below
    fail:        onAuthorizeFail,     // *optional* callback on fail/error - read more below
}));

io.on('connection', function (socket) {
    console.log("connection come");

    socket.on('disconnect', function (socket) {
        console.log("disconnect come");
    });
});

function onAuthorizeSuccess(data, accept){
    console.log('successful connection to socket.io');
    accept(null, true);
}

function onAuthorizeFail(data, message, error, accept){
    console.log('failed connection to socket.io:', message);
    if(error){
        throw new Error(message);
    }
    // We use this callback to log all of our failed connections.
    accept(null, false);
}

console.log('The magic happens on port' + port);
var passportSocketIo = require("passport.socketio");
exports.emitData = function(userId, eventName,data){

console.log("io 1", io);

    var sockets = passportSocketIo.filterSocketsByUser(io, function(user){
        if(user._id){
            return user._id.toHexString() === userId.toHexString();
        }
        return false;
    });
    sockets.forEach(function(socket){
        socket.emit(eventName,data);
    });
    return sockets.length;
}

exports.checkUserHaveConnect = function(userId){

console.log("io 2", io);

    return passportSocketIo.filterSocketsByUser(io, function(user){
        if(user._id){
            return user._id.toHexString() === userId.toHexString();
        }
        return false;
    }).length;
}
以下是可能导致问题的方法

helpers/utils.js

var server = http.createServer(app);

var io = require('socket.io').listen(server);
GLOBAL.io = io;

// launch
server.listen(port);

//With Socket.io >= 1.0
io.use(passportSocketIo.authorize({
    cookieParser: express.cookieParser,
    key:         EXPRESS_SID_KEY,       // the name of the cookie where express/connect stores its session_id
    secret:      SESSION_SECRET,    // the session_secret to parse the cookie
    store:       sessionStore,        // we NEED to use a sessionstore. no memorystore please
    success:     onAuthorizeSuccess,  // *optional* callback on success - read more below
    fail:        onAuthorizeFail,     // *optional* callback on fail/error - read more below
}));

io.on('connection', function (socket) {
    console.log("connection come");

    socket.on('disconnect', function (socket) {
        console.log("disconnect come");
    });
});

function onAuthorizeSuccess(data, accept){
    console.log('successful connection to socket.io');
    accept(null, true);
}

function onAuthorizeFail(data, message, error, accept){
    console.log('failed connection to socket.io:', message);
    if(error){
        throw new Error(message);
    }
    // We use this callback to log all of our failed connections.
    accept(null, false);
}

console.log('The magic happens on port' + port);
var passportSocketIo = require("passport.socketio");
exports.emitData = function(userId, eventName,data){

console.log("io 1", io);

    var sockets = passportSocketIo.filterSocketsByUser(io, function(user){
        if(user._id){
            return user._id.toHexString() === userId.toHexString();
        }
        return false;
    });
    sockets.forEach(function(socket){
        socket.emit(eventName,data);
    });
    return sockets.length;
}

exports.checkUserHaveConnect = function(userId){

console.log("io 2", io);

    return passportSocketIo.filterSocketsByUser(io, function(user){
        if(user._id){
            return user._id.toHexString() === userId.toHexString();
        }
        return false;
    }).length;
}

要解决这个问题,我必须使用passport.socketio的最新版本,因为NPM上的版本已经过时

在使用nodejitsu时,我必须在package.json文件中添加一个“bundleDependencies”部分,如下所示:

"bundleDependencies": [
"passport.socketio"
],

这可确保使用passport.socketio的本地版本,而不是NPM版本。

此功能已中断,因为socket.io>1.0不再具有用于passport.socketio FilterSocketsByser方法的握手数据(替换为“请求”)

到npm的路上有一个补丁,同时,您可以在包中替换:

function filterSocketsByUser(socketIo, filter){
  var handshaken = socketIo.sockets.manager.handshaken;
  return Object.keys(handshaken || {})
    .filter(function(skey){
      return filter(handshaken[skey].user);
    })
    .map(function(skey){
      return socketIo.sockets.manager.sockets.sockets[skey];
    });
}
由以下人员提供:


来源:

您能否共享错误堆栈,而不仅仅是错误描述?