简体   繁体   English

JavaScript 和 PHP 中的双向服务器/客户端通信

[英]Bidirectional server/client communication in JavaScript and PHP

I want to create a multiplayer game using JavaScript (no jQuery) and PHP, where most of the mechanics use AJAX calls.我想使用 JavaScript(无 jQuery)和 PHP 创建多人游戏,其中大多数机制使用 AJAX 调用。 However, I need to determine when a user has left the game to update the player status on other players' screens (I assume by regular AJAX requests?).但是,我需要确定用户何时离开游戏以更新其他玩家屏幕上的玩家状态(我假设通过常规 AJAX 请求?)。 Also, once all players have left the game files (.txts) on the server need to be deleted.此外,一旦所有玩家都离开了服务器上的游戏文件(.txts),就需要删除。

I am using a free web hosting service which means I can't use WebSockets or cron jobs.我正在使用免费的 web 托管服务,这意味着我不能使用 WebSockets 或 cron 作业。 I also don't want to use Node.js.我也不想使用 Node.js。 Most of what I have read advise regularly timestamping with PHP sessions and this is fine, but I would like to know how to then check to see if the user/game has been inactive for a period of time.我读过的大部分内容都建议定期使用 PHP 会话进行时间戳记,这很好,但我想知道如何检查用户/游戏是否在一段时间内处于非活动状态。

Also, using window.onbeforeunload is too unreliable, in case browsers crash etc.此外,使用window.onbeforeunload太不可靠,以防浏览器崩溃等。

Don't use the wrong tool for the job.不要为工作使用错误的工具。

Any kind of AJAX-based solution you attempt for this purpose is likely to very be inefficient, or unreliable, or probably both.您为此目的尝试的任何类型的基于 AJAX 的解决方案都可能非常低效或不可靠,或者可能两者兼而有之。 If you have more than a tiny number of concurrent users, the sheer volume of AJAX requests would be likely to overwhelm the server and potentially bust your monthly quota.如果您有超过少量的并发用户,那么 AJAX 请求的绝对数量可能会使服务器不堪重负,并可能破坏您的每月配额。 And as you've discovered, determining when someone has ended their session by closing the browser window is not straightforward or reliable.正如您所发现的,通过关闭浏览器 window 来确定某人何时结束了他们的 session 并不简单或可靠。 I would advise against any such architecture.我建议不要使用任何这样的架构。

Websockets is really the correct solution for real-time or near-real-time updates between client and server (and vice versa). Websockets 确实是客户端和服务器之间实时或近实时更新的正确解决方案(反之亦然)。 It'a also easy for the socket server to know when someone has disconnected (which would occur if they close the window/tab).套接字服务器也很容易知道某人何时断开连接(如果他们关闭窗口/选项卡就会发生这种情况)。

So you could either upgrade your hosting so you're able to run a websocket server successfully, or try to integrate a websocket based solution hosted elsewhere, eg Azure SignalR or some similar product (I am not making specific recommendations in an answer, as that's regarded as off-topic). So you could either upgrade your hosting so you're able to run a websocket server successfully, or try to integrate a websocket based solution hosted elsewhere, eg Azure SignalR or some similar product (I am not making specific recommendations in an answer, as that's视为题外话)。

use an interval function and ping the php endpoint every second.使用间隔 function 并每秒 ping php 端点。 If it stops the User is out如果它停止用户退出

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

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