我使用以下内容:
"dependencies": {
"express": "^4.0.0",
"socket.io": "0.9.16",
"mongoose": "^3.8.8",
"passport": "^0.2.0",
"passport-local": "^1.0.0",
"express-session": "^1.0.3",
"cookie-parser": "^1.0.1",
"body-parser": "^1.0.2",
"session-mongoose": "git://github.com/danpe/session-mongoose.git#master",
"passport.socketio": "^3.0.1"
}
设置我的socket.io授权:
io.set("authorization", passportSocketIo.authorize({
passport : passport,
cookieParser: cookieParser(),
key: settings.sessionKey, //the cookie where express (or connect) stores its session id.
secret: settings.sessionSecret, //the session secret to parse the cookie
store: sessionStore, //the session store that express uses
fail: function(data, accept) {
console.log("failed");
// console.log(data);// *optional* callbacks on success or fail
accept(null, false); // second param takes boolean on whether or not to allow handshake
},
success: function(data, accept) {
console.log("success socket.io auth");
// console.log(data);
accept(null, true);
}
}));
当我试图与我的客户联系时:
var socket = io.connect('http://127.0.0.1:3000/');
socket.on('connect', function () {
// socket connected
console.log("Socket Connected!");
});
服务器因以下内容崩溃:
S:\Server\node_modules\cookie-parser\index.js:27
var cookies = req.headers.cookie;
^
TypeError: Cannot read property 'cookie' of undefined
at Object.cookieParser(S:\Server\node_modules\cookie-parser\index.js:27:30)
at parseCookie (S:\Server\node_modules\passport.socketio\lib\index.js:4:27)
at Manager.<anonymous> (S:\Server\node_modules\passport.socketio\lib\index.js:37:19)
at Manager.authorize (S:\Server\node_modules\socket.io\lib\manager.js:910:31)
at Manager.handleHandshake (S:\Server\node_modules\socket.io\lib\manager.js:786:8)
at Manager.handleRequest (S:\Server\node_modules\socket.io\lib\manager.js:593:12)
at Server.<anonymous> (S:\Server\node_modules\socket.io\lib\manager.js:119:10)
at Server.EventEmitter.emit (events.js:98:17)
at HTTPParser.parser.onIncoming (http.js:2108:12)
at HTTPParser.parserOnHeadersComplete [as onHeadersComplete] (http.js:121:23)
任何想法如何发生这种情况?
答案 0 :(得分:6)
这是一个愚蠢的错误,我应该将cookieParser作为库而不是实例化对象传递:
var cookieParser= require('cookie-parser');
io.set("authorization", passportSocketIo.authorize({
passport : passport,
cookieParser: cookieParser,
key: settings.sessionKey, //the cookie where express (or connect) stores its session id.
secret: settings.sessionSecret, //the session secret to parse the cookie
store: sessionStore, //the session store that express uses
fail: function(data, accept) {
console.log("failed");
// console.log(data);// *optional* callbacks on success or fail
accept(null, false); // second param takes boolean on whether or not to allow handshake
},
success: function(data, accept) {
console.log("success socket.io auth");
// console.log(data);
accept(null, true);
}
}));
答案 1 :(得分:3)
嗨,我遇到了同样的问题。
它非常符合逻辑,因为您正在向cookie解析器传递套接字而不是请求。只需将socket.request
传递给cookie解析器,就应该修复它。
可在此处找到解决方案:https://github.com/expressjs/cookie-parser/issues/3#issuecomment-41443866
var cookieParser = require('cookie-parser')(SESSION_SECRET);
// ### Cookie parser
// Wrapper arround Express cookie parser, so we can use the same cookie parser for socket.io.
// Parse Cookie header and populate `socket.request.cookies` with an object keyed by the cookie names.
// Uses signed cookies by passing a secret string, which assigns `socket.request.secret` so it may be used by other middleware.
function cookieParserWrapper (socket, next) {
// request, response and callback
cookieParser(socket.request, {}, next);
}
编辑:
找到一个有趣的模块 socket.io-bundle :https://github.com/nkzawa/socket.io-bundle/blob/master/lib/cookieParser.js
除了cookie-parser包装器外,它还有一个用于express-session和csurf模块的包装器。