我是一名具有中级经验的程序员,我想创建一个联网游戏。我不能决定我应该为这个游戏挑选什么样的网络“模型”。
“模型”是指应该将信息从服务器传输到客户端以及处理实际发生的位置的方法。目前,这是我想到的模型:
现在,这是一款2人游戏,因此我预计服务器无法处理所有用户请求。我想听听您对此模型的反馈。好吗?如果没有,有哪些替代方案?
答案 0 :(得分:0)
你绝对应该阅读有关帝国时代如何运作的整个PDF文件。这完全相关,将有助于澄清您的想法:
http://web.cs.wpi.edu/~claypool/courses/4513-B03/slides/BT01.pdf
答案 1 :(得分:-2)
如果没有完整的图片,我想我会构建两个Web服务,连接到某个DB(最有可能)。
第一个接受一些与用户执行的操作相关的参数(点击坐标,时间,用户ID等),将这些参数保存到数据库中,并回复一些状态详细信息(成功,失败等)。
另一个API会接受时间作为参数,并返回(以JSON或XML格式)自那时以来发生的所有活动(如在聊天程序中...这里是您上次更新后的消息) ...或者,取决于游戏构造,累积状态......例如,如果游戏由两个团队从红色到蓝色翻转,则该API将返回该区块的当前状态。然后,在客户端代码中,我将处理响应以反映给该用户当前的游戏状态。
以下是一些示例代码。这不会按原样自动更新,您可以设置超时以定期轮询新数据,或设置某种长轮询(wiki)。哦,因为这很有教育意义,所以没有输入验证(除了篇幅):
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>Game</title>
<style>
html, body {margin:0;padding:0;height:100%;width:100%;}
#board {width:298px;height:298px;border:thin solid #000;position:relative;}
#puck {width:58px; height:58px; display:block; background-image:url('al.png'); background-size:contain; position:absolute; top:120px; left:120px;}
</style>
</head>
<body onload="update();">
<div id="board">
<div id="puck"></div>
</div>
<button onclick="update();">Update</button>
</body>
<script>
var x,y;
document.getElementById('board').addEventListener('click',move);
function move(e){
a1=((window.XMLHttpRequest)?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP"));
x=(e.clientX-29);
y=(e.clientY-29);
a1.abort();
a1.onreadystatechange=function()
{
if (a1.readyState==4&&a1.status==200){
document.getElementById('puck').style.left = x+'px';
document.getElementById('puck').style.top= y+'px';
}
}
params='x='+x+'&y='+y;
a1.open("POST","al_accept.php",true);
a1.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
a1.setRequestHeader("Content-length", params.length);
a1.setRequestHeader("Connection", "close");
a1.send(params);
}
function update(){
a2=((window.XMLHttpRequest)?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP"));
a2.onreadystatechange=function()
{
if (a2.readyState==4&&a2.status==200){
res=JSON.parse(a2.responseText)
x=res.x;
y=res.y;
document.getElementById('puck').style.left = x+'px';
document.getElementById('puck').style.top= y+'px';
}
}
params='x='+x+'&y='+y;
a2.open("POST","al_respond.php",true);
a2.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
a2.setRequestHeader("Content-length", params.length);
a2.setRequestHeader("Connection", "close");
a2.send(params);
}
</script>
</html>
你会看到move
函数将数据发送到存储它的al_accept.php
(这里只是写入文件,因为每次移动都会超越之前的任何内容)和update
函数调用al_respond.php
以检索最新数据。
al_accept.php:
<?php
$x=(strlen($_REQUEST['x'])>3)?substr($_REQUEST['x'],0,3):$_REQUEST['x'];
$y=(strlen($_REQUEST['y'])>3)?substr($_REQUEST['y'],0,3):$_REQUEST['y'];
$pos=fopen('al.txt','w');
$str='{"x":'.$x.',"y":'.$y.'}';
fwrite($pos,$str);
fclose($pos);
?>
al_respond.php:
<?php
header('content-type:application/json');
echo file_get_contents('al.txt');
?>
如果你在两台机器上打开它,你可以将球移到另一台机器上,当你在另一台机器上更新时,你会看到最新的位置(反之亦然)。