delphimvcframework/StompClient.pas

801 lines
21 KiB
ObjectPascal
Raw Normal View History

2011-04-14 11:13:25 +02:00
// Stomp Client for Embarcadero Delphi & FreePascal
// Tested With ApacheMQ 5.2/5.3, Apache Apollo 1.2, RabbitMQ
// Copyright (c) 2009-2015 Daniele Teti
2011-04-14 11:13:25 +02:00
//
// Contributors:
// Daniel Gaspary: dgaspary@gmail.com
// Oliver Marr: oliver.sn@wmarr.de
2013-05-10 18:00:10 +02:00
// Marco Mottadelli: mottadelli75@gmail.com
2011-04-14 11:13:25 +02:00
// WebSite: www.danieleteti.it
// email:d.teti@bittime.it
// *******************************************************
unit StompClient;
// For FreePascal users:
// Automatically selected synapse tcp library
{$IFDEF FPC}
{$MODE DELPHI}
{$DEFINE USESYNAPSE}
{$ENDIF}
// For Delphi users:
// Decomment following line to use synapse also in Delphi
{ .$DEFINE USESYNAPSE }
interface
uses
StompTypes,
SysUtils,
2015-01-21 14:59:20 +01:00
{$IFNDEF USESYNAPSE}
IdTCPClient,
IdException,
IdExceptionCore,
2016-06-28 15:21:47 +02:00
IdHeaderList,
2015-01-21 14:59:20 +01:00
{$ELSE}
synsock,
blcksock,
2015-01-21 14:59:20 +01:00
{$ENDIF}
Classes;
type
{ TStompClient }
TSenderFrameEvent = procedure(AFrame: IStompFrame) of object;
TStompClient = class(TInterfacedObject, IStompClient)
private
2015-01-21 14:59:20 +01:00
{$IFDEF USESYNAPSE}
FSynapseTCP: TTCPBlockSocket;
FSynapseConnected: boolean;
2015-01-21 14:59:20 +01:00
{$ELSE}
FTCP: TIdTCPClient;
2015-01-21 14:59:20 +01:00
{$ENDIF}
FHeaders: IStompHeaders;
FPassword: string;
FUserName: string;
FTimeout: Integer;
FSession: string;
FInTransaction: boolean;
FTransactions: TStringList;
FReceiptTimeout: Integer;
FServerProtocolVersion: string;
FClientAcceptProtocolVersion: TStompAcceptProtocol;
2015-01-21 14:59:20 +01:00
FServer: string;
FOnBeforeSendFrame: TSenderFrameEvent;
FOnAfterSendFrame: TSenderFrameEvent;
2016-09-23 22:36:36 +02:00
FHost: string;
FPort: Integer;
FClientID: string;
FAcceptVersion: TStompAcceptProtocol;
FConnectionTimeout: UInt32;
procedure SetReceiptTimeout(const Value: Integer);
2016-09-23 22:36:36 +02:00
procedure SetConnectionTimeout(const Value: UInt32);
protected
2015-01-21 14:59:20 +01:00
{$IFDEF USESYNAPSE}
procedure SynapseSocketCallBack(Sender: TObject; Reason: THookSocketReason;
const Value: string);
2015-01-21 14:59:20 +01:00
{$ENDIF}
procedure Init;
procedure DeInit;
2016-09-23 22:36:36 +02:00
procedure MergeHeaders(var AFrame: IStompFrame;
var AHeaders: IStompHeaders);
procedure SendFrame(AFrame: IStompFrame);
2016-07-26 11:21:07 +02:00
function FormatErrorFrame(const AErrorFrame: IStompFrame): string;
public
function SetPassword(const Value: string): IStompClient;
function SetUserName(const Value: string): IStompClient;
2016-09-23 22:36:36 +02:00
function Receive(out StompFrame: IStompFrame; ATimeout: Integer)
: boolean; overload;
function Receive: IStompFrame; overload;
function Receive(ATimeout: Integer): IStompFrame; overload;
procedure Receipt(const ReceiptID: string);
2016-09-23 22:36:36 +02:00
procedure Connect(Host: string = '127.0.0.1';
Port: Integer = DEFAULT_STOMP_PORT; ClientID: string = '';
AcceptVersion: TStompAcceptProtocol = TStompAcceptProtocol.
STOMP_Version_1_0);
2013-10-15 10:14:36 +02:00
procedure Disconnect;
2016-09-23 22:36:36 +02:00
procedure Subscribe(QueueOrTopicName: string;
Ack: TAckMode = TAckMode.amAuto; Headers: IStompHeaders = nil);
procedure Unsubscribe(Queue: string);
2016-09-23 22:36:36 +02:00
procedure Send(QueueOrTopicName: string; TextMessage: string;
Headers: IStompHeaders = nil); overload;
2016-09-23 22:36:36 +02:00
procedure Send(QueueOrTopicName: string; TextMessage: string;
TransactionIdentifier: string; Headers: IStompHeaders = nil); overload;
procedure Ack(const MessageID: string;
const TransactionIdentifier: string = '');
{ STOMP 1.1 }
2016-09-23 22:36:36 +02:00
procedure Nack(const MessageID: string;
const TransactionIdentifier: string = '');
procedure BeginTransaction(const TransactionIdentifier: string);
procedure CommitTransaction(const TransactionIdentifier: string);
procedure AbortTransaction(const TransactionIdentifier: string);
/// ////////////
2016-08-02 18:15:11 +02:00
constructor Create; overload; virtual;
2016-09-23 22:36:36 +02:00
class function CreateAndConnect(Host: string = '127.0.0.1';
Port: Integer = DEFAULT_STOMP_PORT; ClientID: string = '';
AcceptVersion: TStompAcceptProtocol = TStompAcceptProtocol.
STOMP_Version_1_0): IStompClient; overload; virtual;
destructor Destroy; override;
2016-09-23 22:36:36 +02:00
function Clone: IStompClient;
function Connected: boolean;
function SetReceiveTimeout(const AMilliSeconds: Cardinal): IStompClient;
function GetProtocolVersion: string;
function GetServer: string;
function GetSession: string;
2016-09-23 22:36:36 +02:00
property ReceiptTimeout: Integer read FReceiptTimeout
write SetReceiptTimeout;
property Transactions: TStringList read FTransactions;
2016-09-23 22:36:36 +02:00
property ConnectionTimeout: UInt32 read FConnectionTimeout
write SetConnectionTimeout;
// * Manage Events
2016-09-23 22:36:36 +02:00
property OnBeforeSendFrame: TSenderFrameEvent read FOnBeforeSendFrame
write FOnBeforeSendFrame;
property OnAfterSendFrame: TSenderFrameEvent read FOnAfterSendFrame
write FOnAfterSendFrame;
end;
implementation
{$IFDEF FPC}
const
CHAR0 = #0;
{$ELSE}
uses
2013-05-10 18:00:10 +02:00
// Windows, // Remove windows unit for compiling on ios
2015-01-21 14:59:20 +01:00
IdGlobal,
2016-06-28 15:21:47 +02:00
IdGlobalProtocols,
Character;
{$ENDIF}
{ TStompClient }
procedure TStompClient.AbortTransaction(const TransactionIdentifier: string);
var
Frame: IStompFrame;
begin
2015-01-21 14:59:20 +01:00
if FTransactions.IndexOf(TransactionIdentifier) > -1 then
begin
Frame := TStompFrame.Create;
Frame.SetCommand('ABORT');
Frame.GetHeaders.Add('transaction', TransactionIdentifier);
SendFrame(Frame);
2013-10-15 10:14:36 +02:00
FInTransaction := False;
FTransactions.Delete(FTransactions.IndexOf(TransactionIdentifier));
end
else
2016-09-23 22:36:36 +02:00
raise EStomp.CreateFmt
('Abort Transaction Error. Transaction [%s] not found',
[TransactionIdentifier]);
end;
2016-09-23 22:36:36 +02:00
procedure TStompClient.Ack(const MessageID: string;
const TransactionIdentifier: string);
var
Frame: IStompFrame;
begin
Frame := TStompFrame.Create;
Frame.SetCommand('ACK');
2013-10-15 10:14:36 +02:00
Frame.GetHeaders.Add(TStompHeaders.MESSAGE_ID, MessageID);
if TransactionIdentifier <> '' then
Frame.GetHeaders.Add('transaction', TransactionIdentifier);
SendFrame(Frame);
end;
procedure TStompClient.BeginTransaction(const TransactionIdentifier: string);
var
Frame: IStompFrame;
begin
2015-01-21 14:59:20 +01:00
if FTransactions.IndexOf(TransactionIdentifier) = -1 then
begin
Frame := TStompFrame.Create;
Frame.SetCommand('BEGIN');
Frame.GetHeaders.Add('transaction', TransactionIdentifier);
SendFrame(Frame);
// CheckReceipt(Frame);
FInTransaction := True;
FTransactions.Add(TransactionIdentifier);
end
else
2016-09-23 22:36:36 +02:00
raise EStomp.CreateFmt
('Begin Transaction Error. Transaction [%s] still open',
[TransactionIdentifier]);
end;
2013-10-15 10:14:36 +02:00
// procedure TStompClient.CheckReceipt(Frame: TStompFrame);
// var
// ReceiptID: string;
// begin
// if FEnableReceipts then
// begin
// ReceiptID := inttostr(GetTickCount);
// Frame.GetHeaders.Add('receipt', ReceiptID);
// SendFrame(Frame);
// Receipt(ReceiptID);
// end
// else
// SendFrame(Frame);
// end;
2016-09-23 22:36:36 +02:00
function TStompClient.Clone: IStompClient;
begin
Result := TStompClient.Create;
TStompClient(Result).ConnectionTimeout := FConnectionTimeout;
TStompClient(Result).Connect(FHost, FPort, FClientID, FAcceptVersion);
end;
procedure TStompClient.CommitTransaction(const TransactionIdentifier: string);
var
Frame: IStompFrame;
begin
2015-01-21 14:59:20 +01:00
if FTransactions.IndexOf(TransactionIdentifier) > -1 then
begin
Frame := TStompFrame.Create;
Frame.SetCommand('COMMIT');
Frame.GetHeaders.Add('transaction', TransactionIdentifier);
SendFrame(Frame);
2013-10-15 10:14:36 +02:00
FInTransaction := False;
FTransactions.Delete(FTransactions.IndexOf(TransactionIdentifier));
end
else
2016-09-23 22:36:36 +02:00
raise EStomp.CreateFmt
('Commit Transaction Error. Transaction [%s] not found',
[TransactionIdentifier]);
end;
2016-07-26 17:41:05 +02:00
procedure TStompClient.Connect(Host: string; Port: Integer; ClientID: string;
AcceptVersion: TStompAcceptProtocol);
var
Frame: IStompFrame;
begin
2016-09-23 22:36:36 +02:00
FHost := Host;
FPort := Port;
FClientID := ClientID;
FAcceptVersion := AcceptVersion;
try
Init;
2015-01-21 14:59:20 +01:00
{$IFDEF USESYNAPSE}
2013-10-15 10:14:36 +02:00
FSynapseConnected := False;
FSynapseTCP.Connect(Host, intToStr(Port));
FSynapseConnected := True;
2015-01-21 14:59:20 +01:00
{$ELSE}
2016-09-23 22:36:36 +02:00
FTCP.ConnectTimeout := FConnectionTimeout;
FTCP.Connect(Host, Port);
FTCP.IOHandler.MaxLineLength := MaxInt;
2015-01-21 14:59:20 +01:00
{$ENDIF}
Frame := TStompFrame.Create;
Frame.SetCommand('CONNECT');
FClientAcceptProtocolVersion := AcceptVersion;
2016-09-23 22:36:36 +02:00
if TStompAcceptProtocol.STOMP_Version_1_1 in [FClientAcceptProtocolVersion]
then
begin
2016-07-26 17:41:05 +02:00
Frame.GetHeaders.Add('heart-beat', '0,1000'); // stomp 1.1
Frame.GetHeaders.Add('accept-version', '1.1'); // stomp 1.1
end;
Frame.GetHeaders.Add('login', FUserName).Add('passcode', FPassword);
2016-09-23 22:36:36 +02:00
FClientID := ClientID;
if ClientID <> '' then
2016-09-23 22:36:36 +02:00
begin
Frame.GetHeaders.Add('client-id', ClientID);
2016-09-23 22:36:36 +02:00
end;
SendFrame(Frame);
Frame := nil;
while Frame = nil do
Frame := Receive;
if Frame.GetCommand = 'ERROR' then
2016-06-28 15:21:47 +02:00
raise EStomp.Create(FormatErrorFrame(Frame));
if Frame.GetCommand = 'CONNECTED' then
begin
FSession := Frame.GetHeaders.Value('session');
FServerProtocolVersion := Frame.GetHeaders.Value('version'); // stomp 1.1
FServer := Frame.GetHeaders.Value('server'); // stomp 1.1
end;
{ todo: 'Call event?' }
except
on E: Exception do
begin
raise EStomp.Create(E.message);
end;
end;
end;
function TStompClient.Connected: boolean;
begin
2015-01-21 14:59:20 +01:00
{$IFDEF USESYNAPSE}
Result := Assigned(FSynapseTCP) and FSynapseConnected;
2015-01-21 14:59:20 +01:00
{$ELSE}
Result := Assigned(FTCP) and FTCP.Connected;
2015-01-21 14:59:20 +01:00
{$ENDIF}
end;
2016-09-23 22:36:36 +02:00
class function TStompClient.CreateAndConnect(Host: string; Port: Integer;
ClientID: string; AcceptVersion: TStompAcceptProtocol): IStompClient;
2016-08-02 18:15:11 +02:00
begin
Result := TStompClient.Create;
Result.Connect(Host, Port, ClientID, AcceptVersion);
end;
constructor TStompClient.Create;
begin
inherited;
2013-10-15 10:14:36 +02:00
FInTransaction := False;
FSession := '';
FUserName := 'guest';
FPassword := 'guest';
FHeaders := TStompHeaders.Create;
2016-07-26 17:41:05 +02:00
FTimeout := 200;
FReceiptTimeout := FTimeout;
2016-09-23 22:36:36 +02:00
FConnectionTimeout := 1000 * 10; // 10secs
end;
procedure TStompClient.DeInit;
begin
2015-01-21 14:59:20 +01:00
{$IFDEF USESYNAPSE}
FreeAndNil(FSynapseTCP);
2015-01-21 14:59:20 +01:00
{$ELSE}
FreeAndNil(FTCP);
2015-01-21 14:59:20 +01:00
{$ENDIF}
FreeAndNil(FTransactions);
end;
destructor TStompClient.Destroy;
begin
2013-10-15 10:14:36 +02:00
Disconnect;
DeInit;
inherited;
end;
2013-10-15 10:14:36 +02:00
procedure TStompClient.Disconnect;
var
Frame: IStompFrame;
begin
if Connected then
begin
2013-10-15 10:14:36 +02:00
Frame := TStompFrame.Create;
Frame.SetCommand('DISCONNECT');
SendFrame(Frame);
2015-01-21 14:59:20 +01:00
{$IFDEF USESYNAPSE}
FSynapseTCP.CloseSocket;
2013-10-15 10:14:36 +02:00
FSynapseConnected := False;
2015-01-21 14:59:20 +01:00
{$ELSE}
FTCP.Disconnect;
2015-01-21 14:59:20 +01:00
{$ENDIF}
end;
DeInit;
end;
2016-07-26 11:21:07 +02:00
function TStompClient.FormatErrorFrame(const AErrorFrame: IStompFrame): string;
2016-06-28 15:21:47 +02:00
begin
if AErrorFrame.GetCommand <> 'ERROR' then
raise EStomp.Create('Not an ERROR frame');
2016-09-23 22:36:36 +02:00
Result := AErrorFrame.GetHeaders.Value('message') + ': ' +
AErrorFrame.GetBody;
2016-06-28 15:21:47 +02:00
end;
function TStompClient.GetProtocolVersion: string;
begin
Result := FServerProtocolVersion;
end;
function TStompClient.GetServer: string;
begin
Result := FServer;
end;
function TStompClient.GetSession: string;
begin
Result := FSession;
end;
procedure TStompClient.Init;
begin
DeInit;
2015-01-21 14:59:20 +01:00
{$IFDEF USESYNAPSE}
FSynapseTCP := TTCPBlockSocket.Create;
FSynapseTCP.OnStatus := SynapseSocketCallBack;
FSynapseTCP.RaiseExcept := True;
2015-01-21 14:59:20 +01:00
{$ELSE}
FTCP := TIdTCPClient.Create(nil);
2015-01-21 14:59:20 +01:00
{$ENDIF}
FTransactions := TStringList.Create;
end;
{$IFDEF USESYNAPSE}
procedure TStompClient.SynapseSocketCallBack(Sender: TObject;
Reason: THookSocketReason; const Value: string);
begin
// As seen at TBlockSocket.ExceptCheck procedure, it SEEMS safe to say
// when an error occurred and is not a Timeout, the connection is broken
2016-09-23 22:36:36 +02:00
if (Reason = HR_Error) and (FSynapseTCP.LastError <> WSAETIMEDOUT) then
begin
2013-10-15 10:14:36 +02:00
FSynapseConnected := False;
end;
end;
{$ENDIF}
2016-09-23 22:36:36 +02:00
procedure TStompClient.MergeHeaders(var AFrame: IStompFrame;
var AHeaders: IStompHeaders);
var
i: Integer;
h: TKeyValue;
begin
if Assigned(AHeaders) then
if AHeaders.Count > 0 then
for i := 0 to AHeaders.Count - 1 do
begin
h := AHeaders.GetAt(i);
AFrame.GetHeaders.Add(h.Key, h.Value);
end;
// If the frame has some content, then set the length of that content.
if (AFrame.ContentLength > 0) then
2016-07-26 11:21:07 +02:00
AFrame.GetHeaders.Add('content-length', intToStr(AFrame.ContentLength));
end;
procedure TStompClient.Nack(const MessageID, TransactionIdentifier: string);
var
Frame: IStompFrame;
begin
Frame := TStompFrame.Create;
Frame.SetCommand('NACK');
Frame.GetHeaders.Add('message-id', MessageID);
if TransactionIdentifier <> '' then
Frame.GetHeaders.Add('transaction', TransactionIdentifier);
SendFrame(Frame);
end;
procedure TStompClient.Receipt(const ReceiptID: string);
var
Frame: IStompFrame;
begin
if Receive(Frame, FReceiptTimeout) then
begin
if Frame.GetCommand <> 'RECEIPT' then
raise EStomp.Create('Receipt command error');
if Frame.GetHeaders.Value('receipt-id') <> ReceiptID then
raise EStomp.Create('Receipt receipt-id error');
end;
end;
2016-09-23 22:36:36 +02:00
function TStompClient.Receive(out StompFrame: IStompFrame;
ATimeout: Integer): boolean;
begin
StompFrame := nil;
StompFrame := Receive(ATimeout);
Result := Assigned(StompFrame);
end;
function TStompClient.Receive(ATimeout: Integer): IStompFrame;
{$IFDEF USESYNAPSE}
function InternalReceiveSynapse(ATimeout: Integer): IStompFrame;
var
2015-01-21 14:59:20 +01:00
c: char;
s: string;
tout: boolean;
begin
2013-10-15 10:14:36 +02:00
tout := False;
Result := nil;
try
try
FSynapseTCP.SetRecvTimeout(ATimeout);
s := '';
try
while True do
begin
c := Chr(FSynapseTCP.RecvByte(ATimeout));
if c <> CHAR0 then
2016-09-23 22:36:36 +02:00
s := s + c
// should be improved with a string buffer (daniele.teti)
else
begin
c := Chr(FSynapseTCP.RecvByte(ATimeout));
Break;
end;
end;
except
on E: ESynapseError do
begin
if E.ErrorCode = WSAETIMEDOUT then
tout := True
else
raise;
end;
on E: Exception do
begin
raise;
end;
end;
if not tout then
begin
Result := StompUtils.CreateFrame(s + CHAR0);
end;
finally
s := '';
end;
except
on E: Exception do
begin
raise;
end;
end;
end;
{$ELSE}
function InternalReceiveINDY(ATimeout: Integer): IStompFrame;
var
2016-06-28 15:21:47 +02:00
s: string;
lSBuilder: TStringBuilder;
Headers: TIdHeaderList;
ContentLength: Integer;
Charset: string;
2015-01-21 14:59:20 +01:00
{$IF CompilerVersion < 24}
2016-06-28 15:21:47 +02:00
Encoding: TIdTextEncoding;
FreeEncoding: boolean;
2015-01-21 14:59:20 +01:00
{$ELSE}
2016-06-28 15:21:47 +02:00
Encoding: IIdTextEncoding;
2015-01-21 14:59:20 +01:00
{$ENDIF}
2016-06-28 15:21:47 +02:00
begin
Result := nil;
2016-06-28 15:21:47 +02:00
lSBuilder := TStringBuilder.Create(1024 * 4);
try
2016-07-26 11:21:07 +02:00
FTCP.Socket.ReadTimeout := ATimeout;
2016-06-28 15:21:47 +02:00
FTCP.Socket.DefStringEncoding :=
{$IF CompilerVersion < 24}TIdTextEncoding.UTF8{$ELSE}IndyTextEncoding_UTF8{$ENDIF};
try
2016-06-28 15:21:47 +02:00
// read command line
2016-07-26 17:41:05 +02:00
// repeat
2016-09-23 22:36:36 +02:00
s := FTCP.Socket.ReadLn(LF, ATimeout, -1,
FTCP.Socket.DefStringEncoding);
2016-07-26 17:41:05 +02:00
// until s <> '';
if s = '' then
Exit(nil);
2016-06-28 15:21:47 +02:00
lSBuilder.Append(s + LF);
// read headers
Headers := TIdHeaderList.Create(QuotePlain);
try
2016-06-28 15:21:47 +02:00
repeat
s := FTCP.Socket.ReadLn;
lSBuilder.Append(s + LF);
if s = '' then
Break;
2016-06-28 15:21:47 +02:00
Headers.Add(s);
until False;
// read body
//
// NOTE: non-text data really should be read as a Stream instead of a String!!!
//
if IsHeaderMediaType(Headers.Values['content-type'], 'text') then
begin
2016-06-28 15:21:47 +02:00
Charset := Headers.Params['content-type', 'charset'];
if Charset = '' then
Charset := 'utf-8';
Encoding := CharsetToEncoding(Charset);
{$IF CompilerVersion < 24}
FreeEncoding := True;
{$ENDIF}
end
else
begin
2016-06-28 15:21:47 +02:00
Encoding := IndyTextEncoding_8Bit();
{$IF CompilerVersion < 24}
FreeEncoding := False;
{$ENDIF}
end;
{$IF CompilerVersion < 24}
try
{$ENDIF}
if Headers.IndexOfName('content-length') <> -1 then
begin
// length specified, read exactly that many bytes
ContentLength := IndyStrToInt(Headers.Values['content-length']);
if ContentLength > 0 then
begin
s := FTCP.Socket.ReadString(ContentLength, Encoding);
lSBuilder.Append(s);
end;
// frame must still be terminated by a null
2016-07-26 17:41:05 +02:00
FTCP.Socket.ReadLn(#0 + LF);
2016-06-28 15:21:47 +02:00
end
else
2016-06-28 15:21:47 +02:00
begin
// no length specified, body terminated by frame terminating null
2016-07-26 17:41:05 +02:00
s := FTCP.Socket.ReadLn(#0 + LF, Encoding);
2016-06-28 15:21:47 +02:00
lSBuilder.Append(s);
end;
lSBuilder.Append(#0);
{$IF CompilerVersion < 24}
finally
if FreeEncoding then
Encoding.Free;
end;
2016-06-28 15:21:47 +02:00
{$ENDIF}
finally
Headers.Free;
end;
2016-06-28 15:21:47 +02:00
except
on E: Exception do
begin
2016-06-28 15:21:47 +02:00
if lSBuilder.Length > 0 then
raise EStomp.Create(E.message + sLineBreak + lSBuilder.toString)
else
raise;
end;
end;
2016-06-28 15:21:47 +02:00
Result := StompUtils.CreateFrame(lSBuilder.toString);
if Result.GetCommand = 'ERROR' then
raise EStomp.Create(FormatErrorFrame(Result));
finally
lSBuilder.Free;
end;
end;
{$ENDIF}
begin
2015-01-21 14:59:20 +01:00
{$IFDEF USESYNAPSE}
2013-10-15 10:14:36 +02:00
Result := InternalReceiveSynapse(ATimeout);
2015-01-21 14:59:20 +01:00
{$ELSE}
2013-10-15 10:14:36 +02:00
Result := InternalReceiveINDY(ATimeout);
2013-10-14 14:28:17 +02:00
2015-01-21 14:59:20 +01:00
{$ENDIF}
end;
function TStompClient.Receive: IStompFrame;
begin
Result := Receive(FTimeout);
end;
2016-09-23 22:36:36 +02:00
procedure TStompClient.Send(QueueOrTopicName: string; TextMessage: string;
Headers: IStompHeaders);
var
Frame: IStompFrame;
begin
Frame := TStompFrame.Create;
Frame.SetCommand('SEND');
Frame.GetHeaders.Add('destination', QueueOrTopicName);
Frame.SetBody(TextMessage);
MergeHeaders(Frame, Headers);
SendFrame(Frame);
end;
procedure TStompClient.Send(QueueOrTopicName: string; TextMessage: string;
TransactionIdentifier: string; Headers: IStompHeaders);
var
Frame: IStompFrame;
begin
Frame := TStompFrame.Create;
Frame.SetCommand('SEND');
Frame.GetHeaders.Add('destination', QueueOrTopicName);
Frame.GetHeaders.Add('transaction', TransactionIdentifier);
Frame.SetBody(TextMessage);
MergeHeaders(Frame, Headers);
SendFrame(Frame);
end;
procedure TStompClient.SendFrame(AFrame: IStompFrame);
begin
2015-01-21 14:59:20 +01:00
{$IFDEF USESYNAPSE}
if Assigned(FOnBeforeSendFrame) then
FOnBeforeSendFrame(AFrame);
FSynapseTCP.SendString(AFrame.output);
if Assigned(FOnAfterSendFrame) then
FOnAfterSendFrame(AFrame);
2015-01-21 14:59:20 +01:00
{$ELSE}
// FTCP.IOHandler.write(TEncoding.ASCII.GetBytes(AFrame.output));
if Assigned(FOnBeforeSendFrame) then
FOnBeforeSendFrame(AFrame);
2013-12-04 11:42:45 +01:00
2015-01-21 14:59:20 +01:00
{$IF CompilerVersion < 25}
2013-12-04 11:42:45 +01:00
FTCP.IOHandler.write(TEncoding.UTF8.GetBytes(AFrame.output));
2015-01-21 14:59:20 +01:00
{$IFEND}
{$IF CompilerVersion >= 25}
2013-05-10 18:00:10 +02:00
FTCP.IOHandler.write(IndyTextEncoding_UTF8.GetBytes(AFrame.output));
2015-01-21 14:59:20 +01:00
{$IFEND}
if Assigned(FOnAfterSendFrame) then
FOnAfterSendFrame(AFrame);
2015-01-21 14:59:20 +01:00
{$ENDIF}
2016-09-23 22:36:36 +02:00
end;
2016-09-23 22:36:36 +02:00
procedure TStompClient.SetConnectionTimeout(const Value: UInt32);
begin
FConnectionTimeout := Value;
end;
function TStompClient.SetPassword(const Value: string): IStompClient;
begin
FPassword := Value;
Result := Self;
end;
procedure TStompClient.SetReceiptTimeout(const Value: Integer);
begin
FReceiptTimeout := Value;
end;
2016-09-23 22:36:36 +02:00
function TStompClient.SetReceiveTimeout(const AMilliSeconds: Cardinal)
: IStompClient;
begin
FTimeout := AMilliSeconds;
Result := Self;
end;
function TStompClient.SetUserName(const Value: string): IStompClient;
begin
FUserName := Value;
Result := Self;
end;
2016-09-23 22:36:36 +02:00
procedure TStompClient.Subscribe(QueueOrTopicName: string;
Ack: TAckMode = TAckMode.amAuto; Headers: IStompHeaders = nil);
var
Frame: IStompFrame;
begin
Frame := TStompFrame.Create;
Frame.SetCommand('SUBSCRIBE');
2016-09-23 22:36:36 +02:00
Frame.GetHeaders.Add('destination', QueueOrTopicName)
.Add('ack', StompUtils.AckModeToStr(Ack));
if Headers <> nil then
MergeHeaders(Frame, Headers);
SendFrame(Frame);
end;
procedure TStompClient.Unsubscribe(Queue: string);
var
Frame: IStompFrame;
begin
Frame := TStompFrame.Create;
Frame.SetCommand('UNSUBSCRIBE');
Frame.GetHeaders.Add('destination', Queue);
SendFrame(Frame);
end;
end.