Concepts of method triggering and data broadcasting on server and client

Source: Internet
Author: User
Concepts of method triggering and data broadcasting on server and client

Data transmission and method call between the client and the server are usually required in the application of FCS. the following concepts should be applicable:

Avoid the call method being unresponsive or the information is not delivered to the desired location.

I. How is the server method called?

To enable the function called by the client on the server end, you must define the function on the client object on the server end.

Prototype Method to extend the method of the original client category, so that all instances generated by the client category have this

Method.

Example:

Client. Prototype. newfun = function (){}

You can also define this function under a single client instance,

Example:

Application. onconnect (newclient) = function (){

.....

Newclient. newfun = function (){}

}

Ii. How the client method is called

To enable the server to call a function on the client end, it must be defined on netconnection.

Example:

NC = new netconnection ();

.....

NC. myfun = function (){}

3. What are the differences between the send and call methods? How to Use

Call: This method can use netconnection objects on the client side. What can be used on the server side is

Netconnection and client object

1. Use netconnection. Call on the client to trigger the method of the client object on the server.

2. Use client object. Call on the server side to trigger the method on netconnection on the client side.

3. When netconnection. Call is used on the server, the role of the server is the same as that of the client.

Is the method that triggers the client object on another server.

Send: on the client side, you can use this method to include mongodobject and netstream.

The send method is very interesting. It allows you to invoke all functions on the client side, but because

When you trigger a function, you can include parameters. This feature allows you to broadcast a small amount of data.

The information that is broadcasted to all users is not necessarily placed in mongodobject, And the onsync is used for synchronization.

, Sometimes sending is also a very simple practice, how to define

1. Define the method "myfun" on netstream or mongodobject"

2. Use netstream. Send ("myfun") or mongodobject. Send ("myfun", myvar) to allow all clients

"Myfun" on will be triggered

Iv. Examples of possible interaction types

1. The client transmits data to the server or calls the server to execute the server function.

Example: In a simple chat room, the chat content only exists in the variable on the server, and the comment dobject is not used to store the call.

Call the message method on the server side to process the MSG passed by the client.

Client:

NC. Call ("message", null, MSG );

Server:

Application. onappstart = function (){

Application. chat_content = "";

}

Application. onconnect = function (newclient ){

.......

Newclient. Message = function (MSG ){

Application. chat_content + = MSG;

}

}

2. The client broadcasts data to all clients and executes the function of the specified client.

Example: for example, if you have previously replied to the website, you need to enter a URL url_txt for a clinet.

The client will open the web page of this URL.

Client:

First, define a method attached with a struct on so.

Lobby_so.openpage = function (receive_url ){

Geturl (receive_url );

}

By using send, all clients can receive the URL and enable publish.

Lobby_so.send ("openpage", url_txt );

3. the server sends data to a specific client

Application Example: when a user is connected to the server and the user data is verified correctly, the client

Execute the specified function to jump to a page or make a MC appear ..., At the same time, we need to bring the server information to the system.

Note: The server only responds to the client that is interacting with the server. In the preceding example, the server only triggers the request.

The connected client executes the specified function. Other clients do not respond.

Server:

Application. onconnect = function (newclient, PWD ){

If (Pwd = "OK "){

Application. acceptconnection (newclient );

Newclient. Call ("get_message", null, message );

} Else {

Application. rejectconnection (newclient, errobj );

}

}

Client:

........

NC. get_message = function (Message ){};

4. The server broadcasts data to all clients.

Example: When a disconnection occurs, the server broadcasts all clients so that all clients can synchronously update the client list.

Note: In the preceding example, only the handler application. ondisconnect on the server is used when the clinet has no warning disconnection.

It will be triggered, that is, you need to write some programs in the handler to broadcast notifications to all clients.

How to broadcast? Two methods are available:

1. Put the data in the remote javasdobject object. As long as the so object content changes, so. onsync on the client is automatically triggered.

Write the online personnel list in the remote technical dobject object. If someone is disconnected, remove the so file because

So content is changed, so the handler of so. onsync on all clients will be triggered to update the data.

Server

Application. ondisconnect = function (newclient ){

Userlist_so.setproperty (newclient. Name ,"");

}

Warning: when there is no warning disconnection on the server side, the client side can be retrieved from the handler in NC. onstatus by judging info. Code.

Obtain information

2. When the data does not exist in so, It only exists in a variable on the server, and application. Clients can be used

Broadcast all clients. The following will trigger client_fun on all clients and bring the sendvar variable on the server to the previous

Server:

Application. ondisconnect = function (newclient ){

For (VAR I = 0; I <application. Clients. length; I ++ ){

Application. Clients [I]. Call ("client_fun", null, sendvar );

}

}

Client:

NC. client_fun = function (myvar ){}

Add a broadcast Method

The server sends the data to the client with the same object of getremote.

Server:

Application. abc_so = export dobject. Get ("abc_so", false );

Application. abc_so.send ("msgfromsrvr", MSG );

Client:

Abc_so = export dobject. getremote ("abc_so", abc_nc.uri, false );

Abc_so.msgfromsrvr = function (MSG ){

Showmsg (MSG );

};

Reprinted: http://www.cnblogs.com/AndySong/archive/2009/05/13/1456176.html

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.