Using torque to download info from a site is an extremely valuable feature, especially for modding.
You can download any file as text using torque with a TCPObject or an HTTPObject.
Let's say we want to download a file online to get the latest version for a mod.
First, create your object. I'm gunna use an HTTPObject for simplicity's sake.
I'll also put everything in a checkLatestVersion() function.
function checkLatestVersion()
{
new HTTPObject(getLatestVersion);
}
Now we want to connect. Before we do that, we need to figure out the file we're gunna download. Let's say it's google.com/latestVersion.txt.
When we connect, the server needs to be the first argument (in this case, it's google.com:80). I added :80 because that's the default port for http.
The file name is in a seperate argument (in this case, it's /latestVersion.txt).
Here's our new code:
funtion checkLatestVersion()
{
new HTTPObject(getLatestVersion);
getLatestVersion.get("google.com:80","/latestVersion.txt");
}
Okay. So now we're connected to the file and downloading.
By default, torque downloads one line at a time. To access each line, you need to create your object::onLine() function. It has 1 argument: newly downloaded lines.
function getLatestVersion::onLine(%this,%line)
{
if(firstWord(%line)) $= "Version")
{
%latestVersion=restWords(%line); //We have the latest version! Let's see if the client's version is old:
if($pref::currentVersion < %latestVersion)
{
update();
}
}
}
If you want to stay connected to a server, like if you wanted to connect to an IRC channel, you would use a TCPObject.
It works a little bit differently than the HTTPObject.
To connect, you use MyTCPobj.connect("url.com:80");
Then:
function MyTCPobj::onConnected(%this)
{
$file="myFileHere.txt";
$url="url.com:80";
%this.send("GET "@$file@" HTTP/1.0\nHost: " @$url@"\nUser-Agent: Torque\n\r\n\r\n");
}
And now you can finish off with the MyTCPobj::onLine() function, just like the HTTPobject one.
**NOTE**: When downloading files, torque does not read the last line, so it's important you include an extra line at the bottom.