delphimvcframework/examples/Chat/ChatClient/MainFormClient.pas

102 lines
2.4 KiB
ObjectPascal
Raw Normal View History

2009-11-10 11:32:54 +01:00
unit MainFormClient;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, stompclient, StompTypes;
2009-11-10 11:32:54 +01:00
type
TForm5 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Button1: TButton;
Edit3: TEdit;
Memo1: TMemo;
Memo2: TMemo;
Button2: TButton;
tmr: TTimer;
procedure Button1Click(Sender: TObject);
procedure tmrTimer(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Memo2KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
private
stomp: IStompClient;
2009-11-10 11:32:54 +01:00
roomname: string;
public
{ Public declarations }
end;
var
Form5: TForm5;
implementation
{$R *.dfm}
procedure TForm5.Button1Click(Sender: TObject);
begin
roomname := '/topic/' + Edit2.Text;
stomp := TStompClient.Create;
stomp.SetUserName('admin');
stomp.SetPassword('password');
2009-11-10 11:32:54 +01:00
stomp.Connect(Edit1.Text);
//Setup for reading messages
stomp.Subscribe(roomname, amClient);
Button1.Enabled := False;
Edit1.Enabled := False;
Edit2.Enabled := False;
Edit3.Enabled := False;
Button2.Enabled := True;
Memo2.Enabled := True;
tmr.Enabled := true;
end;
procedure TForm5.Button2Click(Sender: TObject);
begin
stomp.Send(roomname, Memo2.Lines.Text,
2010-04-02 11:45:06 +02:00
StompUtils.NewHeaders
2009-11-10 11:32:54 +01:00
.Add('sender', Edit3.Text)
.Add('datetime', formatdatetime('yyyy/mm/dd hh:nn:ss', now))
2010-04-02 11:45:06 +02:00
.Add(TStompHeaders.NewPersistentHeader(true))
2009-11-10 11:32:54 +01:00
);
Memo2.Lines.Clear;
end;
procedure TForm5.Memo2KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
if (key = 13) and not (ssCtrl in Shift) then
begin
key := 0;
Button2.Click;
end;
end;
procedure TForm5.tmrTimer(Sender: TObject);
var
2010-04-02 11:45:06 +02:00
f: IStompFrame;
2009-11-10 11:32:54 +01:00
fw: FLASHWINFO;
begin
f := stomp.Receive(50); //this should be done in a secondary thread
2009-11-10 11:32:54 +01:00
if assigned(f) then
begin
2010-04-02 11:45:06 +02:00
Memo1.Lines.Add('[' + f.GetHeaders.Value('datetime') + ' ' + f.GetHeaders.Value('sender') + ']' + sLineBreak + f.GetBody);
2009-11-10 11:32:54 +01:00
if (WindowState = wsMinimized) or (Application.ActiveFormHandle <> self.Handle) then
begin
fw.cbSize := SizeOf(FLASHWINFO);
fw.hwnd := self.Handle;
fw.dwFlags := FLASHW_ALL;
fw.uCount := 5;
fw.dwTimeout := 500;
FlashWindowEx(fw);
end;
end;
end;
end.