Game Development Community

Opening a Socket Connection

by Netwyrm · in Torque Game Builder · 09/27/2009 (1:23 am) · 0 replies

I have a server I wrote in Java, to which I have offloaded all the database work I use to maintain world and account persistence. The server performs all the interface work needed to talk to a backend database, and maintains a listening socket, accepting connections, and responding with requested bits of information on demand.

This is a subset of the socket-handling code I wrote in TorqueScript to handle communications back and forth to that server.

// Uncomment the echo comments if you would like to see when something occurs by checking 
// console window output for your event message.

//---------------------------------------------------------------------------------------------

// The address of the server which will handle the connection, concatenated with the port number
$hostaddress = "XXX.XXX.XXX.XXX:XXXX";
%socket = "";

// Create an object called MySocketConnection, unless an object of that name already exists.
if (!isObject(MySocketConnection))
{
   //echo ("Creating a TCPObject for later use");
   %socket = new TCPObject(MySocketConnection);
}
   
// This function is a callback, it is called by TGB when the connection is established
// It does not have to send something to the server at this point, but you can send information
// to establish a session on the server, if your server is designed to handle such a thing.
function MySocketConnection::onConnected(%this)
{
   //echo ("CONNECT!");
   // Send message content + EOL
   %this.send("Message to Server" @ "n");
}

// This is the function to call from other places in your code when a message is to be sent to the server.
// All it does is make sure the string being sent has an EOL character, so the server can tell where the message
// ends and it is to begin processing the message.
function MySocketConnection::sendMessage(%this,%msgToWrap)
{
   //echo (%msgToWrap);
   %this.send(%msgToWrap @ "n");
}

// This is the function which interprets what is sent back from the server. %line holds the value of the message
// returned, essentially everything the server sent back before the EOF. This function will be called every time
// the server sends any data terminated by EOF.
function MySocketConnection::onLine(%this,%line)
{
   //echo ("RECEIVED " @ %line);
   if (strlen(%line) > 0) {
         
      // This is where you would place your message handling switch (if your "%line" is a string value, 
      // otherwise, use the numeric switch form)
      switch$ (%line) {
         case "SOMETHING":
            dosomething(%line);
         case "OTHERTHING":
            dootherthing(%line);
         default:
            echo "GOT A MESSAGE: " @ %line @ " but did not understand it!";
      }
      
   }
         
}

One must include the socket-handling source file so the functions are available when main.cs begins the work of the game. The entity representing the socket is created when the file is first loaded by newTCPObject().

The initial call for the connection is made, in my case, in the main.cs file, because I establish a socket connection with the intent it remain open for the length of the client session (it is faster than opening, closing, and reopening a socket for each single request).

The global address stored in $hostaddress in the example file is picked up and used in the connection call, which is as simple as this statement:

MySocketConnection.connect($hostaddress);

Please note that this code is of little use unless you have a server on the other end of your connection capable of understanding and responding in proper fashion to your calls!

About the author

My adventures in T3D are chronicled at http://www.worldofantra.com. Please be aware the subject is sword-and-sorcery, and the occasional bloody or bare body part may be in scope.