Web access from within engine? (HTTP)
by Vern Jensen · in Torque Game Builder · 05/21/2009 (11:01 am) · 4 replies
Before coming to Torque, I wrote a Mac game that retrieved data from my website via HTTP, to verify that a user's registration code was valid. It would send the user's name and code to the website, and the website would respond with a value indicating if the code is valid or not.
The code worked by using this URL:
http://www.actionsoft.com/mm/validate_code.php?name=
followed by the person's name, such as "Vern Jensen" but with spaces first converted to %20. This was then followed by "&code=" and the code, and then null-terminated.
All this I can easily enough do in TorqueScript. The thing is, there was this MacOS X function I used called URLSimpleDownload that sent that URL and got the result from the website.
Question is, how to do this in Torque Game Builder? I can do it in either TorqueScript or in the engine itself -- I have access to the source, and have already made mods. My question isn't how to create the URL -- that's easy enough. It's how to post it and get data back from the website. I have seen some code for something similar with high scores below, but don't particularly understand how it works. Can anyone explain it or point me to documentation? I am lost when it comes to the POST httpCmd line. This also does decimal-to-hex conversions and stuff... is that necessary for what I want to do???
http://www.garagegames.com/community/blogs/view/10202
Is there documentation for this anywhere? I tried going to the URL below and clicking on NetObject, but it brings up a blank page.
http://tdn.garagegames.com/wiki/TGB/Reference
The code worked by using this URL:
http://www.actionsoft.com/mm/validate_code.php?name=
followed by the person's name, such as "Vern Jensen" but with spaces first converted to %20. This was then followed by "&code=" and the code, and then null-terminated.
All this I can easily enough do in TorqueScript. The thing is, there was this MacOS X function I used called URLSimpleDownload that sent that URL and got the result from the website.
Question is, how to do this in Torque Game Builder? I can do it in either TorqueScript or in the engine itself -- I have access to the source, and have already made mods. My question isn't how to create the URL -- that's easy enough. It's how to post it and get data back from the website. I have seen some code for something similar with high scores below, but don't particularly understand how it works. Can anyone explain it or point me to documentation? I am lost when it comes to the POST httpCmd line. This also does decimal-to-hex conversions and stuff... is that necessary for what I want to do???
http://www.garagegames.com/community/blogs/view/10202
Is there documentation for this anywhere? I tried going to the URL below and clicking on NetObject, but it brings up a blank page.
http://tdn.garagegames.com/wiki/TGB/Reference
#2
Turns out that thanks to another resource I found on TDN, I got it working first try. Whoohoo! For anyone else curious, my code is:
It echoes this out to the console:
HTTP Post
- Requesting data at URL: /mm/validate_code.php?name=Vern%20Jensen&code=ABCDEFGHIJKLMNOP
Results: HTTP/1.1 200 OK
Results: Date: Thu, 21 May 2009 18:27:12 GMT
Results: Server: WebServerX
Results: X-Powered-By: PHP/5.2.6
Results: Connection: close
Results: Content-Type: text/html
Results:
Results:
Results: -1
Done getting data
It's that very last value, the -1, that I'm wanting to use. So looks like I'll just save the final returned line and use that. Cool to know I can get the current date this way too.
-Vern
05/21/2009 (11:31 am)
My needs are quite simple.Turns out that thanks to another resource I found on TDN, I got it working first try. Whoohoo! For anyone else curious, my code is:
function doWebTest()
{
// RSS ticker configuration:
$HttpPost::serverName = "www.actionsoft.com";
$HttpPost::serverPort = 80;
$HttpPost::serverURL = "/mm/validate_code.php?name=Vern%20Jensen&code=ABCDEFGHIJKLMNOP";
$HttpPost::userAgent = "Torque/1.4";
new TCPObject(HttpPostObject);
HttpPostObject.connect($HttpPost::serverName @ ":" @ $HttpPost::serverPort);
}
function HttpPostObject::onConnected(%this)
{
echo("HTTP Post");
echo(" - Requesting data at URL: " @ $HttpPost::serverURL );
// Reset some useful state information.
$HttpPost::lineCount = 0;
$HttpPost::requestResults = "";
// Request our data.
%this.send("GET " @ $HttpPost::serverURL @ " HTTP/1.0\nHost: " @ $HttpPost::serverName @ "\nUser-Agent: " @ $HttpPost::userAgent @ "\n\r\n\r\n");
}
// Necessary for RSS, but probably not for us...
function HttpPostObject::onLine(%this, %line)
{
// Collate info.
$HttpPost::lineCount++;
$HttpPost::requestResults = $HttpPost::requestResults @ %line;
echo("Results: " @ %line);
}
function HttpPostObject::onDisconnect(%this)
{
%this.delete();
echo("Done getting data");
}It echoes this out to the console:
HTTP Post
- Requesting data at URL: /mm/validate_code.php?name=Vern%20Jensen&code=ABCDEFGHIJKLMNOP
Results: HTTP/1.1 200 OK
Results: Date: Thu, 21 May 2009 18:27:12 GMT
Results: Server: WebServerX
Results: X-Powered-By: PHP/5.2.6
Results: Connection: close
Results: Content-Type: text/html
Results:
Results:
Results: -1
Done getting data
It's that very last value, the -1, that I'm wanting to use. So looks like I'll just save the final returned line and use that. Cool to know I can get the current date this way too.
-Vern
#3
01/08/2010 (6:35 pm)
Thanks for this Vern!
#4
01/08/2010 (7:13 pm)
You bet -- glad it's of use. :-)
Associate Orion Elenzil
Real Life Plus
it looks moderately undocumented in TDN.
it's also a fairly primitive class,
and if you're planning on doing a lot of heavy HTTP stuff (eg https or uploading binary data etc) you might consider integrating a 3rd-party library such as LibCURL.