Torque 3D - Script Manual: HTTPObject Class Reference

TorqueScript

Main   Class List   Namespace List   Online

HTTPObject Class Reference
[Networking]

Allows communications between the game and a server using HTTP. More...

Inheritance diagram for HTTPObject:

List of all members.

Public Member Functions

void get (string Address, string requirstURI, string query="")
 Send a GET command to a server to send or retrieve data.
void post (string Address, string requirstURI, string query, string post)
 Send POST command to a server to send or retrieve data.

Detailed Description

Allows communications between the game and a server using HTTP.

HTTPObject is derrived from TCPObject and makes use of the same callbacks for dealing with connections and received data. However, the way in which you use HTTPObject to connect with a server is different than TCPObject. Rather than opening a connection, sending data, waiting to receive data, and then closing the connection, you issue a get() or post() and handle the response. The connection is automatically created and destroyed for you.

Example:
// In this example we'll retrieve the weather in Las Vegas using
// Google's API.  The response is in XML which could be processed
// and used by the game using SimXMLDocument, but we'll just output
// the results to the console in this example.

// Define callbacks for our specific HTTPObject using our instance's
// name (WeatherFeed) as the namespace.

// Handle an issue with resolving the server's name
function WeatherFeed::onDNSFailed(%this)
{
   // Store this state
   %this.lastState = "DNSFailed";

   // Handle DNS failure
}

function WeatherFeed::onConnectFailed(%this)
{
   // Store this state
   %this.lastState = "ConnectFailed";

   // Handle connection failure
}

function WeatherFeed::onDNSResolved(%this)
{
   // Store this state
   %this.lastState = "DNSResolved";

}

function WeatherFeed::onConnected(%this)
{
   // Store this state
   %this.lastState = "Connected";

   // Clear our buffer
   %this.buffer = "";
}

function WeatherFeed::onDisconnect(%this)
{
   // Store this state
   %this.lastState = "Disconnected";

   // Output the buffer to the console
   echo("Google Weather Results:");
   echo(%this.buffer);
}

// Handle a line from the server
function WeatherFeed::onLine(%this, %line)
{
   // Store this line in out buffer
   %this.buffer = %this.buffer @ %line;
}

// Create the HTTPObject
%feed = new HTTPObject(WeatherFeed);

// Define a dynamic field to store the last connection state
%feed.lastState = "None";

// Send the GET command
%feed.get("www.google.com:80", "/ig/api", "weather=Las-Vegas,US");
See also:
TCPObject

Member Function Documentation

void HTTPObject::get ( string  Address,
string  requirstURI,
string  query = "" 
)

Send a GET command to a server to send or retrieve data.

Parameters:
Address HTTP web address to send this get call to. Be sure to include the port at the end (IE: "www.garagegames.com:80").
requirstURI Specific location on the server to access (IE: "index.php".)
query Optional. Actual data to transmit to the server. Can be anything required providing it sticks with limitations of the HTTP protocol. If you were building the URL manually, this is the text that follows the question mark. For example: http://www.google.com/ig/api?weather=Las-Vegas,US
Example:
// Create an HTTP object for communications
%httpObj = new HTTPObject();

// Specify a URL to transmit to
%url = "www.garagegames.com:80";

// Specify a URI to communicate with
%URI = "/index.php";

// Specify a query to send.
%query = "";

// Send the GET command to the server
%httpObj.get(%url,%URI,%query);
void HTTPObject::post ( string  Address,
string  requirstURI,
string  query,
string  post 
)

Send POST command to a server to send or retrieve data.

Parameters:
Address HTTP web address to send this get call to. Be sure to include the port at the end (IE: "www.garagegames.com:80").
requirstURI Specific location on the server to access (IE: "index.php".)
query Actual data to transmit to the server. Can be anything required providing it sticks with limitations of the HTTP protocol.
post Submission data to be processed.
Note:
The post() method is currently non-functional.
Example:
// Create an HTTP object for communications
%httpObj = new HTTPObject();

// Specify a URL to transmit to
%url = "www.garagegames.com:80";

// Specify a URI to communicate with
%URI = "/index.php";

// Specify a query to send.
%query = "";

// Specify the submission data.
%post = "";

// Send the POST command to the server
%httpObj.POST(%url,%URI,%query,%post);


Copyright © GarageGames, LLC. All Rights Reserved.