Game Development Community

TCPobject Listen Method

by JuanMa · in Technical Issues · 04/18/2008 (4:33 pm) · 2 replies

I am trying to create a TCPobject that will be listening to the things that happen on an external server. The external server will send updates to my game and I will be making changes inside the game based on these. A PHP script will be in charge of sending the updates to my game.


Now the problem that I have is that I have no idea where to go or what to do after I have created my TCPobject.

function StartListening(%portNum){
      %obj = new TCPObject(myListener);   
      
      // use a random default port
      if(%portNum $= "")
         %portNum = "123";
      
      %obj.listen(%portNum);
               
      echo("listening trough port " @ %portNum); 
}

Is there any method that gets invoked once the TCPobject receives information from the outside? or do I have to mod the source code?

I know it is listening because once I call startListening() I can go to the CMD and do something like

telnet localhost 123

and I'll get:
Got bad connected receive event.

Any help in this matter is appreciated!

Thx

Juanma

#1
04/23/2008 (3:45 pm)
Well no one posted anything, but I got some results by looking the code posted on this link

www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=5457

this is my new code:

function StartServer(%portNum){
      %obj = new TCPObject(ChatServer);   
      
      // use default port
      if(%portNum $= "")
         %portNum = "2345";
      
      ChatServer.listen(%portNum);
               
      echo("listening for new connection in port: " @ %portNum); 
}

function ChatServer::onDisconnect(%this){
      %this.delete();
}
function ChatServer::onConnectRequest(%this, %address, %id){
   echo("got onConnected");   
   %client = new TCPObject(EXTSocket, %id);
}
//---------------------------------------------------------------
function EXTSocket::onLine(%this, %line){
   echo("On line ");
   EXTSocket.processLine(%line, %this);
}
function EXTSocket::processLine(%this, %line, %id){
   if(%line!$= "")
      echo("got \"" @ %line @ "\"");
   else
      echo("got nothing");
}

So what I am doing here is creating a new chatServer, the key is to have the
ChatServer::onConnectRequest(%this, %address, %id) method and create a new TCPobject for the client that is trying to get connected (EXTSocket or external socket). Then the the method onLine will be called. At this point I can go to telnet and do a connection

telnet localhost 2345
and anything that I type after that will be echo into the action log.

this is a bit raw, but a good start for what I want to do. If there is a better way to do it please let me know.

Juanma
#2
10/13/2009 (4:53 am)
thanks for your information, it's useful for me too, i'll try what you do.