用于通知 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/