Protocol Files | How Send Files To Clients

To send a file to a client, just call the method SendFile of Files Protocol and pass the Guid of the Connection and the full FileName as argument. The Guid of the client connection can be captured OnConnect event of Server Protocol Files.

The file received by the client will be saved by default in the same directory where the client executable is located or in the Path set in the Files.SaveDirectory property.

 


// ... capture the guid of the client connection to send later the file
procedure OnConnectEvent(Connection: TsgcWSConnection);
begin
  FGuid := Connection.Guid;
end;

// ... Create Server oServer := TsgcWebSocketServer.Create(nil); oServer_Files := TsgcWSPServer_Files.Create(nil); oServer_Files.Server := oServer; oServer_Files.OnConnect := OnConnectEvent; oServer.Host := '127.0.0.1'; oServer.Port := 8080;
// ... Create Client oClient := TsgcWebSocketClient.Create(nil); oClient.URL := 'ws://127.0.0.1:8080';
// ... Create Protocol oClient_Files := TsgcWSPClient_Files.Create(nil); oClient_Files.Client := oClient;
// ... Start Server and Connect Clients oServer.Active := True; oClient.Connect();
// ... Send File to the client connected oServer_Files.SendFile(FConnection.Guid, 'c:\Documents\yourfile.txt');