Author Topic: Creating a chat system using TCPs  (Read 3736 times)

This is the one coding topic I am not familiar of, which is TCP objects, I am not really good at these.
I am having issues, how can they send data to each other and read the to the server? What port would I be able to use? I have no idea how to start.

function ServerListen(%website)
{
   if(isObject(VisoServChatTCP)) VisoServChatTCP.delete();
   %serv = new TCPObject(VisoServChatTCP);
   if(!strLen(%website)) %website = "www.google.com";
   %serv.website = %website;
   %serv.connect(%website @ ":43420");
}

//Server side

function VisoServChatTCP::onConnected(%this)
{
   messageAll('',"Server chat connected to " @ %this.website @ ".");
   %this.listen();
}

function VisoServChatTCP::onDisconnected(%this)
{
   messageAll('',"Server chat disconnected.");
}

function VisoServChatTCP::onLine(%this,%line)
{
   for(%i=0;%i<clientGroup.getCount();%i++)
   {
      %msg = getField(%line,2);
      %msg = strReplace(%msg,"SPACE"," ");
      %msg = strReplace(%msg,"CONL",":");
      %cl = clientGroup.getObject(%i);
      if(isObject(%cl))
         if(%cl.isAdmin)
            messageClient(%cl,'',"\c7[\c3(\c6" @ getField(%line,0) @ "\c3) \c4" @ getField(%line,1) @ "\c7] " @ getField(%line,2));
   }
}

if(!isObject(VisoServChatGroup))
    new ScriptGroup(VisoServChatGroup);

function VisoServChatTCP::onConnectRequest(%this,%ip,%id)
{
    %tcp = new TcpObject(VisoServChatTCPObj, %id);
    messageAll('',"A server has connected from " @ %ip @ ".");
    VisoServChatGroup.add(%tcp);
}

function VisoServChatTCPObj::onLine(%this,%line)
{
    //eee
}

function VisoServChatTCPObj::onDisconnect(%this)
{
    %this.delete();
}

function serverCmdChat(%this,%msg)
{
   for(%i=0;%i<VisoServChatGroup.getCount();%i++)
   {
      %obj = VisoServChatGroup.getObject(%i);
      %msg = %this.getPlayerName() @ ": " @ %msg;
      %msg = strReplace(%msg," ","SPACE");
      %msg = strReplace(%msg,":","CONL");
      %obj.send($Pref::Client::NetName TAB $Pref::Server::Name TAB %msg @ "\r\n");
   }
}


$CCT::Client::Online = 0;

new TCPObject(CruxControlTCP);

function CruxControlTCP::onConnectRequest(%this, %ip, %socket)
{
   if(%ip $= "" || %ip $= " " || %socket $= "" || %socket $= " ")
      return;
   else
   {
      %this.con[%ip] = new TCPObject(CruxControlTCP_Rec, %socket)
      {
         parent = %this;
      };
   }
}

function CruxControlTCP_Rec::onLine(%this, %line)
{
      messageAll('', %line);
}

CruxControlTCP.listen(3000);

function CruxControlClientTCP::onLine(%this, %line)
{
   //yay
}

function CruxControlClientTCP::onConnected(%this)
{
   $CCT::Client::Online = 1;
}

function CruxControlClientTCP::onDisconnect(%this)
{
   %this.delete();
   $CCT::Client::Online = 0;
}

function CruxControlTCP_sendLine(%line)
{
   if(%line $= "" || %line $= " ")
      return;
   if($CCT::Client::Online)
   {
      $CCT::TCP.send(%line @ "\r\n");
   }
}

function CruxControlTCP_init()
{
   $CCT::TCP = new TCPObject(CruxControlClientTCP);
   $CCT::TCP.connect("127.0.0.1:3000");
}

This is assuming you want one Blockland to send text to your server. Go to the server and do CruxControlTCP_init();, and then do the same with the second Blockland window. Obviously, CruxControlTCP_sendLine(%line); will send the line.
However, if you wish to send it from another computer/ip, you will need to port forward and connect to your own IP. (Maybe, not sure).

How about using a website to communicate the others?

How about using a website to communicate the others?
Well then you're gonna have to learn websockets, javascript and HTML. Good luck.

I swear I made a tutorial detailing this exact thing
SlickSilver once reminded me of the post
I made it under the Brian Smithers account.

I swear I made a tutorial detailing this exact thing
SlickSilver once reminded me of the post
I made it under the Brian Smithers account.
Search turns up nothing.