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();
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消息推送的更多相关文章
- 采用MQTT协议实现android消息推送(2)MQTT服务端与客户端软件对比、android客户端示列表
1.服务端软件对比 https://github.com/mqtt/mqtt.github.io/wiki/servers 名称(点名进官网) 特性 简介 收费 支持的客户端语言 IBM MQ 完整的 ...
- SignalR快速入门 ~ 仿QQ即时聊天,消息推送,单聊,群聊,多群公聊(基础=》提升)
SignalR快速入门 ~ 仿QQ即时聊天,消息推送,单聊,群聊,多群公聊(基础=>提升,5个Demo贯彻全篇,感兴趣的玩才是真的学) 官方demo:http://www.asp.net/si ...
- 【原创分享·微信支付】C# MVC 微信支付之微信模板消息推送
微信支付之微信模板消息推送 今天我要跟大家分享的是“模板消息”的推送,这玩意呢,你说用途嘛,那还是真真的牛逼呐.原因在哪?就是因为它是依赖微信生存的呀,所以他能不 ...
- 基于SignalR的消息推送与二维码描登录实现
1 概要说明 使用微信扫描登录相信大家都不会陌生吧,二维码与手机结合产生了不同应用场景,基于二维码的应用更是比较广泛.为了满足ios.android客户端与web短信平台的结合,特开发了基于Singl ...
- C# BS消息推送 SignalR介绍(一)
1. 前言 本文是根据网上前人的总结得出的. 环境: SignalR2.x,VS2015,Win10 介绍 1)SignalR能用来持久客户端与服务端的连接,让我们便于开发一些实时的应用,例如聊天室在 ...
- iOS 之消息推送(个推)---个人小结
前言:自从上个星期开始整这个推送,弄了差不多一个星期,今天终于给整好了,因此现在来记录这段"奇妙"的旅程. 我们公司使用的消息推送是用的第三方--个推,这里不得不说一下,个推的技术 ...
- WebSocket与消息推送
B/S结构的软件项目中有时客户端需要实时的获得服务器消息,但默认HTTP协议只支持请求响应模式,这样做可以简化Web服务器,减少服务器的负担,加快响应速度,因为服务器不需要与客户端长时间建立一个通信链 ...
- 分分钟搞定IOS远程消息推送
一.引言 IOS中消息的推送有两种方式,分别是本地推送和远程推送,本地推送在http://my.oschina.net/u/2340880/blog/405491这篇博客中有详细的介绍,这里主要讨论远 ...
- 基于ajax与msmq技术的消息推送功能实现
周末在家捣鼓了一下消息推送的简单例子,其实也没什么技术含量,欢迎大伙拍砖.我设计的这个推送demo是基于ajax长轮询+msmq消息队列来实现的,具体交互过程如下图: 先说说这个ajax长轮询,多长时 ...
随机推荐
- 跨平台APP----对Cordova,APPCan,DCloud,APICloud四大平台的分析
前言: 移动开发是未来一个很重要的IT领域,而跨平台开发将是这一领域最重要的事情. ----谷震平 一 兵器谱 在国外,最大的是Cordova(PhoneGap,2011年广泛流行), ...
- [Math]理解卡尔曼滤波器 (Understanding Kalman Filter)
1. 卡尔曼滤波器介绍 卡尔曼滤波器的介绍, 见 Wiki 这篇文章主要是翻译了 Understanding the Basis of the Kalman Filter Via a Simple a ...
- Java7,AutoClosable
Java 7的try-with-resource语法的目的是提高Java开发人员的效率,使得他们不需要在编写代码时考虑资源释放问题,大多数的这类"清理"问题是由于异常发生时清理方法 ...
- AVR单片机最小系统 基本硬件线路与分析
单片机最小系统 单片机最小系统设计 AVR基本硬件线路设计与分析 (ATmega16功能小板) AVR DB-CORE Ver2.3 Atmega16开发板 本站商城提供本最小系统销售:99元 AV ...
- [Javascript] Check both prop exists and value is valid
Sometime you need to check one prop exists on the object and value should not be ´null´ or ´undefine ...
- XE6入门(二)项目中的文件
XE6中项目文件为DPR,查看方法请参考一下以前写过的博文: "Delphi项目构成之项目文件DPR" 项目文件DPR 通过主菜单[Project | View Source],就 ...
- linux 常用命令--------雪松整理
linux 常用命令--------雪松整理 博客: http://hi.baidu.com/quanzhou722/blog错误在所难免,还望指正!========================= ...
- 使用Filter过滤非法内容
1.首先,需要编写一个响应的封装器ResponseReplaceWrapper,用它来缓存response中的内容,代码如下: ResponseReplaceWrapper.java package ...
- VMware vSphere 5.1 群集深入解析(一)
http://virtualbox.blog.51cto.com/531002/1168293 VMware vSphere 5.1 Clustering Deepdive HA.DRS.Storag ...
- px 和 em 的区别
相同点:px和em都是长度单位: 异同点:px的值是固定的,指定是多少就是多少,计算比较容易.em得值不是固定的,并且em会继承父级元素的字体大小.浏览器的默认字体高都是16px.所以未经调整的浏览器 ...