如何检测用户何时离开网页游戏大厅

发布于 2025-01-19 14:38:45 字数 579 浏览 2 评论 0原文

我有一个基本的多人网络游戏,当玩家进入网站时,他们会在 2 人大厅中进行匹配。我在服务器端使用 nodejsexpress

我希望服务器能够检测到玩家何时“离开”大厅,以便我可以将大厅中剩余的玩家与其他人进行匹配。离开应包括关闭选项卡、浏览器或刷新页面。

我尝试“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文