Summary of interaction between client side and server side in FMS3

Source: Internet
Author: User
Tags cdata

Series article Navigation
  1. FLEX,FMS3 related articles Index
  2. Flex and Fms3 Create online chat rooms (with Netconnection objects and Sharedobject objects)
  3. FMS3 and Flex create online video recording and playback
  4. FMS3 and Flex create online multiplayer video conferencing and video chats (with original code)
  5. Summary of interaction between client side and server side in FMS3
  6. Free Beauty video chat, multi-person video conferencing feature enhanced version (FMS3 and Flex development (with Source))
  7. Free network remote video conferencing system, free beauty multiplayer video chat (with source download) (Flex and FMS3 development)
  8. Open source Flex Air free passionate beauty video chat room, free network remote video conferencing system (FLEX,FMS3 co-created)

The server-side code in the FMS3 is written in the ASC file, and the client is broadcastmsg.swf
2 Interactive ways I summarize as follows:

1. Client Call Server
The server-side MAIN.ASC code is as follows:


CLIENT.PROTOTYPE.SERVERFUN1 = function (value)
{
Return "value=" +value
};

The client code is as follows:


<?xml version= "1.0" encoding= "Utf-8"?>
<mx:application xmlns:mx= "Http://www.adobe.com/2006/mxml" layout= "Absolute"
Fontsize= "creationcomplete=" init () ">

<mx:Script>
<! [cdata[
Import Mx.controls.Alert;

private Var netconnection:netconnection;
private Var Responder:responder;
private var appserver:string= "Rtmp://192.168.0.249/testcode1";

Private Function init (): void
{
Netconnection = new Netconnection ();
Netconnection.connect (AppServer);
Netconnection.client=this;
}

Private Function OnClick (evt:mouseevent): void
{
Responder = new Responder (okfun,errorfun);
Netconnection.call ("ServerFun1", responder, "va");
}

Private Function Okfun (re:string): void
{
Alert.show (re);
}

Private Function Errorfun (info:object): void
{
Alert.show ("Error:" + info.description);
Alert.show ("Error:" + Info.code);
}

]]>
</mx:Script>

<mx:button x= "y=" label= "Call server" id= "btn" click= "OnClick (event)"/>

</mx:Application>


Code Description:
The Responder class provides an object that is used in Netconnection.call () to handle return values from servers that are related to success or failure of a particular operation.
It has 2 parameters: The 1th is the method that successfully invokes the callback, and the 2nd argument is the method of the callback when the call fails

Call () method
Public function call (command:string, Responder:responder, ... arguments): void
1th parameter: Server-side Method name
2nd parameter: Optional object for handling the return value of the server
3rd parameter: The value passed to the server-side method

This example: After clicking the button, call the ASC file in the ServerFun1 method, and pass the parameter "va" to it, the ServerFun1 method after the method is processed a value, if successful on the callback Okfun, and display "Value=va" string, If the call fails (you may experience a network operation error specific to the current operation or an error related to the current connection state), the callback is errorfun and an error message is displayed

2. Server-side call to specified client
The server-side MAIN.ASC code is as follows:


var handlerobject = function () {};

HandlerObject.prototype.onResult = function (Result)
{
Trace (result);
};

HandlerObject.prototype.onStatus = function (info)
{
Trace ("Error:" + info.description);
Trace ("Error:" + Info.code);
};

Application.onconnect = function (client)
{
This.acceptconnection (client);
var msg = "Hello client, your IP is:" + client.ip;
Client.call ("Asyncservercall", New Handlerobject, MSG);
};

The client code is as follows:


<?xml version= "1.0" encoding= "Utf-8"?>
<mx:application xmlns:mx= "Http://www.adobe.com/2006/mxml" layout= "Absolute"
Fontsize= "creationcomplete=" init () ">

<mx:Script>
<! [cdata[
Import Mx.controls.Alert;

private Var netconnection:netconnection;
private var appserver:string= "Rtmp://192.168.0.249/testcode1";

Private Function init (): void
{
Netconnection = new Netconnection ();
Netconnection.connect (AppServer);
Netconnection.client=this;
}

Public Function Asyncservercall (msg:string): String
{
Alert.show (msg);
Return "I got your message Thanks server!";
}

]]>
</mx:Script>

</mx:Application>


Code Description:
Client.call () Executes a method asynchronously on the flash client and returns the value from the Flash client to the server.
Usage Clientobject.call (methodName, [Resultobj, [P1, ..., PN]])
1th parameter: Method Name of the client
2nd parameter: This parameter is required when the sender expects a return value from the client. If the argument is passed but no return value is expected, the value null is passed. The resulting object can be any object that you define, and for the sake of usefulness, the resulting object should have two methods-onresult and Onstatus, which are called when the result arrives. If the call to the remote method succeeds, the Resultobj.onresult event is triggered, otherwise the Resultobj.onstatus event is triggered.
3rd parameter: The value of a method passed to the client

This example: After starting, the connection succeeds, the server receives the request, and calls the Asyncservercall method (must be public) to change the client, the Asyncservercall method processes the return value, if the processing succeeds Onresult event is triggered, If processing fails, the Onstatus event is triggered.

3. Server-side call to all clients (broadcast)
The server-side MAIN.ASC code is as follows:


Application.onconnect = function (currentclient)
{
Application.acceptconnection (currentclient);
Application.broadcastmsg ("Showservermsg", application.clients.length);
}

The client code is as follows:


<?xml version= "1.0" encoding= "Utf-8"?>
<mx:application xmlns:mx= "Http://www.adobe.com/2006/mxml" layout= "Absolute"
Fontsize= "creationcomplete=" init () ">

<mx:Script>
<! [cdata[

Import Mx.controls.Alert;

private Var netconnection:netconnection;
private var appserver:string= "Rtmp://192.168.0.249/testcode1";

Private Function init (): void
{
Netconnection = new Netconnection ();
Netconnection.connect (AppServer);
Netconnection.client=this;
}

Public Function Showservermsg (n:number): void
{
var msg:string = "already has" +n.tostring () + "bit user connection";
Alert.show (msg);
}

]]>
</mx:Script>

</mx:Application>


Code Description:
Application.broadcastmsg (): Broadcast a message to all connected clients, broadcast to each client
This method is equivalent to looping through the application.clients array and invoking Client.call () on each individual client, but this method is more efficient (especially when the number of clients connected is large). The only difference is that when you call BROADCASTMSG () You cannot specify a response object, except that the two syntaxes are the same.
Equivalent to the following:
Traverse the list of clients and call them separately
for (Var i=0;i<application.clients.length;i++) {
Application.clients[i].call ("Showservermsg", application.clients.length);
}

This example: Once a client connects to the FMS, it broadcasts to each connected client and displays "N-bit user connected"

4. Server-side call server side
Netconnection.call
Usage:
Netconnection.call (MethodName, [Resultobj, P1, ..., PN])
Call a command or method on a Flash communication server or other application server. Usage is the same as the netconnection.call of the client. He calls a method on a remote server. I didn't put a code on it.

5. Code download
Http://files.cnblogs.com/aierong/TestCode1.rar

Summary of interaction between client side and server side in FMS3

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.