Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send command to all connected clients

Tags:

tcp

delphi

indy

I have a TIdHttpServer i must keep the connection open in order to send some commands back to the clients. I want to iterate when i press a button and send a command to all connected clients.

How can i do this ?

like image 726
opc0de Avatar asked Apr 12 '12 05:04

opc0de


1 Answers

You can use the Contexts property to get the clients and then using the IOHandler of each client you can send a message.

Var
  Clients : TList;
  i : integer;
begin

  if not Assigned(IdTCPServer1.Contexts) then exit;

  Clients:=IdTCPServer1.Contexts.LockList;
  try
    for i := 0 to Clients.Count-1 do
      try
        TIdContext(Clients[i]).Connection.IOHandler.Write(LBuffer);//LBuffer is a TBytes with the data to send
      except
        ...
      end;
  finally
    IdTCPServer1.Contexts.UnlockList;
  end;

end;
like image 91
RRUZ Avatar answered Sep 18 '22 10:09

RRUZ