如何在客户端上执行相同的JS? [英] How to execute the same JS is more than on client?

查看:74
本文介绍了如何在客户端上执行相同的JS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了一个基于网络的connect4游戏,但是我遇到了问题.

在单人游戏模式下,它工作得很好,在多人游戏模式下,我设法更新了电路板,将状态(每个插槽为0透明,1-黄色,2-红色)保存为XML文件(通过AJAX),然后更新了电路板也可以通过AJAX在0.5秒内显示图像,以便其他玩家看到对手的移动.

问题是我要做的就是更新映像,但是我也需要更新阵列.当玩家移动时,将执行以下javascript代码:

I developed a web based connect4 game, but I have a problem.

In single player mode it works just fine and for multiplayer mode I managed to update the board saving the state (0-clear, 1- yellow, 2-red for each slot) into a XML file (via AJAX) and then updating the board image within 0.5s also via AJAX so the other player sees the move of his opponent.

The problem is that all I do is update the image, but I need to update the array as well. When a player made his move this javascript code is executed:

arrayPecas[coluna] = arrayPecas[coluna] + 1;


我知道JS是客户端,并且在本地处理,但是我需要在其他播放器的浏览器中更新该数组.

我应该怎么做?

在此先感谢!


I know JS is client side and it''s processed locally, but I need to update that array on the browser of other player.

How should I do the trick?

Thanks in advance!

推荐答案

使用ajax.您的客户是观察者,是一个可观察的服务器端页面.可观察者可以执行任何服务器端进程并更新观察者.观察者不断地使用javascript的setInterval方法来监听可观察对象.

这里说明了这种模式的一个例子.

Web应用程序(JavaScript)中的观察者模式:带有AJAX示例的演练 [
Use ajax. Your clients are observers and a server side page which is observable. The observable do any serverside process and update the observers. The observers listen continuously to the observable by the javascript''s setInterval method.

An example of this patterns is explained here.

Observer Pattern in web applications (JavaScript): A walkthrough with an AJAX example[^]

This article has both observer and observable at client side. But you can move observable to a serverside. Try!


您还可以使用使用ASP.NET AJAX 4.0进行实时数据绑定"
每当玩家使用Ajax更新"arrayPecas"时,您都调用服务器来更新其他玩家

您可以在这里找到示例:

http://msdn.microsoft.com/en-us/magazine/ee309508.aspx [ ^ ]



http://dotnetslackers.com/article/ajax/Live-Data-Binding-using-ASP-NET-AJAX-4-0-Preview-4.aspx [
You can also use "Live Data binding using ASP.NET AJAX 4.0"
each time a player update the "arrayPecas", using Ajax , you call the server to update the others players

you can find exemples here :

http://msdn.microsoft.com/en-us/magazine/ee309508.aspx[^]

and

http://dotnetslackers.com/articles/ajax/Live-Data-Binding-using-ASP-NET-AJAX-4-0-Preview-4.aspx[^]


这篇关于如何在客户端上执行相同的JS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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