Author Topic: Help with TCPObject.....  (Read 499 times)

So I searched google and I didn't really find much help with this, and the torque functions guide only has a very limited reference of it, so could someone explain to me how to use a TCPObject to connect to a server, and send and receive packets from a server. (or point me to a guide that will explain how)

So far I assumed this:
Code: [Select]
$DrenDranChat = new TCPObject();

echo("Hello.");
$DrenDranChat.connect("66.240.176.166:4526");
$DrenDranChat.send("SYJ Hello.");
$DrenDranChat.delete();
echo("Good bye.");
It didn't work.

The reason your snippet won't work is because connecting isn't instantaneous. You have to wait to send data until after it is connected which of course is a callback function. Another problem would be that the send function also isn't instantaneous so you're deleting the tcp object whilst its trying to send data.
Try this:
Code: [Select]
new TCPObject(DrenDranChat);

echo("Hello.");
DrenDranChat.connect("66.240.176.166:4526");
function DrenDranChat::onConnected(%this)
{
$DrenDranChat.send("SYJ Hello.");
}
function DrenDranChat::onLine(%this,%line)
{
//we've received word from the server that they heard what we said
DrenDranChat.disconnect();
echo("Good bye.");
}
function DrenDrenChat::onDisconnect(%this)
{
%this.delete();
}
The onlIne part would only be used if you actually needed helllo goodbye functionality, otherwise you would just leave the object connected to the chat.
« Last Edit: July 08, 2010, 11:09:17 PM by Destiny/Zack0Wack0 »

Thank you, I shall try that out now.

Can I ask a question? Of course I can. Where is this sending to? I don't understand. You send "SYJ Hello" but where does it echo to? Like... a file? Sorry if you don't understand.

Code: [Select]

echo("DDCSIM Loaded.");

function SYJ_DD_CSIM::Print(%text)
{
echo("SYJ-DrenDran : "@%text);
}

SYJ_DD_CSIM::Print("Starting.");

function SYJ_DD_CSIM__chat__main::onConnected(%this)
{
SYJ_DD_CSIM::Print("Connected to chat server.");
SYJ_DD_CSIM__chat__main.send("SYJ ATH 1");
}

function SYJ_DD_CSIM__chat__main::onLine(%this,%line)
{
SYJ_DD_CSIM::Print("Recived:" SPC %line);
}

function SYJ_DD_CSIM::OpenChat()
{
SYJ_DD_CSIM::Print("Connecting to chat server.");
new TCPObject(SYJ_DD_CSIM__chat__main);
SYJ_DD_CSIM__chat__main.connect("127.0.0.1:4526");
}

   %b = new GuiBitmapButtonCtrl(SYJ_DD_CSIM_MainMenuButton)
   {
      profile = "BlockButtonProfile";
      horizSizing = "relative";
      vertSizing = "relative";
      position = "280 400";
      extent = "70 40";
      minExtent = "8 2";
      visible = "1";
      text = "DD Chat";
      groupNum = "-1";
      buttonType = "PushButton";
      bitmap = "base/client/ui/button2";
      command = "SYJ_DD_CSIM::OpenChat();";
      lockAspectRatio = "0";
      alignLeft = "0";
      overflowImage = "0";
      mKeepCached = "0";
      mColor = "255 255 255 255";
   };
   MainMenuGui.add(%b);

When I click the button it connects and sends fine, but the onLine doesn't seem to trigger, even though I'm sure my server is sending back something.