原始的例子是和后台结合写的,下面的是分开写的,其实也没有改什么,主要是熟悉下这种机制,以后被遗忘之神光临了,好有个线索找回来 ,呵呵。
[img]http://dl.iteye.com/upload/attachment/284336/3e2cf324-3414-3693-92ea-a064ae065473.jpg[/img]
------》前台
------》后台
[img]http://dl.iteye.com/upload/attachment/284336/3e2cf324-3414-3693-92ea-a064ae065473.jpg[/img]
------》前台
<?xml version="1.0" encoding="utf-8"?>
<mx:Panel xmlns:mx="http://www.adobe.com/2006/mxml" width="100%" height="100%"
creationComplete="initComp()">
<mx:Script>
<![CDATA[
import mx.messaging.channels.StreamingAMFChannel;
import mx.messaging.ChannelSet;
import mx.messaging.channels.AMFChannel;
import mx.messaging.events.MessageEvent;
import mx.messaging.messages.AsyncMessage;
import mx.messaging.messages.IMessage;
private function initComp():void
{
var myStreamingAMF:AMFChannel = new StreamingAMFChannel("my-streaming-amf", "http://localhost:8400/samples/messagebroker/streamingamf");
var myPollingAMF:AMFChannel = new AMFChannel("my-polling-amf", "http://localhost:8400/samples/messagebroker/amfpolling");
myPollingAMF.pollingEnabled = true;
myPollingAMF.pollingInterval = 2000;
var channelSet:ChannelSet = new ChannelSet();
channelSet.addChannel(myStreamingAMF);
channelSet.addChannel(myPollingAMF);
consumer.channelSet = channelSet;
producer.channelSet = channelSet;
}
public function set room(name:String):void
{
if (!name) return;
if (consumer && consumer.subscribed)
{
log.text += "Leaving room " + consumer.destination + "\n";
consumer.unsubscribe();
}
consumer.destination = name;
producer.destination = name;
consumer.subscribe();
log.text += "Entering room " + name + "\n";
}
private function send():void
{
var message:IMessage = new AsyncMessage();
message.body = msg.text;
producer.send(message);
msg.text = "";
}
private function messageHandler(event:MessageEvent):void
{
log.text += event.message.body + "\n";
}
]]>
</mx:Script>
<mx:Producer id="producer"/>
<mx:Consumer id="consumer" message="messageHandler(event)"/>
<mx:TextArea id="log" width="100%" height="100%"/>
<mx:ControlBar>
<mx:TextInput id="msg" width="100%" enter="send()" enabled="{consumer.subscribed}"/>
<mx:Button label="Send" click="send()" enabled="{consumer.subscribed}"/>
</mx:ControlBar>
</mx:Panel>
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns="*" layout="horizontal"
creationComplete="srv.getRoomList()">
<mx:RemoteObject id="srv" destination="chat-room-service" endpoint="http://localhost:8400/samples/messagebroker/amf"
fault="Alert.show(event.fault.rootCause.message, 'Error')"/>
<mx:Panel title="Room List" width="100%" height="100%" paddingTop="4" paddingLeft="4" paddingBottom="4" paddingRight="4">
<mx:Label text="Select a room and start chatting in the right panel:"/>
<mx:List id="list" dataProvider="{srv.getRoomList.lastResult}" width="100%" height="100%"/>
<mx:ControlBar>
<mx:VBox width="100%">
<mx:Button label="Refresh" click="srv.getRoomList()"/>
<mx:Spacer height="2"/>
<mx:HRule width="100%" height="1"/>
<mx:Label text="Create a room:"/>
<mx:HBox>
<mx:TextInput id="room"/>
<mx:Button label="Create" click="srv.createRoom(room.text)" enabled="{room.text.length>0}"/>
</mx:HBox>
</mx:VBox>
</mx:ControlBar>
</mx:Panel>
<ChatPanel title="Chat" room="{list.selectedItem}"/>
<mx:Script>
<![CDATA[
import mx.controls.Alert;
]]>
</mx:Script>
</mx:Application>
------》后台
package com.xj.manager;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.springframework.stereotype.Service;
import flex.messaging.MessageBroker;
import flex.messaging.MessageDestination;
//import flex.messaging.config.ServerSettings;
import flex.messaging.services.MessageService;
/**
* Simplistic implementation of a chat room management service. Clients can add rooms,
* and obtain a list of rooms. The interesting part of this example is the "on-the-fly"
* creation of a message destination. The same technique can be used to create DataService
* and Remoting destinations.
*/
@Service
public class ChatRoomService {
private List rooms;
public ChatRoomService()
{
rooms = Collections.synchronizedList(new ArrayList());
}
public List getRoomList()
{
return rooms;
}
public void createRoom(String id) {
if (roomExists(id))
{
throw new RuntimeException("Room already exists");
}
// Create a new Message destination dynamically
String serviceId = "message-service";
MessageBroker broker = MessageBroker.getMessageBroker(null);
MessageService service = (MessageService) broker.getService(serviceId);
MessageDestination destination = (MessageDestination) service.createDestination(id);
if (service.isStarted())
{
destination.start();
}
rooms.add(id);
}
public boolean roomExists(String id)
{
int size = rooms.size();
for (int i=0; i<size; i++)
{
if ( ((String)rooms.get(i)).equals(id) )
{
return true;
}
}
return false;
}
}