| Blockland Forums > Modification Help |
| Help with TCPObject..... |
| (1/1) |
| DrenDran:
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: ---$DrenDranChat = new TCPObject(); echo("Hello."); $DrenDranChat.connect("66.240.176.166:4526"); $DrenDranChat.send("SYJ Hello."); $DrenDranChat.delete(); echo("Good bye."); --- End code --- It didn't work. |
| Destiny/Zack0Wack0:
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: ---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(); } --- End code --- 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. |
| DrenDran:
Thank you, I shall try that out now. |
| Pew446:
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. |
| DrenDran:
--- Code: --- 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); --- End code --- 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. |
| Navigation |
| Message Index |