delphimvcframework/examples/Multiple/MainForm.pas

118 lines
2.5 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
2016-10-14 15:19:06 +02:00
procedure OnMessage(MessageBody: string; var TerminateListener: Boolean);
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;
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;
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;
FProducerThread.WaitFor;
FProducerThread.Free;
FSTOMPListener := nil;
end;
2016-10-14 15:19:06 +02:00
procedure TForm4.OnMessage(MessageBody: string; var TerminateListener: Boolean);
begin
2016-10-14 15:19:06 +02:00
Memo1.Lines.Add(MessageBody);
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;
initialization
AllocConsole;
end.