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长轮询,多长时 ...
随机推荐
- 构建配置 defaultConfig signingConfigs buildTypes productFlavors dependencies
测试项目位置:https://github.com/baiqiantao/FragmentTest.git 项目结构: project 目录的 build.gradle 文件 // Top-level ...
- C# 播放铃声最简短的代码实现方式
因为只是做一个软件的闹铃播放效果,到网上找的时候试了几种,哎,都失败了,而且代码挺杂的,最终一句搞定了: 1 // 窗体加载事件 2 private void Time ...
- android动手写控件系列——老猪叫你写相机
前记:Android这个开源而自由的系统,为我们带来开发便利,同时也埋下太多的深坑.例如调用系统自带的相机就会出现照片丢失,或者其他各种各样的问题.因此,看来自定义一个相机十分的必要. 要自定义相机我 ...
- python用post访问restful服务接口
具体代码如下: import requests import json data={"]} url="http://XXXXX" data_json = json.dum ...
- Mahout源码目录说明
http://www.cnblogs.com/dlts26/archive/2011/08/23/2150230.html mahout项目是由多个子项目组成的,各子项目分别位于源码的不同目录下,下面 ...
- VIM的buffers
原文:http://ju.outofmemory.cn/entry/13522 重新在不同的 tab 中打开多个关闭的buffer 文件, https://stackoverflow.com/ques ...
- Java-JUC(三):原子性变量与CAS算法
原子性 并发程序正确地执行,必须要保证原子性.可见性以及有序性.只要有一个没有被保证,就有可能会导致程序运行不正确. 原子性:一个操作或多个操作要么全部执行完成且执行过程不被中断,要么就不执行. 可见 ...
- ASP入门(十六)-ASP开发的规范
毋容置疑,在开发中遵守一套规范,将会有利于提高代码的可读性,较低后期维护成本. 文件存放目录规范 js 目录下存放着页面所使用的 JavaScript 脚本文件,因为我们可能用到第三方提供的免费的 J ...
- Office WPS如何在页眉页脚添加一条横线
点击样式,页眉,修改 然后设置格式,可以添加一条或者两条横线,也可以设置不同的线型 最后效果如下图所示
- JAVA遍历HashMap和ArrayList
List Map 基础信息 HashMap 最近写程序经常需要遍历集合,所以总结一下内容: 一.简单实现 Map map = new HashMap(); for(Object o : map.key ...