简体   繁体   中英

Expire or timelimit socket.io connection in node.js

I've dug around in the docs but don't see anything that specifically handles this so I'm looking for a manageable workaround to effectively 'expire' or disconnect a socket.io client after a certain amount of time. Obviously on node.js I need any events handing this to be async and non thread blocking.

It seems to me to be a bad example of what I want to achieve, but consider:

A user connects to a room with a max time of 3 minutes:

socket.on('connection', function(params){
   var maxTime = params.maxTime; 
   socket.join(params.roomId);
   setTimeout(function{
   socket.leave(params.roomId);
}, 180000)
});

I feel like this would work for 1 connection fine, but seems like it would cause some serious overhead should I have say 10000 concurrent clients connected to a range of rooms?

Is there a better or accepted way of tackling this?

为什么不在客户端跟踪连接时间并且不使用任何服务器资源呢?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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