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

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. Hadoop视频教程汇总

    一 慕课网 1.Hadoop大数据平台架构与实践--基础篇(已学习) 链接:https://www.imooc.com/learn/391 2.Hadoop进阶(已学习) 链接:https://www ...

  2. Spring Boot 集成Shiro和CAS

    Spring Boot 集成Shiro和CAS 标签: springshirocas 2016-01-17 23:03 35765人阅读 评论(22) 收藏 举报  分类: Spring(42)  版 ...

  3. Safari不兼容Javascript中的Date问题

    在IOS5以上版本(不包含IOS5)中的Safari浏览器能正确解释出Javascript中的 new Date('2013-10-21') 的日期对象,但是在IOS5版本里面的Safari解释new ...

  4. 使用Java语言开发微信公众平台(八)——自定义菜单功能

    随着上一篇文章的结束,我们已经实现了所有消息的类型的回复功能.今天,我们来学习更加高大上,也更加重要的自定义菜单功能. 一.了解自定义菜单 自定义菜单是微信公众平台最常用也是最重要的功能之一.根据微信 ...

  5. Spring(九):Spring配置Bean(二)自动装配的模式、Bean之间的关系

    XML配置里的Bean自动装配 Spring IOC容器可以自动装配Bean,需要做的仅仅是在<bean>的autowire属性里指定自动装配的模式,模式包含:byType,byName, ...

  6. TeamView提示商业用途禁止使用

    一.问题 TM被提示商业用途,用一会就断开连接,或者是提示五分钟后关闭 二.解决思路 2.1:删除原来的TM信息 首先需要卸载TM,其次需要去注册表,运行→regedit,打开注册表,删除相关的tea ...

  7. Uniform and Interpolator Packing的作用

    All of the packing that is done is completely transparent to the user of the OpenGL ES Shading Langu ...

  8. OpenGL ES 3.0片段着色器(四)

    片段着色器流程图 片段着色器(fragment shader)实现了一个通用的可编程操作片段的方法.片段着色器执行由 光栅化生成的每个片段. • Shader program(着色器程序)—片段着色器 ...

  9. Linux环境下网络编程杂谈《转》

    今天我们说说“Pre-网络编程”.内容比较杂,但都是在做网络应用程序开发过程中经常要遇到的问题. 一.大端.小端和网络字节序 小端字节序:little-endian,将低字节存放在内存的起始地址: 大 ...

  10. iOS编程(双语版) - 视图 - 基本概念

    1. 什么是视图? 视图显示为手机上的一块矩形区域,管理该区域的所有屏幕显示,它是UIView或者UIView的子类. 视图既可以从xib生成,也可以用代码生成. 2. 窗口 窗口是UIWindow或 ...