mina与flash利用xml传输

本文介绍如何使用Flash与MINA框架实现Socket通信,并详细展示了配置MINA服务器以支持NUL终止符,以及如何处理跨域请求的具体代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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的内容

[quote]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")[/quote]
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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值