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
Recent comments
4 weeks 1 day ago
5 weeks 3 days ago
6 weeks 4 days ago
6 weeks 5 days ago
7 weeks 5 hours ago
7 weeks 2 days ago
7 weeks 3 days ago
9 weeks 5 days ago
10 weeks 22 hours ago
10 weeks 23 hours ago