我正在尝试构建一个连接两个随机用户的匹配算法,但我无法找到一种方法来删除连接(在MongoDB集合中生成,因此我需要删除查询)离开了页面。
答案 0 :(得分:1)
也许window.onbeforeunload会对您有所帮助。它在用户离开页面时执行Javascript。
流星:
Meteor.startup(function(){
$(window).bind('beforeunload', function() {
closingWindow();
});
});
closingWindow = function(){
...
}
阵营:
componentDidMount() {
window.addEventListener('beforeunload', this.handleLeavePage);
}
componentWillUnmount() {
window.removeEventListener('beforeunload', this.handleLeavePage);
}
handleLeavePage() {
...
}
答案 1 :(得分:0)
用户如何触发页面离开,是通过单击按钮?一段时间后? MongoDB是实时的,如果两个用户之间的连接基于数据库,则将数据库连接设置为null或删除其实例+发布和订阅的使用将起到作用。这是一个例子:
const CheckIfConnection = Meteor.subscribe('collectionsubscription',userId1,userId2)
if(CheckIfConnection.ready()){
const connection = Collection.'ConnectionCollectionName'.findOne();
//Pass it on the component
}
在组件方面,你可以拥有一个'ComponentWillReceiveProps',它将在一个prop(来自容器)来改变时触发,即连接是Gone时触发的。这可以通过数据库方法在匹配算法中发挥作用:)