Search code examples
delphitcpindy

Send command to all connected clients


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 ?


Solution

  • 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;