程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> Delphi消息推送,delphi消息推

Delphi消息推送,delphi消息推

編輯:Delphi

Delphi消息推送,delphi消息推


移動端的消息推送大家都體驗過,智能手機上一大堆廣告等各種消息會不時從消息欄中彈出來騷擾你。

PC程序中我們有時也會用到消息推送,比如通知之類。通常我們使用的方法可能更多地使用Socket之類來處理,有時效率更低的方法是做數據庫的輪詢。如果終端多、消息多,數據庫的輪詢方式是肯定不能被接受的。

現在比較流行的消息服務器有很多,像Apache的ActiveMQ,升級產品是Apollo,RabbitMQ,還有號稱速度最快的ZeroMQ,等等很多。這些消息服務器大多支持各種平台,如Java,PHP,Python等,也大多對Delphi支持。

Delphi下比較著名的客戶端是Habari的客戶端,官方主頁:

https://www.habarisoft.com/

上面有針對ActiveMQ等的專用客戶端。

還有這個開源作品:

http://www.danieleteti.it/stomp-client/

代碼托管在:

http://code.google.com/p/delphistompclient/

ActiveMQ等對消息的持久化支持比較簡單,配置一下就可以,這個百度一下就一大把,關鍵是通過連接消息服務器,可以使客戶端簡單地處理消息,做到高效、實時。

 

下面摘錄一點簡單的Delphi使用stomp協議客戶端的應用吧,就是上面提到的那個開源作品:

 

Stomp Client

Stomp protocol provides an interoperable wire format so that any of the available Stomp Clients can communicate with any Stomp Message Broker to provide easy and widespread messaging interop among languages, platforms and brokers.

The Delphi Stomp Client is an open source implementation of the STOMP protocol for Delphi 2010 (should work with Delphi 2007 and 2009 too) and FreePascal 2.4.x.

This Delphi Stomp Client isn’t an attempt to copy JMS client architecture to Delphi. So aren’t included some JMS specific features like message transformations.

This stomp client is actually tested on ActiveMQ 5.2 and ActiveMQ 5.3, but should work with every STOMP compliant server.

In Delphi you can use the built-in INDY component suite or the OpenSource Synapse acording with a compiler directive. In StompClient.pas there is following definitions:

unit StompClient;


 


// For FreePascal users:


// Automatically selected synapse tcp library


{$IFDEF FPC}


{$MODE DELPHI}


{$DEFINE USESYNAPSE} //FREEPASCAL ALWAYS USE SYNAPSE


{$ENDIF}


// For Delphi users:


// Decomment following line to use synapse also in Delphi


{ .$DEFINE USESYNAPSE } //DELPHI USERS CAN USE INDY OR SYNAPSE

 

Some examples of basic functionalities (not real world example, use included examples instead):

program SimpleMessaging;


{$APPTYPE CONSOLE}


 


uses


  SysUtils, StompClient, StompTypes;


 


procedure Example_Pub_Subscriber;


var


  StompPub, StompSubscriber: IStompClient;


  StompFrame: IStompFrame;


begin


  WriteLn('==> Example_Pub_Subscriber');


  StompSubscriber := StompUtils.NewStomp('127.0.0.1'); // default port


  StompSubscriber.Subscribe('/topic/dummy');


  StompPub := StompUtils.NewStomp('127.0.0.1'); // default port


  StompPub.Send('/topic/dummy', 'Some test message');


  repeat


    StompFrame := StompSubscriber.Receive;


  until Assigned(StompFrame);


  WriteLn(StompFrame.GetBody); // Print "Some test message"


  WriteLn;


end;


 


procedure Example_OnePub_TwoSubscriber;


var


  StompPub, StompSub1, StompSub2: IStompClient;


  StompFrame: IStompFrame;


begin


  WriteLn('==> Example_OnePub_TwoSubscriber');


  StompSub1 := StompUtils.NewStomp('127.0.0.1'); // default port


  StompSub2 := StompUtils.NewStomp('127.0.0.1'); // default port


  StompSub1.Subscribe('/topic/dummy');


  StompSub2.Subscribe('/topic/dummy');


 


 


  StompPub := StompUtils.NewStomp('127.0.0.1'); // default port


  StompPub.Send('/topic/dummy', 'First test message on a topic');


  StompPub.Send('/topic/dummy', 'Second test message on a topic');


 


  StompFrame := StompSub1.Receive(2000);


  if Assigned(StompFrame) then


    WriteLn(StompFrame.GetBody);


  StompFrame := StompSub1.Receive(2000);


  if Assigned(StompFrame) then


    WriteLn(StompFrame.GetBody);


 


  StompFrame := StompSub2.Receive(2000);


  if Assigned(StompFrame) then


    WriteLn(StompFrame.GetBody);


  StompFrame := StompSub2.Receive(2000);


  if Assigned(StompFrame) then


    WriteLn(StompFrame.GetBody);


  WriteLn;


end;


 


procedure Example_PointToPoint;


var


  StompPub, StompSub1, StompSub2: IStompClient;


  StompFrame: IStompFrame;


begin


  WriteLn('==> Example_PointToPoint');


  StompSub1 := StompUtils.NewStomp('127.0.0.1'); // default port


  StompSub2 := StompUtils.NewStomp('127.0.0.1'); // default port


  StompSub1.Subscribe('/queue/dummy');


  StompSub2.Subscribe('/queue/dummy');


 


  StompPub := StompUtils.NewStomp('127.0.0.1'); // default port


  StompPub.Send('/queue/dummy', 'First test message on a queue');


  StompPub.Send('/queue/dummy', 'Second test message on a queue');


 


  StompFrame := StompSub1.Receive(2000);


  if Assigned(StompFrame) then


    WriteLn(StompFrame.Output);


  StompFrame := StompSub1.Receive(2000);


  if Assigned(StompFrame) then


    WriteLn(StompFrame.Output);


 


  StompFrame := StompSub2.Receive(2000);


  if Assigned(StompFrame) then


    WriteLn(StompFrame.Output);


  StompFrame := StompSub2.Receive(2000);


  if Assigned(StompFrame) then


    WriteLn(StompFrame.Output);


 


  WriteLn;


end;


 


 


begin


  try


    Example_Pub_Subscriber;


    Example_OnePub_TwoSubscriber;


    Example_PointToPoint;


  except


    on E: Exception do


      WriteLn(E.ClassName, ': ', E.message);


  end;


end.

 

DelphiStompClient have also a simple listener for asynchronous use. To use the listener you should implement follwing interface:

IStompClientListener = interface


    ['{C4C0D932-8994-43FB-9D32-A03FE86AEFE4}']


    procedure OnMessage(StompFrame: IStompFrame);


  end;

 

Also a normal form can be used:

 TForm1 = class(TForm, IStompClientListener)


  …


  public


    procedure OnMessage(StompFrame: IStompFrame);  //Called by the stomp receiver


  end;

 

And then you can manager your message in a simple form’s method.

There are many features in this STOMP client. I’ve tried to respect all the STOMP specs also in the transactions side (http://stomp.codehaus.org/Protocol).

You can find all the source code and the examples at following Google Code Project:
http://code.google.com/p/delphistompclient/

The FreePascal version is actually mantained by Daniel Gaspary, thank Daniel.

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved