TsgcWebSocketClientEvents › OnBinary

OnBinary Event

Fires every time the server sends a binary message to the client.

Syntax

property OnBinary: TsgcWSBinaryEvent;
// TsgcWSBinaryEvent = procedure(Connection: TsgcWSConnection; const Data: TMemoryStream) of object

Default Value

Remarks

OnBinary is raised once the full binary payload has been received and buffered. The Data parameter exposes the decoded bytes as a TMemoryStream positioned at 0; read it immediately because the stream is owned by the client and becomes invalid after the handler returns. By default NotifyEvents is neAsynchronous, so OnBinary is synchronized with the main thread and it is safe to update UI controls from the handler; switch to neNoSync if you need to avoid synchronization and handle thread-safety yourself. If Options.FragmentedMessages is frgOnlyFragmented the event is not fired and OnFragmented is used instead.

Example


procedure OnBinary(Connection: TsgcWSConnection; const Data: TMemoryStream);
var
  oBitmap: TBitmap;
begin
  oBitmap := TBitmap.Create;
  try
    oBitmap.LoadFromStream(Data);
    Image1.Picture.Assign(oBitmap);
  finally
    FreeAndNil(oBitmap);
  end;
end;

Back to Events