0

socket.io コードを 0.9v から 1.x に再構築しようとしています。Express と socket.io は最終バージョンです。

したがって、エラー: Cannot call method 'get' of undefinedat

 io.on('connection', function(socket) {
            console.log(socket.handshake);
            var username = socket.handshake.user.get('username'); //<----here

            ......

これは私のコードです:

module.exports = function(server) {
                var io = require('socket.io')(server);
                io.set('origins', 'localhost:*');
            //I changed set to use, but it's doesn't matter i think
                io.use(function(socket, next) {  
                    var handshake = socket.request;
                //io.set('authorization', function(handshake, callback) {
                    async.waterfall([
                        function(callback) {
                         handshake.cookies = cookie.parse(handshake.headers.cookie || '');

     var sidCookie = handshake.cookies[config.get('session:key')];
      var sid = connect.utils.parseSignedCookie(sidCookie, config.get('session:secret'));
      //var sid = cookieParser.signedCookie(sidCookie, config.get('session:secret'));

                            loadSession(sid, callback);
                        },  ...continue...

継続:

function(session, callback) {

                            if (!session) {
                                callback(new HttpError(401, "No session"));
                            }

                            handshake.session = session;
                            loadUser(session, callback);
                        },
                        function(user, callback) {
                            if (!user) {
                                callback(new HttpError(403, "Anonymous session may not connect"));
                            }

                            handshake.user = user;
                            callback(null);
                        }

                    ], function(err) {.....});
                next();
                });

        io.on('connection', function(socket) { ... };

io を返します。};

わかりました、私を助けてください、そしてありがとう!

4

1 に答える 1

0

これをどのようにデバッグするか。 未定義のものに対してメソッドを呼び出すことはできません

したがって、.get を呼び出しているものはまだ作成されていないことがわかります。この場合:

var username = socket.handshake.user.get('username'); //<----here

したがって、ide デバッガーにアクセスできない場合に最初に行うことは、

console.log(ソケット)

データがある場合

console.log(socket.handshake);

そこにデータがあれば

console.log(socket.handshake.user);

データがある場合は、メソッド GET があることを確認してください。

とにかく、私の仮定は、socket.handshake.userまだ定義されていないということです。これを設定/公開するコードはどこにありますか?

于 2014-11-09T11:28:20.590 に答える