客户端断开连接后的服务器清理 [英] Server cleanup after a client disconnects
本文介绍了客户端断开连接后的服务器清理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法通过刷新或离开页面来检测客户端何时断开与流星服务器的连接,以便服务器可以尝试进行一些清理?
Is there a way to detect when a client disconnects from a meteor server, either by refreshing or navigating away from the page, so that the server can attempt some cleanup?
推荐答案
一种技术是实现每个客户端定期调用的keepalive方法。假设您在每个客户的会话
中保留了 user_id
。
One technique is to implement a "keepalive" method that each client regularly calls. This assumes you've got a user_id
held in each client's Session
.
// server code: heartbeat method
Meteor.methods({
keepalive: function (user_id) {
if (!Connections.findOne(user_id))
Connections.insert({user_id: user_id});
Connections.update(user_id, {$set: {last_seen: (new Date()).getTime()}});
}
});
// server code: clean up dead clients after 60 seconds
Meteor.setInterval(function () {
var now = (new Date()).getTime();
Connections.find({last_seen: {$lt: (now - 60 * 1000)}}).forEach(function (user) {
// do something here for each idle user
});
});
// client code: ping heartbeat every 5 seconds
Meteor.setInterval(function () {
Meteor.call('keepalive', Session.get('user_id'));
}, 5000);
这篇关于客户端断开连接后的服务器清理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文