[FMS] Learning Flash Media Server step by step (4)

Source: Internet
Author: User
Tags sendmsg

Today we are talking about a very simple multi-person chat function, and we also do not have server code. Today we will use a new thing ---- mongodobject

Let's take a look at a piece of code and change the code in the previous section:

 

Package net. smilecn. Chat {

Import flash. display. Sprite;
Import flash.net. netconnection;
Import flash.net. Export dobject;
Import flash. Events. netstatusevent;
Import flash. Events. syncevent;
Import flash. Events. mouseevent;
Import Fl. Controls. textarea;
Import Fl. Controls. Button;
Import Fl. Controls. textinput;


Public class chat extends sprite {

Private var NC: netconnection;
Private var rtmpurl: String = "rtmp: // localhost/chat ";
Private var button: button;
Private var textarea: textarea;
Private var textinput: textinput;
Private var chatmsg_so: sharedobject;
Private var Username: String = "user001 ";

Public Function chat (): void {
Textarea = new textarea ();
Textarea. setsize (200,300 );
Textarea. Move (20, 20 );
Addchild (textarea );

Textinput = new textinput ();
Textinput. width = 140;
Textinput. Move (20,330 );
Addchild (textinput );

Button = new button ();
Button. width = 50;
Button. Label = "send ";
Button. Move (1, 170,330 );
Addchild (button );
Button. addeventlistener (mouseevent. Click, sendmsg );


NC = new netconnection ();
NC. addeventlistener (netstatusevent. net_status, netstatushandler );
NC. Connect (rtmpurl );
}

Private function netstatushandler (Event: netstatusevent): void {
If (event.info. Code = "netconnection. Connect. Success "){
Chatmsg_so = export dobject. getremote ("chatmsg", NC. Uri, false );
Chatmsg_so.connect (NC );
Chatmsg_so.addeventlistener (syncevent. Sync, checkso );
}
}

Private function checkso (Event: syncevent): void {
For (var I: uint; I <event. changelist. length; I ++)
{
Switch (event. changelist [I]. Code)
{
Case "clear ":
Trace ("clear ");
Break;
Case "success ":
Trace (chatmsg_so.data.msg );
Break;

Case "change ":
Textarea. appendtext (chatmsg_so.data.msg + "");
Break;
}
}
}

Private function sendmsg (E: mouseevent): void {
Chatmsg_so.setproperty ("MSG", username + ":" + textinput. Text );
Textarea. appendtext (username + ":" + textinput. Text + "");
Textinput. Text = "";
}

}

}

To see the effect, we first deploy an EXE file, equivalent to a client, and then change username to a name, the first one is user001, And we will change it to user002, open the EXE, publish the Flash file, and try to publish it with text points to see if the chat effect has been achieved.

Next we will explain the Code:

After the connection is successful, there is a code like chatmsg_so = sharedobject. getremote ("chatmsg", NC. Uri, false );

Chatmsg_so is an example of sharedobject, which is a shared object. The official explanation is as follows:

The export dobject class is used to read and store a limited amount of data on a user's computer or server. Using shared objects, you can share real-time data between multiple client SWF files and objects permanently stored on a local computer or remote server. Local shared objects are similar to browser cookies, and remote shared objects are similar to real-time data transmission devices.

Mongodobject can be used to store data. Here we use it to store our chat data.

The getremote method is used to obtain a remote shared object, which is a shared object on the FMS. Its name is chatmsg and its address is NC. uri, the last parameter is whether to save as a file. False indicates not to save. When the server has such a shared object named chatmsg, this method will get this shared object. If this shared object is not available, a shared object named chatmsg will be created.

In our code, a shared object will be created after the first customer connects to it, and other users will get the shared object.

Chatmsg_so.addeventlistener (syncevent. Sync, checkso );

This code is used to listen on the status of the shared object.

Event. changelist [I]. Code is the State. Here we use three States ---- clear, success, change

Clear is used to clear data (this is triggered when the first person in our code goes in)
Success is successful (triggered when messages are sent in our code)
Change is a change (this is triggered when someone else sends a message in our code)

When we implement this chat function, everyone changes the shared object, and then the shared object changes, it notifies all clients, others will receive this message (note that they will not receive the change message ).

This chat is not very useful in practice. Here we only introduce how to use shared objects, and there are still many features of this chat. (I do not like to use shared objects)

We will explain how to write the server code later. The two sections do not write the server code. continue in the next section.

Reprinted: http://blog.csdn.net/arrowyoung/article/details/2421965

Related Article

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.