Flex and. Net interoperability (11): fluorinefx. Net's Remote Procedure Call (2)

Source: Internet
Author: User

. NET Server can also call the client conveniently and call the client method (for example, implement system broadcast ).

I. Client RPC (The client calls the server.)

To implement the method for client access to the server, you must first be familiar with netconnection in ActionScript. This class provides an example method called () for RPC access. The method is defined as follows:

Public Function call (command: String, responder: responder, arguments ): Void

For example, when developing a timely application, all clients need to synchronize the system time of the server, at this time, we can provide the client with a method to return the current system time. As follows:

// Returns the current system time.
Public String Getservertime ()
{
Return Datetime. Now. tostring ();
}

With this method, the client can remotely call the call () method of netconnection and process the call result through responder. If the called server-side method has no return value, then, you can choose not to use responder during the call. Complete flex instance as followsCode:

<? XML version = " 1.0 " Encoding = " UTF-8 " ?>
< MX: Application xmlns: MX = " Http://www.adobe.com/2006/mxml " Layout = " Absolute " Fontsize = " 12 " >
< MX: script >
<! [CDATA [
Import MX. Controls. Alert;
Import MX. rpc. Events. resultevent;

PrivateVaR NC: netconnection;
PrivateVaR responder: Responder= NewResponder (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 status: " + Event . Info. Code;
If ( Event . Info. Code = " Netconnection. Connect. Success " )
{
Trace ( " Server connected " );
}
}

/* call the server method */
private function ongetservertime ( event : mouseevent): void
{< br> NC. call ( " getservertime " , responder);
}

/*Processing results returned when the server method is successfully called*/
PrivateFunction onresult (Result: string ):Void
{
This. Txtresult. Text= "Getservertime" +Result;
}

/* Processing results returned when a server method is called */
Private Function onerror ( Event : Event ): Void
{
Trace ( " An error occurred while calling the server method. " );
}
] >
</ MX: script >
< MX: button x = " 30 " Y = " 43 " Label = " Connect to the server " Click = " Connectionserver (Event) " />
< MX: Label x = " 30 " Y = " 75 " Width = " 296 " ID = " Connstatus " />
< MX: button x = " 30 " Y = " 112 " Label = " Call the server Method " Click = " Ongetservertime (Event) " />
< MX: Label x = " 30 " Y = " 153 " ID = " Txtresult " Width = " 296 " />
</ MX: Application >

Fluorinefx. Net RPC is as simple as this. The client calls the server method directly through the call () method of the current connection and uses responder to process the call result.

Ii. server-side RPC (the server-side calls the client)

Client and server calls can be bidirectional, and the server can also call client methods. Both the client and the server can call each other without the current connection object. For example, if the above client calls the server method through the call () method of the current connection, the server also calls the client method through the current connection object.

In fluorinefx, the interface used for connection is iconnection. As long as the client successfully connects to the server, all the attributes of the connection objects connected to the server are saved on the server. The iservicecapableconnection interface is used to call the client method. The iservicecapableconnection interface is defined as follows:

If you want the server to call the client method, you must use this interface to complete the process. You must forcibly convert the iconnection interface currently connected

Iservicecapableconnection interface, and then calls its invoke () method to call the client method on the server side.

/// <Summary>
/// Call the client Method
/// </Summary>
/// <Param name = "connection"> </param>
Public Void Invokeclient ()
{
Iservicecapableconnection Conn = Fluorinecontext. Current. Connection As Iservicecapableconnection;
If (Connection ! = Null )
{
Object [] ARGs = New Object [] {Getservertime ()};
Conn. Invoke ( " Recservermessage " , ArgS );
}
}

On the server side, you can use fluorinecontext. current. connection obtains the connection object of the current client, converts it to an iservicecapableconnection interface object, and calls its invoke () method to call the client method.

Note that the client method provided to the server must be a public method. Otherwise, the server reports an exception "Not Found Method: method name. The following code block:

/* *
* Method of receiving server calls
* */
Public Function recservermessage (message: Object ): Void
{
Alert. Show (message. tostring ());
}

Fluorinefx. NET provides the above two features are very practical, especially in the development of timely applications, client RPC can easily achieve the client is the data request to the server for processing, for example, in the development of online games, different data on the game client is changing at any time, so we can use the client RPC to achieve this requirement. Server-side RPC can also be applied to this scenario. The data in the game changes through the client RPC request to the server, after the server performs phase-based business computing and processing, it returns the data required by the client to the client in the current request through the server-side RPC.

Some may want to ask, if I want to develop a chat room, whether this method can be used to implement system broadcast messages. The answer is yes. to broadcast messages, you can obtain all the clients connected to the server from the server, and then call the client method cyclically to broadcast messages to the system. I will not introduce how to implement system broadcast here, because fluorinefx. NET provides us with a better way to implement system broadcast. If you are interested, follow up on me.ArticleIn the next article, I will detail the knowledge points in this area.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.