mina与flash利用xml传输

flash通过socket和服务器端互传信息
服务器端采用mina构架

小二,上代码
        IoAcceptor acceptor = new NioSocketAcceptor();
        acceptor.setHandler(new ProjectHandle());
        acceptor.getFilterChain().addLast(
                "codec",
                new ProtocolCodecFilter(
                        new TextLineCodecFactory(Charset.forName("UTF-8"),
                                LineDelimiter.NUL, LineDelimiter.NUL)));
        // acceptor.getFilterChain().addLast("ddd", new StreamWriteFilter());
        acceptor.getSessionConfig().setReadBufferSize(2048);
        acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);
        try {
            acceptor.bind(new InetSocketAddress(PORT));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


acceptor.getFilterChain().addLast(
                "codec",
                new ProtocolCodecFilter(
                        new TextLineCodecFactory(Charset.forName("UTF-8"),
                                LineDelimiter.NUL, LineDelimiter.NUL)));

这句是重点,是mina2.0的新特性,注意LineDelimiter的内容

引用
static LineDelimiter AUTO
          A special line delimiter which is used for auto-detection of EOL in TextLineDecoder.
static LineDelimiter CRLF
          The CRLF line delimiter constant ("\r\n")
static LineDelimiter DEFAULT
          the line delimiter constant of the current O/S.
static LineDelimiter MAC
          The line delimiter constant of Mac OS ("\r")
static LineDelimiter NUL
          The line delimiter constant for NUL-terminated text protocols such as Flash XML socket ("\0")
static LineDelimiter UNIX
          The line delimiter constant of UNIX ("\n")
static LineDelimiter WINDOWS
          The line delimiter constant of MS Windows/DOS ("\r\n")

flash传输信息以“\0”结尾,所以选择NUL

handle代码,接受跨域请求,回复跨域文件
    private final static String security_req = "<policy-file-request/>";
    private final String flexString = "<cross-domain-policy>\n"
            + "  <allow-access-from  domain=\"*\"  to-ports=\"*\"  />\n"
            + "</cross-domain-policy>";    

    public void messageReceived(IoSession session, Object msg) throws Exception {
        if (msg.toString().equals(security_req)) {
            session.write(flexString);
        }
        logger.info("recv:" + msg);
        process(msg.toString(), session);
    }


flex端demo,建立连接、模拟跨域请求、得到xml回复,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">
	<mx:Script>
		<![CDATA[
			import mx.controls.Alert;
			import flash.net.Socket;
			
			private var socket:XMLSocket = new XMLSocket();
			
			internal function init():void{
				socket.addEventListener(Event.CLOSE,closehandler);
				socket.addEventListener(Event.CONNECT,connectHandle);
				socket.addEventListener(IOErrorEvent.IO_ERROR,ioErrorHandle);
				socket.addEventListener(DataEvent.DATA,dataHandle);
				socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR,securityHandle);
			}
			
			internal function closehandler(evt:Event):void{
				Alert.show("连接关闭");
//				trace("连接关闭");
			}
			internal function connectHandle(evt:Event):void{
				Alert.show("连接建立");
//				trace("连接建立");
			}
			internal function ioErrorHandle(evt:IOErrorEvent):void{
//				trace("io异常");
			}
			internal function securityHandle(evt:SecurityErrorEvent):void{
//				trace("安全异常");
			}
			internal function dataHandle(evt:DataEvent):void{
				var response:XML = new XML(evt.data);
				text1.text = response.toXMLString();
			}			
			internal function dosocket():void{
				socket.connect( "127.0.0.1",10100);
				socket.send("<policy-file-request/>");
			}
			
		]]>
	</mx:Script>
	<mx:Panel width="400" height="300">
		<mx:Button label="connection" click="dosocket()" textAlign="center"/>
		<mx:Spacer/>
		<mx:Spacer/>	
		<mx:Text id="text1"  width="100%" height="50%"/>
	</mx:Panel>
</mx:Application>

你可能感兴趣的:(xml,socket,Flex,Flash,Mina)