BlazeDSの主要な機能の一つであるMessegingのクライアントサイドの処理について。
-
srcを右クリックして、「新規 > MXML アプリケーション」をクリック。「ファイル名」をmessaging.mxmlと入力して「終了」。
-
クライアントの処理を書く。
- ActionScript
- Source
- <?xml version="1.0" encoding="utf-8"?>
- <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" initialize="init()" viewSourceURL="srcview/index.html">
- <mx:Script>
- <![CDATA[
- import mx.messaging.events.MessageFaultEvent;
- import mx.messaging.messages.AsyncMessage;
- import mx.messaging.messages.IMessage;
- import mx.messaging.ChannelSet;
- import mx.messaging.channels.AMFChannel;
- import mx.messaging.channels.StreamingAMFChannel;
- import mx.messaging.events.MessageEvent;
- import mx.messaging.events.ChannelEvent;
- import mx.messaging.Consumer;
- import mx.messaging.Producer;
- private var producer:Producer = new Producer();
- private var consumer:Consumer = new Consumer();
- private function init():void {
- initConnectEvent();
- connectToServer();
- }
- private function initConnectEvent():void {
- consumer.addEventListener(ChannelEvent.CONNECT, hdlConsumerConnect);
- consumer.addEventListener(ChannelEvent.DISCONNECT, hdlConsumerDisconnect);
- consumer.addEventListener(MessageEvent.MESSAGE, hdlConsumerMessage);
- }
- private function hdlConsumerConnect(ce:ChannelEvent):void {
- log("Connect to Server.");
- setSend(true);
- }
- private function hdlConsumerDisconnect(ce:ChannelEvent):void {
- log("Disconnect from Server.");
- setSend(false);
- }
- private function hdlConsumerMessage(me:MessageEvent):void {
- log(me.message.body.chat);
- }
- private function setSend(bln:Boolean):void {
- if (bln) {
- btnSend.addEventListener(MouseEvent.CLICK, hdlSendClick);
- } else {
- btnSend.removeEventListener(MouseEvent.CLICK, hdlSendClick);
- }
- btnSend.enabled = bln;
- tiChat.enabled = bln;
- }
- private function hdlSendClick(e:*):void {
- var msg:IMessage = new AsyncMessage();
- msg.body = {chat:tiChat.text};
- producer.send(msg);
- tiChat.text = "";
- }
- private function connectToServer():void {
- setChannel("localhost");
- setDistination();
- consumer.subscribe();
- }
- private function setChannel(hostname:String):void {
- var channelSet:ChannelSet = new ChannelSet();
- var myStreamingAMF:AMFChannel = new StreamingAMFChannel("my-streaming-amf", "http://" + hostname + ":8400/tutorial/messagebroker/streamingamf");
- var myPollingAMF:AMFChannel = new AMFChannel("my-polling-amf", "http://" + hostname + ":8400/tutorial/messagebroker/amfpolling");
- myPollingAMF.pollingEnabled = true;
- myPollingAMF.pollingInterval = 1;
- channelSet.addChannel(myStreamingAMF);
- channelSet.addChannel(myPollingAMF);
- producer.channelSet = channelSet;
- consumer.channelSet = channelSet;
- }
- private function setDistination():void {
- producer.destination = "messagingTutorial";
- consumer.destination = "messagingTutorial";
- }
- private function log(str:String):void {
- taLog.text = str + "\n" + taLog.text;
- }
- ]]>
- </mx:Script>
- <mx:ApplicationControlBar width="100%" height="100%">
- <mx:TextArea width="100%" height="100%" id="taLog" editable="false"/>
- </mx:ApplicationControlBar>
- <mx:ApplicationControlBar width="100%">
- <mx:TextInput id="tiChat" width="100%" enabled="false" enter="hdlSendClick(event)"/>
- <mx:Button label="Send" id="btnSend" enabled="false"/>
- </mx:ApplicationControlBar>
- </mx:Application>
-
「ファイル > エクスポート」をクリックして、「Flex Builder > リリースビルド」を選択して「次へ」。
「書き出し先フォルダ」をtutorialにして「終了」。
-
ブラウザを二窓開いてhttp://localhost:8400/tutorial/messaging.htmlへアクセス。
一方の窓でテキストを入力し、Sendを押すと、両方のクライアントにストリーミングでテキストが配信される。




