MINA服務端+flash(AS)客戶端+嵌入式socket服務端互聯互通
1、 應用環境:
Web伺服器:TOMCAT 7
服務端開源框架:spring2.5+hibernate3+struts2
SOCKET服務端:MINA
客戶端:flash+html
第三方服務端:嵌入式、socket
資料庫:mysql5.5
2、 Flash客戶端連線MINA服務端
Ø MINA服務端配置和spring配置檔案整合,隨tomcat啟動而啟動。
<!-- Apache mina 配置-->
<bean id="serverHandler" class="com.upos.protocol.mina.ServerHandler" />
<bean id="SecurityHandler"
<!-- executorFilter多執行緒處理 -->
<bean id="executorFilter" class="org.apache.mina.filter.executor.ExecutorFilter" />
<bean id="mdcInjectionFilter" class="org.apache.mina.filter.logging.MdcInjectionFilter">
<constructor-arg value="remoteAddress"
</bean>
<bean id="codecFilter" class="org.apache.mina.filter.codec.ProtocolCodecFilter">
<constructor-arg>
<!-- <bean class="org.apache.mina.filter.codec.textline.TextLineCodecFactory" />-->
<!-- 處理物件流時候用ObjectSerializationCodecFactory -->
<!-- <bean class="org.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory" /> -->
<bean class="com.upos.protocol.mina.ServerCodeFactory" />
</constructor-arg>
</bean>
<bean id="loggingFilter" class="org.apache.mina.filter.logging.LoggingFilter" />
<bean id="filterChainBuilder" class="org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder">
<property name="filters">
<map>
<entry key="executor" value-ref="executorFilter" />
<entry key="mdcInjectionFilter" value-ref="mdcInjectionFilter" />
<entry key="codecFilter" value-ref="codecFilter" />
<entry key="loggingFilter" value-ref="loggingFilter" />
</map>
</property>
</bean>
<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="java.net.SocketAddress">
<bean class="org.apache.mina.integration.beans.InetSocketAddressEditor" />
</entry>
</map>
</property>
</bean>
<bean id="ioAcceptor" class="org.apache.mina.transport.socket.nio.NioSocketAcceptor" init-method="bind" destroy-method="unbind">
<property name="defaultLocalAddress" value=":9001" />
<property name="handler" ref="serverHandler" />
<property name="filterChainBuilder" ref="filterChainBuilder" />
<property name="reuseAddress" value="true" />
</bean>
<bean id="ioAcceptor2" class="org.apache.mina.transport.socket.nio.NioSocketAcceptor" init-method="bind" destroy-method="unbind">
<property name="defaultLocalAddress" value=":9999" />
<property name="handler" ref="SecurityHandler" />
<property name="filterChainBuilder" ref="filterChainBuilder" />
<property name="reuseAddress" value="true" />
</bean>
ServerHandler 處理器用於服務端接收資料,應答資料。
SecurityHandler 處理器用於為flash客戶端提供安全策略檔案。
以上配置檔案中,將ioAcceptor2的監聽埠設定為9999埠。flash客戶端在和服務端進行socket連線時,會先去預設的843埠請求安全策略檔案。如果在843埠未獲得安全授權,將會向9999埠請求安全策略檔案。如果安全授權確認,flash客戶端將請求ioAcceptor建立socket連線。
Ø SecurityHandler 程式碼如下:
public class SecurityHandler extends IoHandlerAdapter {
@Override
public void sessionOpened(IoSession session) throws Exception {
// TODO Auto-generated method stub
String xml = "<cross-domain-policy>";
xml = xml + "<allow-access-from domain="*" to-ports="*" />";
xml = xml + "</cross-domain-policy>";
session.write(xml + "\0");
}
}
Ø Flash客戶端的socket連線實現類:
public class SocketBase extends Socket
{
private var myremote:MyRemoteObject;
//private var surityStr:String = "xmlsocket://"+host+":843"; ;
public function SocketBase()
{
this.objectEncoding = ObjectEncoding.AMF3;
this.addEventListener(Event.CONNECT,onConnect);
this.addEventListener(Event.CLOSE,onClose);
this.addEventListener(IOErrorEvent.IO_ERROR,onIOError);
this.addEventListener(SecurityErrorEvent.SECURITY_ERROR,onSecurityError);
this.addEventListener(ProgressEvent.SOCKET_DATA,onData);
}
override public function connect(host:String , port:int):void
{
Security.allowDomain("*");
Security.loadPolicyFile("xmlsocket://" + host + ":" + 9999);
trace('Security.loadPolicyFile('+"xmlsocket://" + host +":" + port+')');
super.connect(host,port);
}
override public function close():void
{
if(this.connected)
{
super.close();
}
}
private function onData(e:ProgressEvent):void
{
Singleton.getInstance().dispatchEvent(new SocketEvent(SocketEvent.RECEIEVED));
}
private function onConnect(e:Event):void
{
trace('連線socket伺服器成功!');
loadremoteip();
}
public function loadremoteip():void{
myremote = new MyRemoteObject('minaforflexService');
var op:Operation = myremote.getOp('getRemoteIp',onSuccess);
op.send();
}
private function onSuccess(event:ResultEvent):void{
login(event.result.toString());
}
private function onClose(e:Event):void
{
trace('斷開了和socket伺服器的連線!');
close();
}
private function onIOError(e:IOErrorEvent):void
{
trace('連線伺服器失敗,請稍候操作');
}
private function onSecurityError(e:SecurityErrorEvent):void
{
trace('發生沙箱安全錯誤!'+e.text);
}
public function login(value:String):void{
if(!this.connected)
{
return;
}
sendMessage("LOGIN:"+value);
}
public function broadcast(senddata:String):void
{
if(!this.connected)
{
return;
}
sendMessage("BROADCAST:"+senddata);
}
public function quit():void
{
if(!this.connected)
{
return;
}
sendMessage("QUIT");
}
public function sendMessage(senddata:String):void
{
if(!this.connected)
{
return;
}
var message:ByteArray=new ByteArray();
var sendmessage:String = senddata;
message.writeMultiByte(sendmessage+"\n",'GBK');
this.writeBytes(message,0,message.length);
this.flush();//傳送資料
}
}
在服務端連線成功後,會通過RemoteObject訪問spring提供的後臺服務,獲得客戶端IP地址,並通過login方法傳送至mina服務端。
Mina服務記錄該這次連線的session及IP地址。用於將來向flash客戶端廣播訊息。
Ø 服務端獲得客戶端IP地址的程式碼:
@RemotingInclude
public String getRemoteIp(){
String str = FlexContext.getHttpRequest().getRemoteHost();
int i = ai.getAndIncrement();
return str+"_"+i;
}