将 javascript 客户端协调到单个后端游戏的最佳方法是什么? [英] What's the best way to coordinate javascript clients to a single backend game?

查看:73
本文介绍了将 javascript 客户端协调到单个后端游戏的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

What's the best method to use to notify javascript client's of changes that occur in a game asynchronously (i.e. moves made by other clients). As an example, assume a turn based board game. Should I just have the client poll the PHP backend every second or so for new moves, or is there a better way to send an asynchronous notification to the other clients in the same game? What is your best idea for how to distribute the notification between the backend instances for each client?

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

I'm currently planning on putting each move in an SQL database and then having each client poll the database for new moves every second, but this seems kludgy and inefficient...

推荐答案

每隔一秒左右轮询一次是一种选择,但您可能需要考虑 长轮询,以减少延迟.

Polling every second or so is one option, but you may want to consider long polling instead, to reduce the latency.

引用 Comet Daily:长轮询技术:

长轮询 Comet 技术是一种优化传统轮询以减少延迟的技术.

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

传统轮询以固定时间间隔向服务器发送 XMLHttpRequest.例如,每 15 秒打开一个新的 XMLHttpRequest,收到立即响应,然后关闭连接.

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.

这篇关于将 javascript 客户端协调到单个后端游戏的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆