2012-04-12 3 views
6

J'ai un TIdHttpServer Je dois garder la connexion ouverte afin de renvoyer certaines commandes aux clients. Je veux itérer quand j'appuie sur un bouton et envoie une commande à tous les clients connectés.Envoyer la commande à tous les clients connectés

Comment puis-je faire cela?

+2

Version d'indy? – RRUZ

+0

J'utilise delphi xe 2 avec indy 10 – opc0de

Répondre

10

Vous pouvez utiliser la propriété Contexts pour obtenir les clients, puis utiliser le IOHandler de chaque client pour envoyer un 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; 
Questions connexes