TCPObjects Explained
by Robert Fritzen · 07/13/2011 (7:27 am) · 4 comments
At some point in a multiplayer game, you will want to implement some form of score keeping, or record holding that is "universal" to all game clients.
To do this, you need to have the clients all possess the same information. Torque makes the in game part of this easy, by providing the TCPObject, which allows you to make a raw socket connection to a server (can be any server).
So, let's say we want to connect to a basic PHP script, and then collect the output from it. To do so, let's first start on a web server, and throw together a nice little .php script.
Let's say the URL to this is: http://www.somewebsite.com/files/aphpscript.php
Now we want to connect to this file. First however, you need to understand the proper HTTP syntax. This is found here.
Sound confused yet? don't worry, because we're going to make this really easy.
Create a script file, and name it what you please, execute it in the game load. Add the following to it:
What this code does, is give us some really easy to use asset functions to assemble our HTTP requests. Now, let's connect to that little PHP script of ours. Create a second script file, and add the following code to it:
Using the asset code, we assemble a PHP POST request to the location of the page, notice I establish port 80 (HTML) to make the connection, but do not use http:// in the link.
The script automatically disconnects after 5 seconds to ensure no stray connections are kept, and to save use of memory, the object is deleted once it has disconnected.
You will want to spend time with the onLine method, as if you are handling connections to/from a server, this is where all of the server's responses will be handled. Also notice, that the HTTP header from the server is attached, so you need to find out a little way to get past this ;).
This is a pretty basic example. I'm sure others can expand on this topic if they know about it. I'm interested to see the routes others have taken to do the same thing. I don't know, maybe I have enlightened a few people out there with this, and maybe you can enlighten me. :P
To do this, you need to have the clients all possess the same information. Torque makes the in game part of this easy, by providing the TCPObject, which allows you to make a raw socket connection to a server (can be any server).
So, let's say we want to connect to a basic PHP script, and then collect the output from it. To do so, let's first start on a web server, and throw together a nice little .php script.
Let's say the URL to this is: http://www.somewebsite.com/files/aphpscript.php
<?php $var1 = $_POST["var1"]; $var2 = $_POST["var2"]; echo "You gave me: ".$var1." and ".$var2; ?>
Now we want to connect to this file. First however, you need to understand the proper HTTP syntax. This is found here.
Sound confused yet? don't worry, because we're going to make this really easy.
Create a script file, and name it what you please, execute it in the game load. Add the following to it:
function getRandomSeperator(%length)
{
%alphanumeric = "abcdefghijklmnopqrstuvwxyz0123456789";
for (%i = 0; %i < %length; %i++)
{
%index = getRandom(strLen(%alphanumeric));
%letter= getSubStr(%alphanumeric, %index, 1);
%UpperC = getRandom(0, 1);
if (%UpperC)
%letter = strUpr(%letter);
else
%letter = strLwr(%letter);
%seq = %seq @ %letter;
}
return %seq;
}
function makeDisposition(%seperator, %name, %content, %isEnd)
{
if (%isEnd)
%dispo = "--" @ %seperator @ "\r\nContent-Disposition: form-data; name=""@%name@""\r\n\r\n"@%content@"\r\n--" @ %seperator @ "--";
else
%dispo = "--" @ %seperator @ "\r\nContent-Disposition: form-data; name=""@%name@""\r\n\r\n"@%content@"\r\n";
return %dispo;
}
function makeHeader(%cmd, %get, %host, %userAgent, %extra)
{
%header = %cmd SPC %get SPC "HTTP/1.1\r\n" @
"Host: "@%host@"\r\n" @
"User-Agent: "@%userAgent@"\r\nConnection: close\r\n" @
%extra;
return %header;
}
function isset(%v)
{
return (%v !$= "");
}What this code does, is give us some really easy to use asset functions to assemble our HTTP requests. Now, let's connect to that little PHP script of ours. Create a second script file, and add the following code to it:
function DoConnect(%var1, %var2) {
%host = "www.somewebsite.com"; //DO NOT USE HTTP://
%this = new TCPObject("MyTCPConnection");
%this.v1 = %var1;
%this.v2 = %var2;
%this.connect(%host @ ":80"); //make the connection
%this.schedule(5000, "disconnect"); //disconnect after 5 seconds
}
function MyTCPConnection::onConnected(%this) {
%location = "/files/aphpscript.php";
%host = "www.somewebsite.com";
%seperator = getRandomSeperator(16);
%header = makeHeader("POST", %location, %host, "MyGame", "Content-Type: multipart/form-data; boundary="@%seperator@"\r\n");
%dispo = makeDisposition( %seperator, var1, %this.v1);
%dispo = %dispo @ makeDisposition( %seperator, var2, %this.v2, 1);
%header = %header @ "Content-Length:" SPC strLen(%dispo) @ "\r\n\r\n";
%request = %header @ %dispo;
%this.send(%request);
}
function MyTCPConnection::onLine(%this, %line) {
echo(%line); //EDIT: missed a ';'
}
function MyTCPConnection::onDisconnect(%this) {
echo("All Done!");
%this.delete();
}
function MyTCPConnection::onConnectFailed(%this) { error("Connection failure"); }
function MyTCPConnection::onDNSFailed(%this) { error("DNS failure"); }Using the asset code, we assemble a PHP POST request to the location of the page, notice I establish port 80 (HTML) to make the connection, but do not use http:// in the link.
The script automatically disconnects after 5 seconds to ensure no stray connections are kept, and to save use of memory, the object is deleted once it has disconnected.
You will want to spend time with the onLine method, as if you are handling connections to/from a server, this is where all of the server's responses will be handled. Also notice, that the HTTP header from the server is attached, so you need to find out a little way to get past this ;).
This is a pretty basic example. I'm sure others can expand on this topic if they know about it. I'm interested to see the routes others have taken to do the same thing. I don't know, maybe I have enlightened a few people out there with this, and maybe you can enlighten me. :P
About the author
Illinois Grad. Retired T3D Developer / Pack Dev.
#2
08/03/2011 (6:48 am)
erm, this resource is really useful, this tutorial is about send the value to php, but how do i send the value from php back to the game? waiting for your reply.
#3
This is actually quite simple, and can be done from the same script.
Use the OnLine Method to detect the returned values from PHP.
09/29/2011 (8:33 am)
Wow, I need to pay more attention to my resources....This is actually quite simple, and can be done from the same script.
Use the OnLine Method to detect the returned values from PHP.
#4
11/27/2011 (8:42 am)
Does TCPObject provide any way to create connections using SSL? 
Torque Owner D Fasel
Game 3D