是否有替代 ajax 不需要轮询而不需要服务器端修改?
我正在尝试创建一个小型且基本的基于“ajax”的多人游戏。 对象的坐标由 PHP“处理程序”给出。 使用 ajax 每 200MS 轮询一次 handler.php 文件。
由于没有任何事情发生时不需要轮询,我想知道是否有什么东西可以在不频繁轮询的情况下完成同样的事情? 例如。 Comet,虽然我听说您需要为 Comet 配置服务器端应用程序。 这是一个共享的网络服务器,所以我不能这样做。
如果客户端无需更改任何内容,则可能会阻止 handler.php 文件返回响应,这可能吗? 然而,即使某些事情还没有改变,你仍然会让客户无用地要求响应。 基本上,如果需要告诉客户端某些事情,它应该只使用带宽和服务器资源,例如。 物体坐标的改变。
I'm trying to create a small and basic "ajax" based multiplayer game. Coordinates of objects are being given by a PHP "handler". This handler.php file is being polled every 200MS, by using ajax.
Since there is no need to poll when nothing happens, I wonder, is there something that could do the same thing without frequent polling? Eg. Comet, though I heard that you need to configure server side applications for Comet. It's a shared webserver, so I can't do that.
Maybe prevent the handler.php file from even returning a response if nothing has to be changed at the client, is that possible? Then again you'd still have the client uselessly asking for a response even though something hasn't changed yet. Basically, it should only use bandwidth and sever resources if something needs to be told to the client, eg. the change of an object's coordinates.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
Comet 通常用于此类事情,并且它可能是一个脆弱的设置,因为它不是一种特别常见的技术,因此很容易不“正确”。 也就是说,现在可用的资源比我上次尝试时大约两年前更多。
我不认为你可以按照你的想法去做,让 handler.php 不返回任何内容并停止执行:Web 服务器将保持连接打开并阻止任何进一步的轮询,直到 handler.php 执行某些操作(终止或提供输出) )。 当它发生时,您仍在处理响应。
您可以尝试长轮询技术,其中您的 AJAX 允许非常大的超时(例如 30 秒),并且 handler.php 会旋转而不响应,直到它有要报告的内容,然后返回。 (您需要确保旋转不是资源密集型的)。 如果 handler.php “过期”并且没有任何反应,请让它退出并让 AJAX 再次轮询。 由于它每 30 秒才发生一次,因此与每秒 5 次相比,这将是一个巨大的改进。 这将使您的投票保持在最低限度。
但这正是 Comet 的设计目的。
Comet is generally used for this kind of thing, and it can be a fragile setup as it's not a particularly common technology so it can be easy not to "get it right." That said, there are more resources available now than when I last tried it ~2 years ago.
I don't think you can do what you're thinking and have handler.php simply not return anything and stop execution: The web server will keep the connection open and prevent any further polling until handler.php does something (terminates or provides output). When it does, you're still handling a response.
You can try a long polling technique, where your AJAX allows a very large timeout (e.g. 30 seconds), and handler.php spins without responding until it has something to report, then returns. (You'll want to make sure the spinning is not resource-intensive). If handler.php "expires" and nothing happens, have it exit and let AJAX poll again. Since it only happens every 30 seconds, it will be a huge improvement over ~5 times a second. That would keep your polling to a minimum.
But that's the sort of thing Comet is designed for.
由于 Ajax 只为您提供客户端服务器请求模型(通常称为拉取,而不是推送),因此从服务器获取数据的唯一方法是通过请求。 然而,解决这个问题的常用技术是服务器仅在有新数据时才响应。 因此,客户端发出请求,服务器会保留该请求,直到发生某些情况,然后进行回复。 即使数据没有更改,这也可以避免频繁轮询的需要,因为您只需要客户端在收到响应后发送新请求。
由于您使用的是 PHP,一种简单的方法可能是让 PHP 代码在检查数据更改之间一次调用 sleep 命令 200 毫秒,然后在数据发生更改时将数据返回给客户端。
编辑:我还建议请求超时。 因此,如果 2 秒内没有任何反应,则会发回一条“无变化”消息。 这样客户端就知道服务器仍然存在并正在处理其请求。
As Ajax only offers you a client server request model (normally termed pull, rather than push), the only way to get data from the server is via requests. However a common technique to get around this is for the server to only respond when it has new data. So the client makes a request, the server hangs on to that request until something happens and then replies. This gets around the need for frequent polling even when the data hasn't changed as you only need the client send a new request after it gets a response.
Since you are using PHP, one simple method might be to have the PHP code call the sleep command for 200ms at a time between checks for data changes and then return the data to the client when it does change.
EDIT: I would also recommend having a timeout on the request. So if nothing happens for say 2 seconds, a "no change" message is sent back. That way the client knows the server is still alive and processing its request.
由于它被标记为“html5”:HTML5 有
和 WebSocket< /a>,但实践中执行方仍采用将来时态。Opera 实现了旧版本的
,称为
。Since this is tagged “html5”: HTML5 has
<eventsource>
and WebSocket, but the implementation side is still in the future tense in practice.Opera implemented an old version of
<eventsource>
called<event-source>
.这里有一个解决方案 - 使用 SaaS comet 提供商,例如 WebSync On-Demand。 无需担心服务器资源,无论是否共享托管,因为它全部被卸载,并且您可以根据需要推出信息。
由于它是 SaaS,因此它可以与任何服务器语言一起使用。 对于 PHP,已经有一个发布者编写并准备就绪。
Here's a solution - use a SaaS comet provider, such as WebSync On-Demand. No server resources to worry about, shared hosting or not, since it's all offloaded, and you can push out the information as needed.
Since it's SaaS, it'll work with any server language. For PHP, there's already a publisher written and ready to go.
服务器必须参与此过程。 请咨询托管提供商有哪些模块可用。 或者尝试说服他们支持彗星。
也许您应该为此考虑使用小型虚拟专用服务器(VPS)。
The server must take part in this. Check with the hosting provider what modules are available. Or try to convince them to support Comet.
Maybe you should consider a small Virtual Private Server (VPS) for this.
在长轮询建议上要添加的一件事是:如果您使用的是共享服务器,则此解决方案的可扩展性将受到限制,因为每个活动的长轮询都会使连接(以及为该连接提供服务的服务器端进程)保持活动状态。 您的提供商很可能对您一次可以打开的连接数量有限制(无论是策略定义的还是事实上的),因此,如果您的会话/窗口多于同时播放的会话/窗口,您就会遇到困难。
One thing to add on the long polling suggestions: If you're on a shared server, this solution will have limited scalability, as each active long poll will keep a connection (and a server-side process to service that connection) active. Your provider most likely has limits (either policy-defined or de facto) on the number of connections you can have open at a time, so you'll hit a wall if you have more sessions/windows than that playing concurrently.