Currently, I am developing a multiplayer game in Angular utilizing the PubNub service along with the presence add-on.
One of the challenges I am facing is detecting when a player unexpectedly leaves the game. This is crucial for sending notifications to other players and properly terminating the game. My attempt to unsubscribe a user upon refreshing the page or navigating to another URL is not working as expected. More specifically, while using Chrome, unlike FireFox, no presence event is triggered, suggesting that the user was not successfully unsubscribed before leaving the page.
Below is the snippet of code I have been trying:
$(window).bind("unload",function(e) {
PubNub.ngUnsubscribe({channel:$scope.game.channel});
e.preventDefault();
});
I acknowledge that relying on beforeunload
and unload
events can be problematic due to poor browser support, which might be the root cause of the issue. If this is indeed the case, I am wondering if there are alternative cross-browser detection methods that I could implement instead?