我编写的TcpLink类有问题。该类连接到数据库,并且可以接收来自echo语句的简单消息。但是,由于某些原因,TcpLink类不会发送参数。有人能帮我吗?
通过浏览器运行它会返回一个正确的结果:"array(1){["turbes"]=>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
}
这是因为Opened
方法总是在建立连接后发送查询字符串(即POST请求)。您的GET参数应该在TargetPath
中传递,这样它才能正常工作。