procedure TSecureChatServerService.TCPServerConnect(
AThread: TIdPeerThread);
var
Msg:
String;
Client: TClient;
N: Integer;
TempStr:
String;
begin
Inc(GuestNum);
AThread.Connection.WriteLn('
This is the SecureChat Server on ' + AThread.Connection.LocalName);
BroadcastMsg('
Guest ' + inttostr(GuestNum) + '
betritt den Chat.');
Client := TClient.Create(Self);
Client.
Name := '
Guest ' + inttostr(GuestNum);
Client.Thread := AThread;
PublicRoom.AddClient(Client);
Client.Room := PublicRoom;
ClientList.AddClient(Client);
Client.SendMessage('
Your Nickname is ' + Client.
Name, true);
while AThread.Connection.Connected
do
begin
Msg := AThread.Connection.ReadLn;
if (Msg = '
#~CMD: DISCONNECT')
then
begin
Client.SendMessage('
Verbindung wird getrennt. Auf Wiedersehen!', true);
Client.Room.RemoveClient(Client);
ClientList.RemoveClient(Client);
AThread.Connection.Disconnect;
end else
begin
Client := ClientList.FindClient(Client.Thread);
if Pos('
#~CMD:', Msg) = 1
then
begin
TempStr := ProcessCommand(Msg, Client);
if TempStr <> '
'
then Client.SendMessage(TempStr, true);
end else
try
for N := 0
to Client.Room.ClientsCount-1
do
begin
if Assigned(Client.Room.Client(N).Thread)
then
Client.Room.Client(N).SendMessage(Client.
Name + '
: ' + Msg, true);
end;
except on Exception do
Client.SendMessage('
Exception...', true);
end;
end;
end;
BroadcastMsg(Client.
Name + '
verlässt den Chat.');
Client.Destroy;
end;
procedure TSecureChatServerService.BroadcastMsg(Msg:
String);
var N: Integer;
begin
try
for N := 0
to ClientList.ClientCount-1
do ClientList.Client(N).SendMessage(Msg, true);
except on Exception do ;
end;
end;