简体   繁体   English

取消订阅PubNub频道以响应(之前)卸载事件

[英]Unsubscribe to a PubNub channel in response to (before)unload event

I am building a multi-players game in angular using the PubNub service, including the presence add-on. 我正在使用PubNub服务(包括状态附加组件)以角度构建多人游戏。

Since I need to detect if someone left the game unexpectedly - in order to send a message to the other players and end the game - I am trying to unsubscribe a user when he refreshes the page or goes to some other url. 由于我需要检测是否有人意外离开了游戏-为了向其他玩家发送消息并结束游戏-我试图在用户刷新页面或转到其他网址时退订该用户。 However, in Chrome - as opposed to FireFox - no presence event is being triggered, which probably means that the user wasn't unsubscribed prior to his leaving of the page. 但是,在Chrome(与FireFox相对)中,不会触发在线状态事件,这可能意味着用户在离开页面之前并未取消订阅。

CODE: 码:

 $(window).bind("unload",function(e) {

    PubNub.ngUnsubscribe({channel:$scope.game.channel});
    e.preventDefault();

    }); 

I am aware that these events ( beforeunload and unload ) are problematic and have poor browser support, which is probably the reason for the failure. 我知道这些事件( beforeunloadunload )是有问题的,并且对浏览器的支持不佳,这可能是失败的原因。 If this is the case, is there any other cross-browsers detection method I can use? 如果是这种情况,我还可以使用其他任何跨浏览器检测方法吗?

There is no reliable way to catch the browser closing. 没有可靠的方法来捕获浏览器关闭。 The "beforeUnload" event is your best shot, but most mobile browsers don't trigger it. “ beforeUnload”事件是您的最佳选择,但是大多数移动浏览器不会触发它。 The best way to handle a user closing the browser in PubNub is to specify a heartbeat when subscribing ( http://www.pubnub.com/docs/javascript/api/reference.html#subscribe ). 处理用户关闭PubNub中的浏览器的最佳方法是在订阅时指定心跳( http://www.pubnub.com/docs/javascript/api/reference.html#subscribe )。 The heartbeat tells how many seconds PubNub should wait after an ungraceful disconnect (such as closing the browser), before considering a user as timed out. 心跳表明在非正常断开连接(例如关闭浏览器)之后,PubNub应该等待几秒钟,然后才将用户视为超时。 This is defaulted to 320 and can be set to as low as 6 seconds (> 5). 默认为320,可以设置为低至6秒(> 5)。 It still won't be immediate, but you will receive a presence "timeout" action 6 seconds after the browser is closed. 它仍然不会立即生效,但是在关闭浏览器后6秒钟您将收到状态“超时”操作。 Having a low heartbeat could impact your battery life so be wary of that. 心跳过短可能会影响电池寿命,因此请当心。

Also, I know I linked the doc to the JS API, but the angular ngSubscribe allows most of the same arguments. 另外,我知道我已将文档链接到JS API,但是有角的ngSubscribe允许大多数相同的参数。

Hope that helps. 希望能有所帮助。

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

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