客户端连接两次到服务器,Socket.io

我正在尝试使用passport.socketio将socket.io连接到passport.js来loginlogin用户的用户名。 它是成功的,但它logging了用户名两次,并试图寻找相当长的时间后,我卡住了。

代码部分如下:

服务器代码:

var server = http.createServer(app); var io = require('socket.io')(server); io.use(passportSocketIo.authorize({ cookieParser: cookieParser, // the same middleware you registred in express key: 'connect.sid', // the name of the cookie where express stores its session_id secret: 'hello', // the session_secret to parse the cookie store: new (require("connect-mongo")(Esession))({ url: "mongodb://localhost/local" }), // 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 })); function onAuthorizeSuccess(data, accept){ console.log('successful connection to socket.io'); accept(null, true); accept(); } function onAuthorizeFail(data, message, error, accept){ if(error) throw new Error(message); console.log('failed connection to socket.io:', message); // We use this callback to log all of our failed connections. accept(null, false); // OR // If you use socket.io@1.X the callback looks different // If you don't want to accept the connection if(error) accept(new Error(message)); // this error will be sent to the user as a special error-package // see: http://socket.io/docs/client-api/#socket > error-object } io.on('connection', function(socket) { var userID = socket.request.user; console.log(userID.tg+ " has connected") }); // catch 404 and forward to error handler app.use(function(req, res, next) { var err = new Error('Not Found'); err.status = 404; next(err); }); var port = normalizePort(process.env.PORT || '3000'); app.set('port', port); server.listen(port); 

客户代码

 <script src="http://code.jquery.com/jquery-1.11.1.js"></script> <script src="/socket.io/socket.io.js"></script> <script> var socket = io.connect(); </script> 

输出如下:

 successful connection to socket.io UserName has connected UserName has connected 

我不确定为什么它输出两次任何帮助将不胜感激。 我对Node.js比较陌生,但我不相信这是导致它的护照部分,但是我被困住了,所以我可能没有最好的想法。

先谢谢了

编辑:试过最新版本的套接字和检查我自己的版本都似乎是最新版本,只是为了确保我更新客户端代码:

 <script src="http://code.jquery.com/jquery-1.11.1.js"></script> <script src="https://cdn.socket.io/socket.io-1.3.2.js"></script> <script> var socket = io.connect(); </script> 

问题依然存在

我发现我认为是我自己的愚蠢的问题,

成功授权后,我两次接受连接。 因此,它正在注册一切两次更正应该是:

 function onAuthorizeSuccess(data, accept){ console.log('successful connection to socket.io'); //accept(null, true); -> remove this line accept(); } 

这显然是以前的版本中的错误。 你可能只需要升级。 看看下面的问题。 https://github.com/Automattic/socket.io/issues/350

将来,如果您正在尝试debugging这样的问题,熟悉Node Events Class Documentation是非常有用的。 io只是一个事件监听器,这里发生的一些事情是由于某种原因connection事件正在执行双重任务。

这可能是一些Socket.io代码emit('connection')太多次。

当我debugging这样的东西时,调用emitter.listeners('event')可能会很有用,因为它会告诉我是谁,更重要的是有多less听众正在听。

所以在你的情况下, io.listeners('connection'); 会给你一大堆所有听这个事件的人。 显然, io正在注册两次,但是你可能会发现这个技巧在哪里。

此外,进入包装本身和grep emit('connection'); 会告诉你,只是在那个讨厌的bug。