Flex vs. NET Interop (11): Timely communication applications based on Fluorinefx.net (Remote Procedure Call) (ii)
Fluorinefx.net provides a complete RPC (Remote Procedure call) feature that can be very easy to use (. swf), whether it is developed via Flash or Flex The server-side method of. NET is invoked in RPC, and the. NET Server side can also be very convenient for calling clients, calling the client's methods (such as implementing system broadcasts).
First, client RPC (client Invoke server side)
To implement a client Access server-side approach, you first have to be familiar with the netconnection in ActionScript, which provides an example method call () for RPC access, which is defined as follows:
public function call(command:String, responder:Responder, ...arguments):void
For example, when we develop a timely application, all clients need to synchronize the system time of the server, this time we can provide a way to return the current system time on the server to call the client. As follows:
//返回当前系统时间
public string GetServerTime()
{
return DateTime.Now.ToString();
}
With this method, the client can make a remote call through the Netconnection call () method and responder the result of the invocation, and if the server-side method invoked does not return a value, the responder can be used without the call. The complete Flex instance code is as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<mx:application xmlns:mx= "Http://www.adobe.com/2006/mxml" layout= "absolute" fontsize= ">"
<mx:Script>
<! [cdata[
Import Mx.controls.Alert;
Import mx.rpc.events.ResultEvent;
private Var nc:netconnection;
private var responder:responder = new Responder (onresult,onerror);
Private Function Connectionserver (event:mouseevent): void
{
NC = new Netconnection ();
Nc.connect ("Rtmp://localhost:1617/rpcdemo", "abc", "123");
Nc.addeventlistener (Netstatusevent.net_status,onstatushandler);
Nc.client = this;
}
Private Function Onstatushandler (event:netstatusevent): void
{
This.connStatus.text = "Connection state:" + event.info.code;
if (Event.info.code = = "NetConnection.Connect.Success")
{
Trace ("Connect server succeeded");
}
}
/* Call Server-side method * *
Private Function Ongetservertime (event:mouseevent): void
{
Nc.call ("GetServerTime", responder);
}
/* The results returned when the call to the server-side method succeeds/*
Private Function Onresult (result:string): void
{
This.txtResult.text = "Method GetServerTime return results" + result;
}
/* The result that is returned when processing a call to a server-side method * *
Private Function OnError (event:event): void
{
Trace ("Invoke Server-side method error");
}
]]>
</mx:Script>
<mx:button x= "y=" label= "Connection Server" click= "Connectionserver (event)"/>
<mx:label x= "y=" width= "296" id= "Connstatus"/>
<mx:button x= "y=" 112 "label=" Invoke service-side method "click=" Ongetservertime (event)/>
<mx:label x= "y=" 153 "id=" Txtresult "width=" 296
</mx:Application>
Fluorinefx.net RPC is so simple, the client invokes the server-side method, which is invoked directly through the call () method of the current connection, using responder to process the result of the call.