Mapreduce tutorial 7 understand shared objects

Source: Internet
Author: User

Understanding shared objects
Shared Objects can store any data types supported by flash. As far as the storage location is concerned, shared objects can be divided into local models of client computers and remote models stored on servers. You can use it to record user-related information (such as user name, gender, and other settings parameters), or use it on the server to store information shared by other online users in real time.

Shared object means that users can apply on different servers and different users.ProgramEntity. Flashcom server supports three types of shared objects: local, remote, and server-side. The following describes the three shared objects.

Local shared object)
It is used to access local data stored in users' computer hard disks and can be accessed by other applications at different times. The local shared object extension is. sol, the default storage path is c: \ Documents and Settings \ login name \ Application Data \ Macromedia \ Flash Player \ Server Domain Name \ film path \ photo file name .swf \. The local shared object can also be temporary, that is, only available when the application is running. We can also use a local shared object without connecting to the Flash COM server. For more information about local shared objects, refer to the export dobject. getlocal method in the manual.
The method for creating and accessing a local shared object is as follows:

1.
VaR so = sharedobject. getlocal ('mycooker') // create a local shared object
2. // store data
3. // Note: data attributes must be used to read or write data to a local shared object.
4.
So. Data. Username = 'liu21st ';
5.
So. Data. MSG = 'century of the year ';
6.
So. Data. Counter = 10;
7.
// By default, the above information is not immediately written to the user's disk. It will not be written to the disk until the animation is disabled or the user leaves your website. To write data immediately, you must use the flush method. As follows:
8.
So. Flush ();

You can use the onstatus event to check whether the data is successfully stored.
The Returned Code value is

Export dobject. Flush. Success (write successful)
Export dobject. Flush. Failed (write failed)
CodeExample:

1.
So. onstatus = function (Info ){
2. var
Status = info. Code;
3. If (
Status = 'your dobject. Flush. SUCCESS '){
4.
Trace ('write succeeded ');}
5. Else
Trace ('write failed ');

Remote shared object)
Using the Flash video's actionscript program, you can create materials on the server that can be accessed by different online users. These materials are called remote shared objects. Like a local shared object, a remote shared object can be accessed by a local computer. The difference is that data is stored on the server, so any user can connect to the remote shared object to access the same information. The default storage path is the export dobjects folder under the Application Object Path. The extension is. FSO.
Remote shared object is also the most common shared object type. When an online user (or server program) updates the content of a remote shared object, other users online to the same application entity will automatically receive the update event (onsync ), keep data synchronized with each other. Applications are built using this mechanism.
For example, you can open a remote shared object, such as a telephone number table (valid permanently on the server ). When the user side makes any changes to the shared object, the modified data is automatically synchronized to other users connected to the remote shared object. If, for some reason, the local data information is updated but not connected to the server, the changes will be synchronized to the remote shared object the next time you connect to the server.
For more information, see the export dobject. getremote method in the manual.

To create a remote shared object:

1.
VaR client_nc = new netconnection ();
2.
Client_nc.connect ('rtmp: // localhost/videochat/room1 ');
3.
So = export dobject. getremote ('records ', client_nc.url); // data is not written to the disk.
4.
So. Connect (client_nc );

The Data Reading and Writing Methods for remote shared objects are similar to those for local shared objects. They also use data attributes and flush methods.
Use the following statement to write data to the shared object directory of the server-side application folder.

1.
So = export dobject. getremote ('records ', client_nc.url, true );
2.
// The remotely shared file name of this statement will be records. FSO

When the content of the remote shared object changes or is online for the first time, it will send an onsync event to the client, so that all online users can obtain the latest shared object data in real time.
Sample Code:

1.
So. onsync = function (list ){
2. For (VAR
K In list ){
3.
Trace ('name = '+ list [K]. Name +', event = '+ list [K]. Code );
4 .}
5.
// Do whatever else you want to do here
6.
}

Proxied shared object)
The proxy shared object is a remote shared object that can be shared between the client and the server. The difference is that it is created by the server-side actionscript program, for example, there are two entities on the server: chat01 and chat02. In chat02, you can connect to the shared object defined in chat01. For more information, see sharedobject in the manual. get method.
Unlike mongodobject on the client, the value of shared variables must be set through mongodobject. setproperty to obtain the value of shared variables through mongodobject. getproperty.
Instance code:

1.
Application. appstart = function (){
2.
NC = new netconnection ();
3.
NC. Connect ('rtmp: // '+ master_server +'/'+ master_instance );
4.
Proxyso = export dobject. Get ('myproxy', true, NC );
5.
// Now, whenever the client asks for a persistent
6. // shared object called myproxy they will receive myproxy
7. // shared object from the master_server/master_instance
8.
};

1.
Myinfo = export dobject. Get ('foo ');
2. var
ADDR = myinfo. getproperty ('address ');
3.
Myinfo. setproperty ('city', 'san Francisco ');
4. var
Names = sharedinfo. getpropertynames ();
5. (
X in names ){
6. var
Propval = sharedinfo. getproperty (Names [x]);
7.
Trace ('value of properties' + Names [x] + '=' + propval );
8 .}

Before using the remote shared object, confirm your dobject. connect returns true. mongodobject is called on the client. the flush method only copies a copy locally. To ensure a copy on the server, you must use mongodobject on the server. flush method, such:

1.
// Sample server-side code for flushing a persistent shared object
2. // to the server

3. // get the shared object when the application is loaded.

4.
Application. onappstart = function ()
5 .{

6.
Application. myso = export dobject. Get ('export dobjname', true );
7 .}

8.
// When a user disconnects, flush the shared object.
9.
Application. ondisconnect = function (client)
10 .{
11.
Application. myso. Flush ();
12 .}

If multiple clients or servers synchronize remote shared objects at the same time, the problem may occur. To solve this conflict, follow these steps.
1. store information of different users in different locations
This is the simplest method. For example, you can store data in different locations for each user in the room, and only modify the data part during synchronization.
2. Allocate a data owner
A more complex method is to define a user as the owner of the data within a limited period of time. The owner can lock the shared object on the server and synchronize other data until the available information is returned. Below is the sample code :( http://www.my400800.cn
)
An application that records the highest score of the game is used to solve the synchronization conflict problem. Currently, the highest score saved by the system is 95. At the same time, two users broke this record and scored 105 and 110 respectively, if the highest score is not locked, the updatehighscore method will be executed for both scores. One of the scores may not be recorded. This problem is solved by locking the shared object.

1.
Application. onappstart = function ()
2 .{

3.
Application. scoreso = export dobject. Get ('high _ score_so ', true );
4.
Application. scoreso. onsync = function (listval)
{

Trace ('got an onsync on scoreso ');
}

}

Application. onconnect = function (newclient, name, passwd)
{

Newclient. updatehighscore = function (final_score)
{

Application. scoreso. Lock ();
If (
Application. scoreso. getproperty ('high _ score_so ') <final_score)
{

Application. scoreso. setproperty ('high _ score_so ', final_score );
}

Application. scoreso. Unlock ();
}
}

3. notification client
when the server receives the synchronization request from the client, notify dobject. the onsync event notifies the user that the change is rejected, and then provides a user interface to resolve the conflict. This technology is usually used when clients are not updated frequently.
4. Accept some and reject other
Applications solve synchronization conflicts based on the "first come first served" principle. It usually takes the customer to re-request to resolve the conflict.
5. Use the send method to raise the control level
broadcast dobject. send command to broadcast messages to all clients connected to the remote shared object, including the sender himself. (Edit: Non-bear)

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.