Today I came across an interesting article about creating multi-player video games using Ajax. The article mainly deals with how you would send data to and from the server and also touches on keeping the game in sync on multiple clients.
The below code is how the post purposes that you would send data to the server side (this code uses a hand rolled Ajax call, but you could change it to use a library).
var httpSend = null; var httpGet = null; function send(action) { httpSend=GetXmlHttpObject(); if (httpSend==null){alert (?Your browser does not support AJAX!?);return;} var url=?send.php?; url=url+??action=?+action; url=url+?&p=?+player; url=url+?&g=?+gameid; url=url+?&sid=?+Math.random(); httpSend.onreadystatechange=stateSend; httpSend.open(?GET?,url,true); httpSend.send(null); } function get() { if(httpGet != null) { return 0; } httpGet=GetXmlHttpObject(); if (httpGet==null){alert (?Your browser does not support AJAX!?);return;} var url=?get.php?; url=url+??sid=?+Math.random(); url=url+?&p=?+player; url=url+?&g=?+gameid; httpGet.onreadystatechange=stateGet; httpGet.open(?GET?,url,true); httpGet.send(null); } function stateSend() { if (httpSend.readyState==4){} } function stateGet() { if (httpGet.readyState==4) { str = httpGet.responseText; if(str != ?){ eval(str); } httpGet = null; } } function GetXmlHttpObject() { var xmlHttp=null; try { xmlHttp=new XMLHttpRequest(); } catch (e) { try { xmlHttp=new ActiveXObject(?Msxml2.XMLHTTP?); } catch (e) { xmlHttp=new ActiveXObject(?Microsoft.XMLHTTP?); } } return xmlHttp; }
The following code would be on the server side to receive messages. In the case of this example the code is PHP and the file is called Get.php
<?php include(?config.php?); $p = $_GET[?p’]; $g = $_GET[?g’]; $action = ?; $query = mysql_query(?SELECT * FROM actions WHERE id = ?.$g); $row = mysql_fetch_array($query); $action = $row[$p]; $action = stripslashes($action); $str = ?update actions set `?.$p.?` = ? where id = ?.$g; mysql_query($str); echo $action; ?>
The following code would be on the server side to send messages. In the case of this example the code is PHP and the file is called Send.php
<?php include(?config.php?); $p = $_GET[?p’]; $g = $_GET[?g’]; if($p == 1) { $b = 2; } elseif($p == 2) { $b = 1; } $action = $_GET[?action?]; $action = addslashes($action); $query1 = mysql_query(?select * from actions where id = ?.$g); $row = mysql_fetch_array($query1); $str = $row[$b]; $str = $str . $action; $str = ?UPDATE actions SET `?.$b.?` = ??.$str.?? where id = ?.$g; $query = mysql_query($str); if(!$query) { echo mysql_error().$str; } else { echo 0; } ?>
You can read the full post here
The post also touches on the possibility of sending data via a text string. I personally think that for multi-player games JSON is a perfect data transfer method. Unlike XML there is not a large overhead and unlike sending data via a text string there is no parsing needed on the client which should help performance. This post is interesting as I think that Ajax can be a very useful method of transferring data for multi-player games.
Delicious
Digg
StumbleUpon
Propeller
Reddit
Magnoliacom
Newsvine
Furl
Facebook
Google
Yahoo
Technorati
Icerocket
[...] Multi-player Ajax Games By David Hurth The below code is how the post purposes that you would send data to the server side (this code uses a hand rolled Ajax call, but you could change it to use a library). var httpSend = null; var httpGet = null; function send(action) … Ajaxonomy - The Study of Ajax… - http://www.ajaxonomy.com [...]
[...] Julie Dowd: [...]
[...] Julie Dowd: [...]
If you're interested in what is possible with AJAX as far as multi-player games are concerned, please have a look at my blackjack game.
The aim was to create a multi-player game in the browser without using flash or java or a software download. The front-end client is only about 33K to download to the browser!
It's nearing completion and I welcome people playing this demo and giving me some feedback. Still needs proper graphics adding, but it is certainly playable:
http://blackjack.webutils.co.uk
Post new comment