如何检测用户何时离开网页游戏大厅
我有一个基本的多人网络游戏,当玩家进入网站时,他们会在 2 人大厅中进行匹配。我在服务器端使用 nodejs 和 express 。
我希望服务器能够检测到玩家何时“离开”大厅,以便我可以将大厅中剩余的玩家与其他人进行匹配。离开应包括关闭选项卡、浏览器或刷新页面。
我尝试在“unload”或“beforeunload”事件触发时从客户端发送ajax请求。但我发现这非常不一致且不可靠。我见过人们使用 websockets 来管理此类客户端-服务器通信,但从未使用过它们。 我想到在服务器端使用计时器来查看玩家是否停止回复,这可能会起作用。我应该如何实现这个以及其他多人网络游戏如何解决这个问题?
有关我的服务器端的详细信息(如果有帮助):我目前正在使用快速会话来确定玩家的 ID。一旦玩家进入 2 人大厅,他们就会向服务器发送连续(每 0.5 秒)ajax 请求以获取有关其他玩家的信息。
I've a basic multiplayer web-game where players get matched in a lobby of 2 when they enter the website. I'm using nodejs and express for the server side.
I want the server to detect when a player "leaves" the lobby so that i can then match the remaining player in the lobby with someone else. Leaving should include closing the tab, the browser or refreshing the page.
I tried sending an ajax request from the client side when the "unload" or "beforeunload" events fire. However i found this to be pretty inconsistent and not reliable. I've seen people use websockets for managing such client-server communications but never used them. I thought of using a timer on the server side to see if a player stops replying which might do the trick. How should i go about implementing this and how do other multiplayer web-games solve this problem?
Details about my server-side if it helps: I'm currently using express-session to decide ID's of players. Once the players are in a lobby of 2, they send continuous (every 0.5 seconds) ajax requests to the server to get information about the other player.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论