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

client connecting twice to server, Socket.io

我正在尝试通过使用 passport.socketio 将 socket.io 连接到 passport.js 来记录已登录用户的用户名。它是成功的,但是它记录了用户名两次,并且在尝试并搜索了相当长的时间后我被卡住了。

代码段如下:

服务器代码:

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>

问题仍然存在

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

将来,如果您尝试调试此类问题,熟悉 Node Events Class Documentation 会很有用。 io 只是一个事件侦听器,这里发生的事情是出于 某些 原因 connection 事件正在执行双重任务。

可能是某些 Socket.io 代码触发了 emit('connection') 太多次。

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

因此,在您的情况下,io.listeners('connection'); 将为您提供一组正在收听该活动的所有人。很明显,io 注册了两次,但你可以通过这个技巧找到它的位置。

此外,进入包本身并搜索 emit('connection'); 会告诉你那个讨厌的错误在哪里。

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

我在成功授权后接受了两次连接。因此,它对所有内容进行了两次注册,更正应该是:

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