Chat and Client Sockets
by Adam Gardner · in Torque 3D Professional · 07/16/2012 (6:21 pm) · 8 replies
Hello Well I created a chat server in c++ but now working on making the client into the game now and well for a normal c++ console client this would be the code
but for torque I'm not sure how to receive information from the server, any help would be appreciated.
So far The game can connect to the server and read when I send information but the client cant read the information yet Im not sure how to go by that.
string ClientSocket::checkForIncomingMessages()
{
// Define a string with a blank message
string receivedMessage = "";
// Poll for messages for a specified time (default: 10ms, so 100 times per second)
int activeSockets = SDLNet_CheckSockets(socketSet, ClientSocket::SOCKET_SET_POLL_PERIOD);
// This produces a LOT of debug output, so only uncomment if the code's really misbehaving...
//if (debug) { cout << "There are " << activeSockets << " socket(s) with data on them at the moment." << endl; }
if (activeSockets != 0)
{
// Check if we got a message from the server
int gotMessage = SDLNet_SocketReady(clientSocket);
if (gotMessage != 0)
{
int serverResponseByteCount = SDLNet_TCP_Recv(clientSocket, pBuffer, bufferSize);
if (debug)
{
cout << endl << "Got message: " << pBuffer << " (" << serverResponseByteCount << " bytes)" << endl;
}
if (serverResponseByteCount != 0)
{
receivedMessage = toString(pBuffer);
// If we've received the shutdown signal set the flag appropriately
if (receivedMessage == ClientSocket::SHUTDOWN_SIGNAL)
{
if (debug) { cout << "Setting shutdownClient to true" << endl; }
shutdownClient = true;
}
}
else // If we've received a 0 byte message from the server then we've lost the connection!
{
// ...generate a suitable error message...
string msg = "Lost connection to the server!";
// ...and then throw it as an exception!
SocketException e(msg);
throw e;
}
} // End of if (gotMessage != 0) section
} // End of if (activeSockets != 0) section
// Return the message, whether the actually is a message, or whether it's blank
return receivedMessage;
}but for torque I'm not sure how to receive information from the server, any help would be appreciated.
So far The game can connect to the server and read when I send information but the client cant read the information yet Im not sure how to go by that.
#2
It's an excellent example of how to use TCPObject to listen for connection requests, and send/receive information.
07/17/2012 (8:13 am)
Take a look at this thread. It's an excellent example of how to use TCPObject to listen for connection requests, and send/receive information.
#3
Also genome the chat system in torque isn't good enough, it wont help because people can add friends ignore people create channels all that and doesn't save a log that I can review for future abuse in the chat.
07/17/2012 (8:22 am)
Yeah I looked at the thread but the link is dead and a dead link doesn't help me much, but thanks for the tips!Also genome the chat system in torque isn't good enough, it wont help because people can add friends ignore people create channels all that and doesn't save a log that I can review for future abuse in the chat.
#4
5457.torquechat.zip
As Guy said it's an excellent example of using the TCPObject.
07/17/2012 (8:52 am)
When the Resource links go dead like that you'll likely find that someone somewhere has a backup of it... so it never hurts to ask: 5457.torquechat.zip
As Guy said it's an excellent example of using the TCPObject.
#5
07/17/2012 (8:57 am)
@Michael you are so awesome man thanks a ton that saved my I was about to start working on adding sdl stuff into torque to allow me to use that to read the ports =D
#6
07/17/2012 (9:44 am)
hahaha wow I feel kind of bad now I did have it already working I just needed to make it show up in the gui and I had the messages printing to console
#7
07/17/2012 (9:59 am)
AH, I didn't realize the link was dead. Thx Michael.
#8
07/17/2012 (10:08 am)
Yeah much appreciated on the link and information but it was on my side that I forgot to make it print in the gui and not in just console
Torque 3D Owner genomegames
GenomeGames