【Flex CookBook】 如使用BlazeDS消息机制

本文介绍如何利用BlazeDS消息服务实现发布/订阅模式,支持数据推送和协作应用。通过配置messaging-config.xml文件设置消息目标及交互通道,演示了简单的聊天应用程序案例。

How to use BlazeDS Messaging 如使用BlazeDS消息机制

问题摘要

需要一个发布/订阅的消息架构来实现数据推送或是协作式的程序

解决摘要

通过BlazeDS消息服务支持发布/订阅消息,消息管理服务管理着FLEX客户端能够订阅或者发布的一系列目标消息

解释

Flex提供了两个组件,生产者消费者分别用来发布和订阅一个目标消息。要订阅一个目标消息应该使用consumer类的subscribe()方法,当一个消息被发布到你订阅的目标对象上,消息事件就会触发消费者consumer类。使用BlazeDS的发布和订阅功能非常简单。下面是一个例子

messaging-config.xml文件来配置,配置消息目标的一个关键就是配置用来在客户端和服务端之间交互的数据通道,使用BlazeDS,一个销售目标通常使用流或者轮询作为交互通道。

<? xmlversion = " 1.0 " encoding = " utf-8 " ?>

< mx:Applicationxmlns:mx = http: // www.adobe.com/2006/mxml

creationComplete
= " consumer.subscribe() " >

< mx:Script >

<! [CDATA[



importmx.messaging.messages.AsyncMessage;

importmx.messaging.messages.IMessage;



private functionsend(): void

... {

varmessage:IMessage
=newAsyncMessage();

message.body.chatMessage
=msg.text;

producer.send(message);

msg.text
="";

}


private functionmessageHandler(message:IMessage): void

... {

log.text
+=message.body.chatMessage+" ";

}


]]
>

</ mx:Script >

< mx:Producerid = " producer " destination = " chat " />

< mx:Consumerid = " consumer " destination = " chat " message = " messageHandler(event.message) " />

< mx:Paneltitle = " Chat " width = " 100% " height = " 100% " >

< mx:TextAreaid = " log " width = " 100% " height = " 100% " />

< mx:ControlBar >

< mx:TextInputid = " msg " width = " 100% " enter = " send() " />

< mx:Buttonlabel = " Send " click = " send() " />

</ mx:ControlBar >

</ mx:Panel >

</ mx:Application >

消息目标通过

使用流通道,那么服务端的响应直到通道关闭才关闭,使得服务端能够发送大量的数据到客户端,HTTP迎接通常不是双向,这意味着使用AMF或者HTTP通道传输数据需要两个HTTP连接完成双向的数据传输,一个链接负责从服务端到客户端的数据传输,另一个负责从客户端到服务端的数据传输,数据传输一完成这两个连接就立刻被释放到浏览器的连接池

. 如果数据对象不是马上能够获得(一次轮询无法完成传输),一个轮询通道可以配置一简单的间隔。在两种情况下每一次轮询都完成相应的传输请求,游览器的HTTP1.1连接通常缺省是永久的,所以游览器可能会回收现有的HTTP1.1连接来发送的轮询请求,减少了轮询所花费的时间

源文档

<http://www.adobe.com/cfusion/communityengine/index.cfm?event=showdetails&productId=2&postId=7765>

How to use BlazeDS Messaging

Problem Summary

You need a publish/subscribe messaging infrastructure to build data-push or collaborative applications.

Solution Summary

Flex supports publish/subscribe messaging through the BlazeDS Message Service. The Message Service manages a set of destinations that Flex clients can publish and subscribe to.

Explanation

Flex provides two components, Producer and Consumer that you use to respectively publish and subscribe to a destination. To subscribe to a destination, you use the subscribe() method of the Consumer class. When a message is published to a destination you subscribed to, the message event is triggered on the Consumer.

Using the BlazeDS publish/subscribe capability is easy. Here is an example of a simple chat application:

<? xmlversion = " 1.0 " encoding = " utf-8 " ?>

< mx:Applicationxmlns:mx = http: // www.adobe.com/2006/mxml

creationComplete
= " consumer.subscribe() " >

< mx:Script >

<! [CDATA[



importmx.messaging.messages.AsyncMessage;

importmx.messaging.messages.IMessage;



private functionsend(): void

... {

varmessage:IMessage
=newAsyncMessage();

message.body.chatMessage
=msg.text;

producer.send(message);

msg.text
="";

}


private functionmessageHandler(message:IMessage): void

... {

log.text
+=message.body.chatMessage+" ";

}


]]
>

</ mx:Script >

< mx:Producerid = " producer " destination = " chat " />

< mx:Consumerid = " consumer " destination = " chat " message = " messageHandler(event.message) " />

< mx:Paneltitle = " Chat " width = " 100% " height = " 100% " >

< mx:TextAreaid = " log " width = " 100% " height = " 100% " />

< mx:ControlBar >

< mx:TextInputid = " msg " width = " 100% " enter = " send() " />

< mx:Buttonlabel = " Send " click = " send() " />

</ mx:ControlBar >

</ mx:Panel >

</ mx:Application >

Messaging destinations are configured in messaging-config.xml. A key element of a destination configuration is the channel used to exchange data between the client and the server. Using BlazeDS, a messaging destination typically uses a streaming or a polling channel.

Using a streaming channel, the server response is left open until the channel connection is closed, allowing the server to send down incremental chunks of data to the client. HTTP connections are not duplex. This means that a single streaming AMF or HTTP channel actually requires two browser HTTP connections in order to send data in both directions. One for the streamed response from the server to the client that the channel hangs on to, and a second transient connection, drawn from the browser pool only when data needs to be sent to the server. This second transient connection is immediately released back to the browsers connection pool.

A polling channel can be configured with a simple interval or with a sever wait if data is not immediately available (long polling). In either case, each poll response completes the request. Browser HTTP 1.1 connections are persistent by default, so the browser will likely recycle existing HTTP connections to send subsequent poll requests which lowers the overhead for polling.

基于数据驱动的 Koopman 算子的递归神经网络模型线性化,用于纳米定位系统的预测控制研究(Matlab代码实现)内容概要:本文围绕“基于数据驱动的Koopman算子的递归神经网络模型线性化”展开,旨在研究纳米定位系统的预测控制方法。通过结合数据驱动技术与Koopman算子理论,将非线性系统动态近似为高维线性系统,进而利用递归神经网络(RNN)建模并实现系统行为的精确预测。文中详细阐述了模型构建流程、线性化策略及在预测控制中的集成应用,并提供了完整的Matlab代码实现,便于科研人员复现实验、优化算法并拓展至其他精密控制系统。该方法有效提升了纳米级定位系统的控制精度与动态响应性能。; 适合人群:具备自动控制、机器学习或信号处理背景,熟悉Matlab编程,从事精密仪器控制、智能制造或先进控制算法研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①实现非线性动态系统的数据驱动线性化建模;②提升纳米定位平台的轨迹跟踪与预测控制性能;③为高精度控制系统提供可复现的Koopman-RNN融合解决方案; 阅读建议:建议结合Matlab代码逐段理解算法实现细节,重点关注Koopman观测矩阵构造、RNN训练流程与模型预测控制器(MPC)的集成方式,鼓励在实际硬件平台上验证并调整参数以适应具体应用场景。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值