1

我编写的 TcpLink 类有问题。该类连接到数据库,并且可以从 echo 语句接收简单的消息。但是,由于某种原因,TcpLink 类不会发送参数。有人可以帮帮我吗?

通过浏览器运行它会返回正确的结果: "array(1) { ["turtles"]=> string(3) "yes" }

但是,所有 UnrealScript 返回的是“array(0) { }”

PHP代码:

<?php
$response = "";
$response = var_dump($_GET);
echo $response;
?>

虚幻脚本代码:

    class PlayerStatsLinkClient extends TcpLink;

var string TargetHost;
var int TargetPort;
var String TargetPath;
var String RequestText; //Data To Send
var array<String> RequestQueue; //Data Waiting to be sent(used when request is already in progress)
var bool bRequestInProgress; //Currently Sending A Request

var PlayerController PC;

var bool bGetData;

var String RequestType;

//var TestDatabaseServices ServiceProvider; //Owner Service
var int SecurityKey; //Arbitrary number equivalent to what is in .php to check authority(Prevent hacking slightly[Easily hackable])

struct sParam
{
    var String Key;
    var String Value;
};

var array<sParam> ParamList;

delegate OnResponse(String Type, String Data);

function AddParam(string Key, string Value)
{
    local sParam param;

    param.Key = Key;
    param.Value = Value;

    ParamList.AddItem(param);
}

function SetStats(int level, int xp, int xpToNext){
    AddParam("level", string(level));
    AddParam("xp", string(xp));
    AddParam("xpToNext", string(xpToNext));    
}

function GetStatsFromServer(){
    bGetData = true;
    AddParam("COMMAND", "RETRIEVE");
    Execute();
    bRequestInProgress = true;
    `log("========== END GETSTATSFROMSERVER()");
}

function bool Execute()
{
    if(!SetRequest())
    {
        return false;
    }

    Resolve(targethost);

    bRequestInProgress = true;
    return true;
}

function bool SetRequest(optional String Database)
{
    local sParam param;
    local String tmpRequestText;

    tmpRequestText="securitykey="$SecurityKey;

    foreach ParamList(param)
    {
        tmpRequestText $= "&" $ param.Key $ "=" $ param.Value;
    }

    ParamList.Remove(0, ParamList.Length);

    if(!bRequestInProgress)
    {
        RequestText = tmpRequestText;
        return true;
    }
    else
    {
        RequestQueue.AddItem(tmpRequestText);
        return false;
    }
}

event Resolved( IpAddr Addr )
{
    `log("++++++++++++++ RESOLVED!");
    Addr.Port = TargetPort;

    BindPort();
    if (!Open(Addr))
    {
        `Log("[TcpLinkClient] Open failed");
    }
}

event ResolveFailed()
{
    `Log("[TcpLinkClient] Unable to resolve "$TargetHost);
}

function SetRequestText(string text){
    RequestText = text;   
}

function SetbGetData(bool delta){
   bGetData = delta;   
}

event Opened()
{  
    RequestText = "securitykey=REMOVED";
    //let the php file know what's coming
    if(bGetData){
        SendText("GET /"$TargetPath$" HTTP/1.0"$chr(13)$chr(10));
    }else{
        SendText("POST /"$TargetPath$" HTTP/1.0"$chr(13)$chr(10));
    }
    SendText("Host: "$TargetHost$chr(13)$chr(10));
    SendText("User-Agent: HTTPTool/1.0"$Chr(13)$Chr(10));
    SendText("Content-Type: application/x-www-form-urlencoded"$chr(13)$chr(10));
    SendText("Content-Length: "$len(RequestText)$Chr(13)$Chr(10));
    SendText(chr(13)$chr(10));
    //send securitykey, command and params for php to use
    SendText(RequestText);
    SendText(chr(13)$chr(10));
    SendText("Connection: Close");
    SendText(chr(13)$chr(10)$chr(13)$chr(10));
}

event Closed()
{
    if(RequestQueue.Length <= 0)
    {
        bRequestInProgress=false;
    }
    else
    {
        RequestText = RequestQueue[0];
        RequestQueue.Remove(0,1);
        Resolve(targethost);
    }
}

event ReceivedText( string Text )
{  
    Text = Split(Text, chr(13)$chr(10)$chr(13)$chr(10), true);
    WorldInfo.Game.Broadcast(self, "RECEIVED RESPONSE FROM PLAYERSTATS SERVER: " $ Text);
    `log("++++++++++++++++++++");
    `log(Text);
//    if(ServiceProvider != none)
//    {
//        ServiceProvider.RecieveDatabaseText(Text);
//    }
}

defaultproperties
{
    TargetHost="REMOVED.zzl.org"
    TargetPort=80
    TargetPath = "REMOVED.php"

    bGetData=true

    SecurityKey=REMOVED//used in PHP to authenticate
}
4

1 回答 1

1

这是因为您的Opened方法总是在建立连接后发送查询字符串(即 POST 请求)。应该传入您的 GET 参数TargetPath以使其正常工作。

于 2013-02-19T19:43:35.450 回答