I have a dream... Increase my knowledge

Create Remote Shared Object on Flash Media Server 2

application.onAppStart = function(){
trace("start");
// Create the shared object to store client objects (users)
this.users_so = SharedObject.get("users_so", false);

Subscribe Remote Shared Object on client

//Returns a reference to an object that can be stored on a server
users_so = SharedObject.getRemote("users_so", nc.uri, false);
users_so.addEventListener(SyncEvent.SYNC, usersSync);
users_so.addEventListener(NetStatusEvent.NET_STATUS, onSOnetStatus); //Old 'onStatus()'
users_so.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSOSecurityError);
//SharedObject has a property 'client' that defines obj on which will invoked remote methods
users_so.client = this;
users_so.connect(nc);

Send a function (on client)

public function sendMsg( color:String, msg:String ):void{
var outmsg:String = "Hello World!!";
//Broadcasts a message to all clients connected to the specified remote shared object
chat_so.send("onMsgSend", outmsg);
}

Execute a function (on client) earlier sended

public function onMsgSend( msg:String ):void{
trace("onMsgSend: " + msg);
chatHistory += msg;
dispatchEvent(new Event("histChange"));
}

 

Adding new Attributes and Elements

var novel:XML = <BOOK ISBN="0141182806>
<TITLE>Ulysses</TITLE>
<AUTHOR>Joyce, James</AUTHOR>
<PUBLISHER>Hi Books</PUBLISHER>
<DESCRIPTION>A <B>very</B> thick book.</DESCRIPTION>
</BOOK>;

Modify existing element or attribute

novel.TITLE = "Last Ulysses"
novel.@ISBN = "015687982928";

Result:

<BOOK ISBN="015687982928>
<TITLE>Last Ulysses</TITLE>
<AUTHOR>Joyce, James</AUTHOR>
<PUBLISHER>Hi Books</PUBLISHER>
<DESCRIPTION>A <B>very</B> thick book.</DESCRIPTION>
</BOOK>

Adding new elements

novel.SETTING.CITY = "Rome"
novel.SETTING.COUNTRY = "Italy";

Result:

<BOOK ISBN="015687982928>
<TITLE>Last Ulysses</TITLE>
<AUTHOR>Joyce, James</AUTHOR>
<PUBLISHER>Hi Books</PUBLISHER>
<DESCRIPTION>A <B>very</B> thick book.</DESCRIPTION>
<SETTING>
<CITY>Rome</CITY>
<COUNTRY>Italy</COUNTRY>
</SETTING>
</BOOK>

Adding new attributes

novel.SETTING.@CITY = "Rome"
novel.SETTING.@COUNTRY = "Italy";

Result:

<BOOK ISBN="015687982928>
<TITLE>Last Ulysses</TITLE>
<AUTHOR>Joyce, James</AUTHOR>
<PUBLISHER>Hi Books</PUBLISHER>
<DESCRIPTION>A <B>very</B> thick book.</DESCRIPTION>
<SETTING CITY="Rome" COUNTRY="Italy" />
</BOOK>

Adding new child after a specific existing child

novel.insertChildAfter(novel.AUTHOR[0], <AUTHOR>Dave Luxtor</AUTHOR>);

Adding new child before a specific existing child

novel.insertChildBefore(novel.AUTHOR[0], <PRICE>30.00</PRICE>);

Adding new child before all existing child

novel.prependChild(<PAGECOUNT>340</PAGECOUNT>);

Deleting Elements and Attributes

delete novel.@ISBN;
//Removes the ISBN attribute from the <BOOK> element.

delete novel.TITLE;
//Removes the <TITLE> element from the <BOOK> element.

delete novel.*;
//Removes the <TITLE>, <AUTHOR>, <PUBLISHER>, etc. elements from the <BOOK> element.

delete novel.TITLE.*;
//Removes the text content from the <TITLE> element.

delete novel.@*;
//Removes all attributes.

 

Accessing descendants

var xml:XML = <LOAN>
<BOOK ISBN="976568989" DATE="1136091600000">
<TITLE>Ulysses</TITLE>
<AUTHOR>Joyce, James</AUTHOR>
<PUBLISHER>My Books</PUBLISHER>
</BOOK>

<DVD ISBN="5432334456" DATE="1136691000000">
<TITLE>2001 A Space Odysses</TITLE>
<DIRECTOR>Stanley Kubrick</DIRECTOR>
<PUBLISHER>Warner</PUBLISHER>
</DVD>

<DVD ISBN="7878785566" DATE="1136691000000">
<TITLE>Toy Story</TITLE>
<DIRECTOR>John Lasseter</DIRECTOR>
<PUBLISHER>Walt Disney</PUBLISHER>
</DVD>
</LOAN>

theElement..identifier

A descendant operator(..) returns a XMLList representing all descendants of theElement whose name match identifier.

i.e. xml..DIRECTOR
//Notice that the <DIRECTOR> elements are not direct children of the <LOAN> element; they are grandchildren.
Above expression yields an XMLList that has two XML instances, representing the two <DIRECTOR> elements.


Example
for each(var title:XML in load..TITLE)
trace(title);
//Output
Ulysses
2001 A Space Odysses
Toy Story

theElement..@attributeName

A descendant operator also works with attributes(..@) returns a XMLList representing all descendants attributes of theElement whose name match attributeName.

i.e. xml..@DUE
Above expression yields an XMLList that has three XML instances, representing the three DUE attributes.

 

From the previous example

var pub:XML = novel.PUBLISHER[0];
pub.parent();
//Reference to <PUBLISHER>'s parent (which is <BOOK>)

If the pub is a root node (not in this case) the parent() method returns null. (i.e. novel.parent();)


Accessing Attributes
novel.attributes() and novel.@*
//Returns an XMLList representing <BOOK>'s attributes (which is ISBN node)

Normally attributes are accessed by name, using either the attribute() method
someElement.attribute("attributeName")
or E4X's more convenient variable-access syntax
someElement.@attributeName

 

Example:

var novel:XML = <BOOK ISBN="0141182806>
<TITLE>Ulysses</TITLE>
<AUTHOR>Joyce, James</AUTHOR>
<PUBLISHER>Hi Books</PUBLISHER>
<DESCRIPTION>A <B>very</B> thick book.</DESCRIPTION>
</BOOK>;

novel.children() and novel.*
//Returns an XMLList representing 's child nodes

novel.children()[1]
//Reference to 's second child node

novel.child("AUTHOR") and novel.AUTHOR
//Returns all child elements of named "AUTHOR"

novel.AUTHOR[0].setName("WRITER")
//Change name of the first child element named "AUTHOR"

novel.TITLE.children()[0] and novel.TITLE.*[0]
//A reference to the text node Ulysses and not the string "Ulysses"

novel.TITLE.*[0].parent() //A reference to the <TITLE> element

novel.TITLE.*[0].nodeKind() //Returns the string "text"

novel.TITLE.*[0].toString() //Returns the string "Ulysses"

The <DESCRIPTION> element contains both element and text child nodes:

  • A (text node);
  • <B>very</B> (element node with a child text node);
  • thick book. (text node);

The text() method can also used to retrieve the text nodes from an entire XMLList, not just a single XML element.
trace(novel.DESCRIPTION.text()[0]); //Display: A

trace(novel.DESCRIPTION.text()[1]); //Display: thick book

 

Hello world!!

By Enrico

Hi! This's my first post!