TCPObject Working From Console But Not From Script
by Valador, Inc. · in Torque Game Engine · 06/23/2006 (8:08 am) · 8 replies
I'm trying to use TCPObject to connect out to a custom server and authenticate a user.
If I type the following into the console directly, it works fine:
new tcpobject(tcpobj);
tcpobj.connect("localhost:93148");
tcpobj.send("Auth:Login:Password\n");
tcpobj.disconnect();
I get a message on the authentication server saying that the torque client connected and sent a login/password for authentication. I also get messages in the torque client saying I've connected and I even get a response back on the console saying whether or not the authentication passed (I just have the TCPObject::onLine function echoing a line to the console).
If I put the same code into the torque server's game.cs script file (specifically the onClientEnterGame function), it doesn't work. I see that the script is getting to the code as I have debug echos surrounding the code. The authentication server never sees anyone trying to connect and I get no messages on the torque console.
My first thought was that maybe typing it in the console means it's running on the torque client end and the script is running on the torque server end. So I tried putting the code in a clientCmd and just calling commandToClient from the server end, but that doesn't work either.
Any help would be appreciated.
If I type the following into the console directly, it works fine:
new tcpobject(tcpobj);
tcpobj.connect("localhost:93148");
tcpobj.send("Auth:Login:Password\n");
tcpobj.disconnect();
I get a message on the authentication server saying that the torque client connected and sent a login/password for authentication. I also get messages in the torque client saying I've connected and I even get a response back on the console saying whether or not the authentication passed (I just have the TCPObject::onLine function echoing a line to the console).
If I put the same code into the torque server's game.cs script file (specifically the onClientEnterGame function), it doesn't work. I see that the script is getting to the code as I have debug echos surrounding the code. The authentication server never sees anyone trying to connect and I get no messages on the torque console.
My first thought was that maybe typing it in the console means it's running on the torque client end and the script is running on the torque server end. So I tried putting the code in a clientCmd and just calling commandToClient from the server end, but that doesn't work either.
Any help would be appreciated.
#2
I see the debug echo statements have printed out and the exec reports that the the file has been loaded, but I get no "Connected" message or even "Connection Failed" and the server doesn't report anyone trying to connect.
Yet I type the same exact thing in the console and it works fine.
Also, I know the code is being run because I don't need to instantiate a new TCPObject when I type it manually in the console. I can start the client and the immediately type:
tcpobj.connect("localhost:93148");
and I get back a message saying Connected and the server reports the connection.
It's really strange... Thanks for the assistance though!
06/23/2006 (10:19 am)
Yeah, that was a direct cut and paste from the script file. The only thing left out was the exec of another script file which defines the TCPObject functions that echo status messages (like Connected, Connection Failed, etc) to the console. The code is also wrapped in two debug echo statements so I can see where in the console the code is being executed. I see the debug echo statements have printed out and the exec reports that the the file has been loaded, but I get no "Connected" message or even "Connection Failed" and the server doesn't report anyone trying to connect.
Yet I type the same exact thing in the console and it works fine.
Also, I know the code is being run because I don't need to instantiate a new TCPObject when I type it manually in the console. I can start the client and the immediately type:
tcpobj.connect("localhost:93148");
and I get back a message saying Connected and the server reports the connection.
It's really strange... Thanks for the assistance though!
#3
It appears the problem is that the code is executing too quickly. So as it's trying to connect it sends the message and disconnects. There is no waiting for anything.
I took the disconnect out and I got a server connection. The message still was not being sent though because it was happening before the connection was finalized (this is what I'm guessing anyway). So I tried putting the sending of the auth message in the onConnected function, but that didn't work. The server still doesn't get any message, though is does get a connection because I've kept the disconnect out for the time being.
What can I use to make the script wait until a connection has been completed and the message is sent? I'm using this for user authentication so they shouldn't be able to get to the next part of the script until we're sure they're a valid user.
Thanks!
06/23/2006 (10:39 am)
Alright... It appears the problem is that the code is executing too quickly. So as it's trying to connect it sends the message and disconnects. There is no waiting for anything.
I took the disconnect out and I got a server connection. The message still was not being sent though because it was happening before the connection was finalized (this is what I'm guessing anyway). So I tried putting the sending of the auth message in the onConnected function, but that didn't work. The server still doesn't get any message, though is does get a connection because I've kept the disconnect out for the time being.
What can I use to make the script wait until a connection has been completed and the message is sent? I'm using this for user authentication so they shouldn't be able to get to the next part of the script until we're sure they're a valid user.
Thanks!
#4
For example, when onConnected is called, you send the message. When the server responds with the success code or whatever it is that it's sending, you disconnect. This is why you got callbacks from the engine code.
06/23/2006 (12:07 pm)
You use onConnected and onLine to parse the messages returned from the server and act appropriatly.For example, when onConnected is called, you send the message. When the server responds with the success code or whatever it is that it's sending, you disconnect. This is why you got callbacks from the engine code.
#5
function TCPObject::onConnected(%this)
{
echo("Connected");
%this.send("Auth:LoginName:Password\n");
}
That's my onConnected function. The console does display the "Connected" message, but the server never gets the authentication message.
Plus there's the issue that I don't want the game.cs onClientEnterGame function to continue executing until I get a valid response from the auth server. And if the auth server returns an authentication failure then I want to disconnect the client.
06/23/2006 (12:30 pm)
That isn't working either though. function TCPObject::onConnected(%this)
{
echo("Connected");
%this.send("Auth:LoginName:Password\n");
}
That's my onConnected function. The console does display the "Connected" message, but the server never gets the authentication message.
Plus there's the issue that I don't want the game.cs onClientEnterGame function to continue executing until I get a valid response from the auth server. And if the auth server returns an authentication failure then I want to disconnect the client.
#6
Edit: There are problems with Torque's TCP/IP object though, and it will drop packets randomly.. but not when you're sending one at a time like you do.
06/23/2006 (12:52 pm)
It does on my end, that's all I can say. My code is identical except that I do not use %this.Edit: There are problems with Torque's TCP/IP object though, and it will drop packets randomly.. but not when you're sending one at a time like you do.
#7
06/27/2006 (7:54 am)
I got it working. It turned out that it was calling a different onConnected function somehow. Once I pointed it to the correct place it works fine. Thanks for all the help!
#8
In any case, glad you got it working. (:
06/27/2006 (8:18 am)
You don't point to onConnected, that's done by the engine in a executef() call, so you're not making much sense to me. In any case, glad you got it working. (:
Torque Owner Stefan Lundmark
However, I don't see how the server can pass information back to you when you've closed the connection. Since it's a loopback connection, I guess it could be faster but it's not good practice.
Are you *really* putting the same block of code in your scriptfiles, unmodified from the one you typed in the console? I can't see how that's possible.