delphimvcframework/examples/Multiple/MainForm.pas

120 lines
2.7 KiB
ObjectPascal
Raw Normal View History

unit MainForm;
interface
uses
Winapi.Windows,
Winapi.Messages,
System.SysUtils,
System.Variants,
System.Classes,
Vcl.Graphics,
Vcl.Controls,
Vcl.Forms,
Vcl.Dialogs,
Vcl.StdCtrls,
StompTypes;
type
TForm4 = class(TForm, IStompClientListener)
Button1: TButton;
Memo1: TMemo;
2016-10-14 15:19:06 +02:00
Button2: TButton;
Button3: TButton;
procedure Button1Click(Sender: TObject);
2016-10-14 15:19:06 +02:00
procedure FormDestroy(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
2016-10-14 15:19:06 +02:00
FSTOMPListener: IStompListener;
FSTOMPClient: IStompClient;
FFormClosing: Boolean;
FProducerThread: TThread;
public
procedure OnMessage(StompFrame: IStompFrame; var TerminateListener: Boolean);
2016-10-14 15:19:06 +02:00
procedure OnListenerStopped(StompClient: IStompClient);
end;
var
Form4: TForm4;
implementation
uses StompClient;
{$R *.dfm}
procedure TForm4.Button1Click(Sender: TObject);
begin
2016-10-14 15:19:06 +02:00
FSTOMPListener.StopListening;
Memo1.Lines.Add('Listener Started');
2016-10-14 15:19:06 +02:00
FSTOMPListener.StartListening;
end;
2016-10-14 15:19:06 +02:00
procedure TForm4.Button2Click(Sender: TObject);
begin
FSTOMPListener.StopListening;
end;
2016-10-14 15:19:06 +02:00
procedure TForm4.Button3Click(Sender: TObject);
begin
FProducerThread := TThread.CreateAnonymousThread(
2016-09-20 16:27:17 +02:00
procedure
var
i: Integer;
stomp: IStompClient;
begin
2016-09-20 16:27:17 +02:00
stomp := TStompClient.CreateAndConnect;
2016-10-14 15:19:06 +02:00
i := 1;
while True do
begin
2016-10-14 15:19:06 +02:00
sleep(300);
if FFormClosing then
Exit;
2016-09-20 16:27:17 +02:00
stomp.Send('/topic/danieleteti', 'Hello World ' + IntToStr(i));
2016-10-14 15:19:06 +02:00
inc(i);
end;
stomp.Disconnect;
2016-10-14 15:19:06 +02:00
end);
FProducerThread.FreeOnTerminate := False;
FProducerThread.Start;
Button3.Enabled := False;
ShowMessage('Background thread started... Now you can start the subscriber');
end;
2016-10-14 15:19:06 +02:00
procedure TForm4.FormCreate(Sender: TObject);
begin
2016-10-14 15:19:06 +02:00
FFormClosing := False;
FSTOMPClient := TStompClient.CreateAndConnect;
FSTOMPClient.Subscribe('/topic/danieleteti',
amAuto,
StompUtils.Headers.Add('include-seq', 'seq'));
FSTOMPListener := TStompClientListener.Create(FSTOMPClient, Self);
end;
2016-10-14 15:19:06 +02:00
procedure TForm4.FormDestroy(Sender: TObject);
begin
2016-10-14 15:19:06 +02:00
FFormClosing := True;
if Assigned(FProducerThread) then
begin
FProducerThread.WaitFor;
FProducerThread.Free;
end;
2016-10-14 15:19:06 +02:00
FSTOMPListener := nil;
end;
procedure TForm4.OnMessage(StompFrame: IStompFrame; var TerminateListener: Boolean);
begin
Memo1.Lines.Add(StompFrame.Body);
2016-10-14 15:19:06 +02:00
TerminateListener := FFormClosing;
end;
2016-10-14 15:19:06 +02:00
procedure TForm4.OnListenerStopped(StompClient: IStompClient);
begin
2016-10-14 15:19:06 +02:00
Memo1.Lines.Add('Listener Stopped');
end;
end.