Server Side Chat Application with Borland Delphi/Indy, concluded - Code for leaving a chat
(Page 4 of 4 )
The next bit of code deals with what happens when a client leaves the chat. Go to the object inspector, double click on OnDisconnect and add the code below:
procedure TForm1.tsDisconnect(AContext: TIdContext);
begin
//inform everybody that the client left the chat
TLog.AddMsg(TMyContext(AContext).Nick + ' Left the chat');
tmycontext(acontext).broadcastMsg(tmycontext(acontext).Nick + ' Left the chat');
end;
This last bit disconnects the server. Click anywhere on the form, go to the object inspector events tab, double click on Onclosequery and add the following code:
procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
ts.Active:=false;
end;
Finally
Remember to add the following to the forms’ uses clause right at the top of the form but below ‘interface’: idcontext, idsync, idstreamvcl. Also, I’m using Indy version 10.0.52, so these units may not be available if you are using an older version, so try to update your Indy version.
Copy and paste both the code from the previous tutorial and this one, on one form in the order that I’ve shown them, and you should be ready to go.
That’s it! Now you have a fully functioning chat server. Next we are going to develop a client application that will allow people to connect to your server and chat! So stay tuned.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |