每次 socket.io 尝试连接时,我都会遇到错误(chrome 33 / firefox 24):
WebSocket 连接到“wss://playground-c9-doum.c9.io/socket.io/1/websocket/QBGO1ZlPhWUBkMVQ9cUP”失败:WebSocket 握手期间出错:状态行不以 CRLF socket.io.js:2371 结尾
在服务器端:
var io = socket.listen(server);
var store = require('connect-mongo')(express);
io.set('authorization', passportSocketIo.authorize({
passport: require('passport'),
cookieParser: express.cookieParser,
key: 'connect.sid', // the name of the cookie where express/connect stores its session_id
secret: constant.SALT, // the session_secret to parse the cookie
store: new store({
mongoose_connection: db,
collection: constant.SESSION_COLLECTION,
}),
success: onAuthorizeSuccess, // *optional* callback on success - read more below
fail: onAuthorizeFail,
}));
io.sockets.on('connection', function (socket) {
socket.on('chat:newText', function (data) {
console.log(socket.handshake.user);
data.username = socket.handshake.user.username;
io.sockets.emit('chat:newText', data);
});
});
客户端 :
var connect = function () {
if (Auth.isLoggedIn() === true) {
socket = io.connect();
connected = true;
return true;
}
else {
console.log('Socket wont connect as user is not logged in');
return false;
}
};
我正在使用 express、passport、passport-socket.io ......
我不认为问题出在passport-socket.io,因为我尝试删除套接字配置(io.set('authorization'))的'auth'部分,但我遇到了同样的问题。
我还尝试为客户端 socket.connect(url) 提供一个特定的 url,但也没有工作。
Socket.io 并没有完全失败,websocket 连接被中止,所以它开始 xhr-polling(很痛苦......)。
做了很多研究,没有发现任何关于这个问题的东西(还在 github 上发布了一个新问题)。
npm socket.io -v give 1.3.8 /socket.io/socket.io.js (client) is Socket.IO.js build:0.9.16