简体   繁体   English

Socket.io在页面刷新上创建多个连接

[英]Socket.io creating multiple connections on page refresh

I am making a chatroom but after each page refresh, more connections are made. 我正在创建一个聊天室,但是在刷新每个页面后,会建立更多的连接。 For example, if I go to my chat room page, only one connection is created. 例如,如果我转到聊天室页面,则仅创建一个连接。 However, after I refresh once, 2 clients are now connected. 但是,刷新一次后,现在已连接2个客户端。 After each page refresh, one more connection is added. 刷新每个页面后,又添加了一个连接。

here is my chat route 这是我的聊天路线

    /* GET home page. */
router.get('/', accessControl.ensureAuthenticated, function(req, res, next) {
  const io = req.app.io;
  console.log('const io created');
  io.on('connection', function(socket){
    console.log(' %s sockets connected', io.engine.clientsCount);
    console.log('[NodeApp] (socket.io) A client has connected');
    socket.on('chat message', function(message){
      if (message.sessionID == req.session.id) {
        io.emit('chat message', message);
        console.log('message: ' + message.message);
      } else {
        console.log('client sessionID ('+message.sessionID+') does not match server sessionID ('+req.session.id+')');
      }
    });
    socket.on('disconnect', function(){
      console.log('[NodeApp] (socket.io) A client has disconnected');
      socket.disconnect();
    });
  });

  res.render('chat/index', {
    title: "Chat",
    //send session id for client verification
    sessionID: req.session.id,
  });
});

the output after refreshing the chat page 3 times: 刷新聊天页面3次后的输出:

const io created
1 sockets connected
[NodeApp] (socket.io) A client has connected
const io created
[NodeApp] (socket.io) A client has disconnected
1 sockets connected
[NodeApp] (socket.io) A client has connected
1 sockets connected
[NodeApp] (socket.io) A client has connected
const io created
[NodeApp] (socket.io) A client has disconnected
[NodeApp] (socket.io) A client has disconnected
1 sockets connected
[NodeApp] (socket.io) A client has connected
1 sockets connected
[NodeApp] (socket.io) A client has connected
1 sockets connected
[NodeApp] (socket.io) A client has connected
message: test message (should be sent 3 times)
message: test message (should be sent 3 times)
message: test message (should be sent 3 times)

You can't put this: 你不能这样:

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

inside a route handler. 在路由处理程序中。 Every time that route is hit, you create yet another listener for the event, so they pile up with lots of duplicates and thus you process each message multiple times in all those duplicate event handlers. 每次点击该路由时,您都会为该事件创建另一个侦听器,因此它们会堆积很多重复项,因此您将在所有这些重复事件处理程序中多次处理每条消息。

You need to put that outside any route handler. 您需要将其放在任何路由处理程序之外。 The socket.io server and its listeners needs to be configured when you configured your socket.io server, not in a route handler. 在配置socket.io服务器时,必须在路由处理程序中而不是在配置socket.io服务器及其侦听器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM