php - 将 javascript 客户端协调到单个后端游戏的最佳方法是什么?

标签 php javascript notifications

用于通知 javascript 客户端异步发生在游戏中的更改(即其他客户端的移动)的最佳方法是什么。例如,假设一个回合制棋盘游戏。我应该让客户端每隔一秒左右轮询 PHP 后端以获取新 Action ,还是有更好的方法向同一游戏中的其他客户端发送异步通知?对于如何在每个客户端的后端实例之间分发通知,您的最佳想法是什么?

我目前正计划将每个 Action 都放入 SQL 数据库中,然后让每个客户端每秒轮询数据库以获取新 Action ,但这似乎很笨拙且效率低下...

最佳答案

每秒轮询一次是一种选择,但您可能需要考虑 long polling相反,减少延迟。

引用 Comet Daily: The Long-Polling Technique :

The long-polling Comet technique is a technique that optimizes traditional polling to reduce latency.

Traditional polling sends an XMLHttpRequest to the server in fixed intervals. For example, open a new XMLHttpRequest every 15 seconds, receive an immediate response, and close the connection.

Long-polling sends a request to the server, but a response is not returned to the client until one is available. As soon as the connection is closed, either due to a response being received by the client or if a request times out, a new connection is initiated. The result is a significant reduction in latency because the server usually has a connection established when it is ready to return information to return to the client.

关于php - 将 javascript 客户端协调到单个后端游戏的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3144837/

相关文章:

php - 如何用php写这个爬虫?

php - 正确编号 SQL 查询的一些技巧

Javascript 在 HTML 中设置 ID 的值

android - 从android中的GCM Intent Service无休止地获取通知

android - 如何在不显示通知的情况下更新未读计数

c# - 错误 : Namespace "iOS" does not exist in Unity. 通知

php - 如何使用 WAMP 测试发送邮件

javascript - 如何通过javaScript/jquery从php获取多个返回值?

javascript - c3.js。在具有相同类/ID 的多个 div 循环中创建图形?

javascript - 从辅助函数更新 DOM 后运行函数