我正在尝试使用Cordova实时创建一个简单的游戏(比如Agar.io):Javascript、AJAX、PHP和MySQL。
代码运行良好:
1)JS发送X和Y到PHP服务器
2)PHP将X和Y保存到MySQL数据库
3)PHP从MySQL数据库中检索X和Y
4)PHP帖子X和Y
5)JS检索X和Y的PHP post
6)在检索到的X和Y坐标处显示圆
现在是问题。。
太慢了!整个过程大约需要1秒才能完成(延迟1秒)。而且,有时它甚至落后于2-10秒!
我知道这种方法不是最快的,但我仍然觉得我可能做了错事或效率低下。
帮助
也许这是最有效的方法,但我觉得我做错了什么。或者也许有更好的方法?
另外,我正在使用HostMonster,共享帐户(也许这就是问题所在?),我正在等待它们打开,以便升级到专用服务器并在那里进行尝试。
注意:同时,我真的尽量避免使用Node.js/Websockets,因为服务器非常昂贵。免费的也很有限=/
更糟的是,我将学习更多JS PHP MySQL:)
JAVASCRIPT

function update
{
    ctx.clearRect(0, 0, c.width, c.height);
    ctx.drawImage(test, positionX - c.width/40, positionY - c.width/40, c.width/20, c.width/20);

    if((directionX != positionX || directionY != positionY) &&
        dataServerSendOn && dataServerGetOn)
    {
        dataServerSendOn = false;
        dataServerGetOn = false;

        setTimeout(function(){requestAnimationFrame(sendData);}, FPS);
    }

    requestAnimationFrame(update);
}

function sendData()
{
    var params = "uuid=" + uuid + "&directionX=" + directionX + "&directionY=" + directionY;

    http.open("POST", url, true);

    //Send the proper header information along with the request
    http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

    http.onreadystatechange =
    function()//Call a function when the state changes.
    {
        if(http.readyState == 4 && http.status == 200)
        {
            dataServerSendOn = true;
            requestAnimationFrame(getData);
        }
    }
    http.send(params);
}

function getData()
{
    xmlhttp.onreadystatechange =
    function()
    {
        /*
         * 0: Hasn't Started
         * 1: Connected to the Server
         * 2: Server has received our request
         * 3: Server Processing
         * 4: Request is finished and data is ready
         */
        if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
        {
            httpTransferString = xmlhttp.responseText;
            requestAnimationFrame(sortData);
        }
    }


    xmlhttp.open("GET", "http://www.example.com/game", true);
    xmlhttp.send();
}

function sortData()
{
    positionX = "";
    positionY = "";

    for(var i = 0; httpTransferString[i] != '&'; i++)
    {
        positionX += httpTransferString[i];
    }
    for(var i = positionX.length + 1; httpTransferString[i] != '#'; i++)
    {
        positionY += httpTransferString[i];
    }
    dataServerGetOn = true;
}

菲律宾比索
<?php
require 'core.php';
require 'connect.php';

if(loggedin())
{
    if(isset($_POST['uuid']) &&
       isset($_POST['directionX']) &&
       isset($_POST['directionY']))
    {
        $uuid       = $_POST["uuid"];
        $directionX = $_POST["directionX"];
        $directionY = $_POST["directionY"];

        $query = "SELECT `UUID` FROM `users` WHERE `UUID`='$uuid'";
        if($query_run = mysql_query($query))
        {
            $queryX = "UPDATE `users` SET `x` = '$directionX'  WHERE `uuid`='$uuid'";
            $query_runX = mysql_query($queryX);
            $queryY = "UPDATE `users` SET `y` = '$directionY'  WHERE `uuid`='$uuid'";
            $query_runY = mysql_query($queryY);
        }
    }
}
else
{
    echo "You're not logged in";
}

?>
<form name="tcpForm" action="<?php echo $current_file; ?>" method="POST">
UUID       <input type="text" name="uuid">      <br>
directionX <input type="text" name="directionX"><br>
directionY <input type="text" name="directionY"><br>
<input type="submit" value="Send">
</form>

请给我任何建议,我将不胜感激,我仍然是个傻瓜

最佳答案

对于实时应用程序,HTTP并不是最佳选择,因为它不提供完全的双向通信。这意味着,每当服务器端发生更改时,将无法通知客户端。客户端需要发送大量请求,这会给服务器带来很大的负载,或者使用long-pollingLong-polling是一种方法,实际上更像是一种黑客,可以部分地避免HTTP中缺乏双向通信。它包括客户端向服务器发送请求,服务器保持连接打开,直到有东西要对客户端作出响应。这也不是最优的,因为服务器可能会耗尽套接字。
现在对于一个好的实时服务(或游戏),您可以使用一些专门针对实时的协议。输入WebSocketsWebRTC
WebSockets是服务器和客户机之间的持久双工(双向)连接,它允许客户机向服务器发送和接收更小的“请求”,基本上只有特定于应用程序的数据。与HTTP不同,这是一个持久连接,这意味着您不需要随每个请求发送额外的数据(例如,headers)。另一个好消息是WebSockets在所有主流浏览器中都得到了支持,并且目前正在成为实时通信的事实标准。
WebRTC—这是一种对等连接协议,您可以连接到其他客户端进行通信,而不是直接连接到服务器。这几乎意味着你的玩家将直接连接到彼此,这将更快,因为他们将不会连接到一个集中的位置,这可能会遭受性能问题或网络降级。大多数现代浏览器都支持IE。
因此,这些是创建实时服务/应用程序的最佳可用选项。我建议使用WebSockets,因为它现在已经比较成熟,并且有了更大的应用。如果你有决心使用PHP,你可以签出一些现成的库来使用websockets,比如http://socketo.me/

10-05 21:05
查看更多