Creating Chat Application with Borland Delphi/Indy: The Client - The Code – Dealing with messages sent from the Server
(Page 3 of 5 )
Add the following in the uses clause of the form (up in the interface section):
jpeg,idsync,IdStreamVCL, INIFiles
Then under the ‘type’ declaration add the following:
TReadingThread = class(TThread)
protected
FConn: TIdTCPConnection;
procedure Execute; override;
public
constructor Create(AConn: TIdTCPConnection); reintroduce;
end;
//you will recognise this bit from the server tutorial
TLog = class(TIdSync)
protected
FMsg: String;
procedure DoSynchronize; override;
public
constructor Create(const AMsg: String);
class procedure AddMsg(const AMsg: String);
end;
Basically, these two classes work together to read any messages that get sent by the server; they then parse the messages to the relevant procedures. So any messages that are received by the client are processed by these classes first, before being sent on. They help to make sure that the main VCL thread is not used when messages are received. The TReading class reads the message first and then uses the tlog class to process the message.
In the public section of the form add the following variables:
port: integer;
ip:string;
Then in the form variables add:
rt: TReadingThread = nil;
In the implementation section add:
uses mmsystem,picfrm,setup,sendfile;
function ParseString(s : string; var str1,str2,str3: string) :
boolean;
var
P1,P2 : integer;
begin
P1 := Pos('@',s);
P2 := Pos(';',s);
//Test if both delimiters are present, in the right order and
//at least 1 char apart
if ((P1 > 0) and (P2 > 0) and (P2 > P1) and (Abs(P2-P1) > 1))
then begin
str1 := Copy(s,1,P1-1);
str2 := Copy(s,P1+1,P2-P1-1);
str3 := Copy(s,P2+1,Length(s)-P2);
Result := True; //valid string
end
else Result := False; //invalid string
end;
I’ve already explained what this function does in the server side tutorial, so I will not go through it here.
constructor TLog.Create(const AMsg: String);
begin
FMsg := AMsg;
inherited Create;
end;
Next: The TLog/TReading Classes >>
More Delphi-Kylix Articles
More By Jacques Noah