移动端的消息推送大家都体验过,智能手机上一大堆广告等各种消息会不时从消息栏中弹出来骚扰你。

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();

  if Assigned(StompFrame) then

    WriteLn(StompFrame.GetBody);

  StompFrame := StompSub1.Receive();

  if Assigned(StompFrame) then

    WriteLn(StompFrame.GetBody);

  StompFrame := StompSub2.Receive();

  if Assigned(StompFrame) then

    WriteLn(StompFrame.GetBody);

  StompFrame := StompSub2.Receive();

  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();

  if Assigned(StompFrame) then

    WriteLn(StompFrame.Output);

  StompFrame := StompSub1.Receive();

  if Assigned(StompFrame) then

    WriteLn(StompFrame.Output);

  StompFrame := StompSub2.Receive();

  if Assigned(StompFrame) then

    WriteLn(StompFrame.Output);

  StompFrame := StompSub2.Receive();

  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.

Delphi消息推送的更多相关文章

  1. 采用MQTT协议实现android消息推送(2)MQTT服务端与客户端软件对比、android客户端示列表

    1.服务端软件对比 https://github.com/mqtt/mqtt.github.io/wiki/servers 名称(点名进官网) 特性 简介 收费 支持的客户端语言 IBM MQ 完整的 ...

  2. SignalR快速入门 ~ 仿QQ即时聊天,消息推送,单聊,群聊,多群公聊(基础=》提升)

     SignalR快速入门 ~ 仿QQ即时聊天,消息推送,单聊,群聊,多群公聊(基础=>提升,5个Demo贯彻全篇,感兴趣的玩才是真的学) 官方demo:http://www.asp.net/si ...

  3. 【原创分享·微信支付】C# MVC 微信支付之微信模板消息推送

    微信支付之微信模板消息推送                    今天我要跟大家分享的是“模板消息”的推送,这玩意呢,你说用途嘛,那还是真真的牛逼呐.原因在哪?就是因为它是依赖微信生存的呀,所以他能不 ...

  4. 基于SignalR的消息推送与二维码描登录实现

    1 概要说明 使用微信扫描登录相信大家都不会陌生吧,二维码与手机结合产生了不同应用场景,基于二维码的应用更是比较广泛.为了满足ios.android客户端与web短信平台的结合,特开发了基于Singl ...

  5. C# BS消息推送 SignalR介绍(一)

    1. 前言 本文是根据网上前人的总结得出的. 环境: SignalR2.x,VS2015,Win10 介绍 1)SignalR能用来持久客户端与服务端的连接,让我们便于开发一些实时的应用,例如聊天室在 ...

  6. iOS 之消息推送(个推)---个人小结

    前言:自从上个星期开始整这个推送,弄了差不多一个星期,今天终于给整好了,因此现在来记录这段"奇妙"的旅程. 我们公司使用的消息推送是用的第三方--个推,这里不得不说一下,个推的技术 ...

  7. WebSocket与消息推送

    B/S结构的软件项目中有时客户端需要实时的获得服务器消息,但默认HTTP协议只支持请求响应模式,这样做可以简化Web服务器,减少服务器的负担,加快响应速度,因为服务器不需要与客户端长时间建立一个通信链 ...

  8. 分分钟搞定IOS远程消息推送

    一.引言 IOS中消息的推送有两种方式,分别是本地推送和远程推送,本地推送在http://my.oschina.net/u/2340880/blog/405491这篇博客中有详细的介绍,这里主要讨论远 ...

  9. 基于ajax与msmq技术的消息推送功能实现

    周末在家捣鼓了一下消息推送的简单例子,其实也没什么技术含量,欢迎大伙拍砖.我设计的这个推送demo是基于ajax长轮询+msmq消息队列来实现的,具体交互过程如下图: 先说说这个ajax长轮询,多长时 ...

随机推荐

  1. javaScript:让文本框内的最后一个文字的后面获得焦点

    //当失去交点以后 让文本框内的文字获得焦点 并且光标移到最后一个字后面 function myfocus(myid) { if(isNav){ document.getElementById(myi ...

  2. 在浏览器中直接调用webservice的正确写法

    此文章针对webwork+spring+hibernate的工程,对于其他框架应该一样适用,首先在wsdd文件中找到所需webservice的名称,例如以下写法: <service name=& ...

  3. 如何三招帮你排查Linux中的硬件问题

    下列贴士帮助你更快速更轻松地为Linux中的硬件排查故障.许多不同的因素可能导致Linux硬件出现问题:在你开始尝试诊断之前,了解最常见的问题以及最有可能找到原因的环节是明智之举. Linux服务器在 ...

  4. Eclipse小技巧:收起outline的头文件

  5. Android性能优化系列之App启动优化

    Android性能优化系列之布局优化 Android性能优化系列之内存优化 Android性能优化系列之apk瘦身 应用的启动速度缓慢是我们在开发过程中常常会遇到的问题,比方启动缓慢导致的黑屏.白屏问 ...

  6. Flask调试

    1.AttributeError: 'NoneType' object has no attribute 'app' 原因:直接在py中调用视图函数,但没有上下文,导致出错 2.不转换html代码 { ...

  7. C# 源码 AForge.NET

    AForge.NET是一个专门为开发者和研究者基于C#框架设计的,他包括计算机视觉与人工智能,图像处理,神经网络,遗传算法,机器学习,模糊系统,机器人控制等领域.这个框架由一系列的类库组成.主要包括有 ...

  8. windows下根据进程ID强制杀死进程

    [windows 进程ID PID]NTSD命令详解 1. ntsd -c q -p PID 2. ntsd -c q -pn ImageName 比如:ntsd -c q -pn qq.exe -c ...

  9. C#.NET常见问题(FAQ)-如何使用变量动态添加控件

    可以先声明控件的数组   然后动态的添加到窗体中   在程序执行的时候你还可以随时通过数组下标访问到这些控件   更多教学视频和资料下载,欢迎关注以下信息: 我的优酷空间: http://i.youk ...

  10. 解决Android Studio无法下载sdk的问题

    因为google被墙了,android sdk无法下载.然后各种百度,都是说让设置代理,给的代理地址一般都是用的下面这个代理服务器: 大连东软信息学院镜像服务器地址: mirrors.neusoft. ...