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

发布于 2024-09-07 04:33:31 字数 241 浏览 0 评论 0原文

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

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

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?

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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

少女的英雄梦 2024-09-14 04:33:31

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

引用 Comet Daily:长轮询技术

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

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

长轮询向服务器发送请求,但在有可用响应之前不会将响应返回给客户端。一旦连接关闭(由于客户端收到响应或请求超时),就会启动新连接。结果是延迟显着减少,因为服务器通常在准备好返回信息以返回客户端时建立连接。

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

Quoting 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.

々眼睛长脚气 2024-09-14 04:33:31

不幸的是,对此只有几个选择。如果您想使用推送技术,所有这些都将涉及在服务器上让某种客户端发送通知。否则民意调查是可行的方法。

但正如切线所示,这是我几年前使用的一个解决方案: http ://www.spsolutionscorp.com/blog/2007/03/14/RealTimeUpdatesToWebPageUsingMacromediaFlash.aspx

要点是:打开服务代理的 Sql Server。服务器上使用 SqlDependency 侦听查询结果更改的 .Net 服务 (http://support.microsoft .com/kb/555893)。一旦发生更改,.net 服务就会循环访问我在上面的博客文章中所写的 Flash 客户端建立的所有连接。然后 Flash 应用程序可以调用页面上的 javascript。

这是一个疯狂的想法,我从未投入制作。但根据您的使用情况,可能值得研究一下,而且很容易上手。

但这又是一个完全疯狂的想法,而且我从未将其投入生产,因此使用风险自负。只是想我会分享。

Unfortunately there are only a few options for this. All will involve having some sort of client on the server sending down notifications if you want to use push technology. Or else polling is the way to go.

But just as a tangent here is a solution I played with years ago: http://www.spsolutionscorp.com/blog/2007/03/14/RealTimeUpdatesToWebPageUsingMacromediaFlash.aspx

The gist of it was this: Sql Server with service broker turned on. .Net service on server that is using SqlDependency to listen for changes to query result (http://support.microsoft.com/kb/555893). Once change occurred the .net service looped through all connections that had been made from a flash client that I wrote about in the above blog article. Then the flash app could call out to the javascript on the page.

It was a crazy idea and I never put into productions. But depending on your use it might be worth looking into and it was pretty easy to get going.

But again this was a totally crazy idea and again one I never put into production so use at your own risk. Just thought I would share.

蓝海 2024-09-14 04:33:31

无论您的请求和响应有多轻量级,轮询都可以轻松堆积并消耗大量资源(例如:带宽)。

http://ajaxpatterns.org/Periodic_Refresh

您可能想尝试使用 Comet/HTTP Streaming
http://ajaxpatterns.org/HTTP_Streaming

Polling can easily stack up and consume a lot of resources (eg: bandwidth), no matter how lightweight your requests and responses are.

http://ajaxpatterns.org/Periodic_Refresh

You might want to try and use Comet/HTTP Streaming
http://ajaxpatterns.org/HTTP_Streaming

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文