Author Topic: I know you can! Post and get stuff from web/SQL ingame.  (Read 2159 times)

The RTB mod does it definitly, and so do the projects to get a list of IDs.
So can anyone teach me a little, before I go and look at the source of RTB?

Well, you use TCPObjects to connect to the internet.  Here is an example of creating one.

Code: [Select]
new tcpObject(blocklandConnect);
Now that we have the object, we need something to connect to.  Lets say Blockland, for instance.

Code: [Select]
$connectionAddress = "blockland.us:80";
We define this as a global variable so it can be used again later.

Now, lets define some basic functions for our tcp object.  We like to have some output as to how its going.

Let's start with the ::onConnect method.  It has no arguments.

Code: [Select]
function blocklandConnect::onConnect()
{
      echo("blocklandConnect: Connected!");
}

This is for whenever a connection sucessfully connects to the server.  echo("string"); outputs whatever is in that string to the console, which is useful for knowing when things happen, without doing anything complicated.  We also define functions using the syntax
Code: [Select]
function functionName(%argument,%arguments,%argumenta)
{
      //stuffHere
}

Next, we will do ::onConnectFailed().  It's when it fails to connect to a server for any reason.  I believe it has no arguments.

Code: [Select]
function blocklandConnect::onConnectFailed()
{
      echo("blocklandConnect: Connection FAILED!");
}

One last thing thats also good to know, ::onLine(%line).  %line is an argument because we receive data back from the server.

Code: [Select]
function blocklandConnect::onLine(%line)
{
      echo("BLC: " @ %line);
}

Servers usually send back headers when dealing with web pages.

There is also ::onTimeout for when a connection times out, and ::onDisconnect for whenever it disconnects.

Now, let's start connecting.

Code: [Select]
blocklandConnect.connect($connectionAdress);
This connects us to the address we defined as $connectionAdress.  You should receive a message in console saying "blocklandConnect: Connected!".  Now, we are connected.  We should send something to the server.  We do this by calling the .send() method on it like .connect().  After taking a look at RTB, I found that they sent
Code: [Select]
"GET /blockland/rtbModServer.php?c=GETFILEDL&n="@urlEnc($Pref::Player::NetName)@"&arg1="@%this.SO.file_id@" HTTP/1.1\r\nHost: returntoblockland.com\r\nUser-Agent: Torque/1.0\r\n\r\n"
Now, this is a bit more than we need, so lets trim it down.  GET is something that tells the server that we're requesting a page.  There is aditionally POST, and probably others.  I do not know, I don't work with TCPObjects often.  Next we see /blockland/rtbModServer.php?c=GETFILEDL&n="@urlEnc($Pref::Player::NetName)@"&arg1="@%this.SO.file_id@" .  Well, this is the file we are requesting.  You could basically just say /textfile.txt if you wanted to request a text file.  Next is some stuff I don't know what to call, really.  In this case, we use HTTP/1.1\r\nHost:  -  I know that all of this is nessecary, and has to do with something important.  Where you see returntoblockland.com, you replace with the host.  From what we're working on, we use blockland.us because we're connected to blockland.us. Next is \r\nUser-Agent: Torque/1.0\r\n\r\n - we use this to tell our User-Agent and also have more escape characters.  I'm not one to say what the escape characters are, but you can leave this part alone.  Now, lets go make our connection string.

Code: [Select]
$connectionString = "GET /index.php HTTP/1.1\r\nHost: blockland.us\r\nUser-Agent: Torque/1.0\r\n\r\n";
If you listened to what I said, you should understand the important parts you should change.  Now, this sends a request for the page "index.php" - the main page.  You then use
Code: [Select]
blocklandConnect.send($connectionString);
to send the request for the page.  The server then returns the stuff (which can also be based on user agent, useful) in console from the onLine() method.  Now that we have that stuff, we can disconnect.

Code: [Select]
blocklandConnect.disconnect();
If you need any additional clarification, help, or anything to do with this, just ask.  I might be wrong on certain things, so do ask for help from other people like Ephialtes, as they may be more knowledgeable to this.


You should be knowledgeable enough with the PHP server-side code to not need help (your site seems good).  Also, don't be afraid to experiment.  You may mess something up, but that's just how you should learn - from your mistakes.  I really think you may know alot of this stuff, and some stuff you can do.  Best of luck with this,
               Azimuth
« Last Edit: June 26, 2009, 12:38:38 PM by Azimuth »

So this code:

Code: [Select]
new tcpObject(blocklandConnect);
$connectionAddress = "blockland.us:80";
blocklandConnect::onConnect()
{
echo("Connected!");
}
blocklandConnect::onConnectFailed()
{
echo("Failed!");
}

function serverCmdconzs(%client, %message)
{
blocklandConnect.connect($connectionAdress);
}


Will try to connect to this site. And if suscussful will say connected and if not say failed!

And, Thanks alot!
« Last Edit: June 26, 2009, 11:59:52 AM by Azjherben »

Exactly.  You can change the name of it from blocklandConnect to whatever you like.

I changed it to do it when I sai "conzs", testing it now.





Error on line four?
« Last Edit: June 26, 2009, 12:05:24 PM by Azjherben »

I think its blockland.us:8080

Also, you missed 2 function declarations.
« Last Edit: June 26, 2009, 12:10:11 PM by Jaydee »

8080 is a proxy server port for a start, and you only need to specify the web port (80) in the connect function, not the send one.

8080 is a proxy server port for a start, and you only need to specify the web port (80) in the connect function, not the send one.

Ah.

I think its blockland.us:8080

Also, you missed 2 function declarations.


What 2?

It leaves off the N on new, so I think it might be capitalized as that is what happens to me when I capitalize the F on function or whatever.

8080 is a proxy server port for a start, and you only need to specify the web port (80) in the connect function, not the send one.
Can you tell me how accurate I was on how I explained it, anything I missed?

Still has an error with the N in new capitalized.
And I don't care about port 8080. I need to get it to work basically first.

Ooops, in my code I messed up.  Behind blocklandConnect::onConnect() and blocklandConnect::onConnectFailed() you need to add function.  I'm going to update my original code to reflect this.

You also need a %this for each first arg in all the blocklandConnect functions.

Edit: What would be the issue if I specify it to get a file and it returns me with a 301 error giving me a link to the exact same file I tried to get?