簡體   English   中英

使用Cordova,Javascript,PHP,MySQL進行實時多人游戲-幫助和提示

[英]Real-time multiplayer game using Cordova, Javascript, PHP, MySQL - Help & Tips

我正在嘗試使用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帖子

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

<?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-polling Long-polling實際上是一種黑客手段,可以部分規避HTTP中缺少雙向通信的問題。 它包括客戶端向服務器發送請求,以及服務器保持連接打開直到有什么要響應的內容。 這也不是最佳選擇,因為服務器可能會用完套接字。

現在,為了獲得良好的實時服務(或游戲),您可以使用一些專門針對實時的協議。 輸入WebSocketsWebRTC

  1. WebSocket是服務器和客戶端之間的持久性雙工(雙向)連接,允許客戶端向服務器發送和接收更小的“請求”(基本上僅是特定於應用程序的數據)。 與HTTP不同,這是一個持久連接,這意味着您無需隨每個請求發送附加數據(例如標頭)。 另一個很棒的事情是,所有主流瀏覽器都支持WebSocket,並且WebSocket正在成為實時通信的實際標准。

  2. WebRTC-這是對等連接協議,您可以在其中連接到其他客戶端進行通信,而不是直接連接到服務器。 這幾乎意味着您的播放器可以直接彼此連接,並且由於它們不會連接到集中式位置(這可能會遭受性能問題或網絡性能下降),因此連接甚至更快。 大多數現代瀏覽器都支持它,而IE緊隨其后。

因此,這些是創建實時服務/應用程序的最佳選擇。 我建議使用WebSockets,因為它更加成熟並且目前采用率更高。 如果您有使用PHP的想法,則可以簽出一些可用於處理Websocket的現成庫,例如http://socketo.me/。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM