Sever Side Chat Application with Borland Delphi/Indy - The Code
(Page 2 of 5 )
The first thing we need to do is create a class that will take the details of every client that connects to the chat. For now we will only take three, the IP address(which will enable you to ban a client if necessary), Nick name and time/date of connection. We also define procedures to send messages, list connected names and files in the same class. Place the following code just below the ‘type’ heading of the form, making sure that it is above the form declaration.
TMyContext = class(TIdContext)
public
IP: String;
Nick: String;
Con: TDateTime;
// compname:string;
procedure SendMsg(const ANick: String; const AMsg:
String);
procedure BroadcastMsg(const bmsg: String);
procedure BroadcastMsgAll(const ANick: String; const
bmsg: String);
procedure SendNicks;
procedure SendFile(const ANick,Fn:string);
end;
The code below is responsible for displaying messages. It frees up the main VCL thread. Without it, I found that the server system usually crashes.
TLog = class(TIdSync)
protected
FMsg: String;
procedure DoSynchronize; override;
public
constructor Create(const AMsg: String);
class procedure AddMsg(const AMsg: String);
end;
Add this line of code above the private section of the form:
constructor Create(AOwner: TComponent);override;
Also, add this under the ‘var’ section of the form:
names:string;
and add in the implementation section:
uses jpeg;
Our communication protocol commands
listnames = request list of names of connected clients
all = send message to all connected clients
takeshot = takes a screenshot of any client
name = sends a private message to named client
file = sends a file
Next: Receiving messages: listnames, all, takeshot, name, file >>
More Delphi-Kylix Articles
More By Jacques Noah