这个其实是基于 blazeds 的消息过滤.官方关于消息过滤的参考文档:
http://livedocs.adobe.com/blazeds/1/blazeds_devguide/help.html?content=messaging_6.html#154656
首先要了解一点,blazeds 的消息机制是基于订阅的.也就是说只有客户端运行后.服务器才会存储该客户机的信息.所以假设已经给了一个用户的ID是固定的,但他没在线.这个时候消息是发送不到这个人的.
blazeds的消息服务有个重点,客户机之间是互相不知道对方状态的,也就是不知道对方是否在线.
当要保证某个用户必须收到消息时,这个要逻辑做处理,最简单的如: 可以先保存到数据库,当现客户机收到信息并处理了就发送请求把刚保存的数据删掉. 或客户机不在线,下回登录时,主动去读取数据库里的信息.保证用户在线或不在线的情况下,消息均可被目标用户收到.
那么假设用户在线的情况下.以下实例可以实现针对某个 id 的客户机发送消息.而无视其它客户机.(这个实例的前提是已经会使用 blazeds 的消息服务.基础的就不在这讲了.)
APP:
--------------------------------------
[Bindable]
private var selectorIDs:String
//订阅消息,以及当前客户机的id赋值并显示出来.
protected function creationCompleteHandler(event:FlexEvent):void{
consumer.subscribe();
selectorIDs = "ids= "+Math.floor(Math.random()*1000); //这里使用了一个变量字符串来做选择器. "ids=用户ID" 这个用户ID我在这里模拟的时候使用了随机数,实际应用的时候使用用户在数据库的ID.
currentUserID.text = selectorIDs;
}
//发送消息,注意红字,这个是过滤条件
protected function sendMsg():void{
var msg:AsyncMessage = new AsyncMessage()
msg.headers = new Array();
msg.headers["ids"] = targetID.text;
msg.body = new Object();
msg.body.xx="目标客户的消息,时间戳:"+new Date().toUTCString();
producer.send(msg)
}
protected function consumer_messageHandler(event:MessageEvent):void{
showInfos.text = event.message.body.xx.toString()
}
//注意Consumer 的 selector .这个是消息接收的条件, 也就是把当前客户机的 selectorIDs 作为选择器.
<fx:Declarations>
<s:Producer id="producer" destination="MessagingDestination" channelSet="{new ChannelSet(['my-polling-amf'])}"/>
<s:Consumer selector="{selectorIDs}" id="consumer" message="consumer_messageHandler(event)" destination="MessagingDestination" channelSet="{new ChannelSet(['my-polling-amf'])}" />
</fx:Declarations>
<s:TextInput editable="false" id="currentUserID" width="300" height="30" x="310" y="210" /><!--当前客户自己的ID-->
<s:TextInput id="targetID" width="300" height="30" x="310" y="250" /><!--目标的ID,只要 selectorIDs 后面的数字就可以 -->
<s:TextArea width="300" height="300" id="showInfos" />
<s:Button click="{sendMsg()}" label="发送消息给目标,请先在 targetID 字段填写目标ID" x="310" width="200" height="200"/>
--------------------------------------
另附 blazeds 的运算与条件符号:
* ...
/ ...
+ ...
- ...
is ...
not ...
like ...
in ...
between ...
= + ...
= - ...
= ( ...
= <STRING_LITERAL> ...
= <INTEGER_LITERAL> ...
= <FLOATING_POINT_LITERAL> ...
= <BOOLEAN_LITERAL> ...
= <ID> ...
<> ...
> ...
>= ...
< ...
<= ...