Instant Messaging (RPC) rtmp Implementation--code implementation Chapter

Source: Internet
Author: User

The general steps to implement are:

Step 1: Define Netconnection objects to connect rtmp and listen for netstatusevent.net_status events

Step 2: Judge event.info.code== "NetConnection.Connect.Success" in the Netstatusevent.net_status event, through Sharedobject.getromote () Get Sharedobject Object

Step 3: Listen for the Syncevent.sync event of the Sharedobject object and Sharedobj.connect (netconnection)

Step 4: This time you can use Sharedobj.send () to send a message to the server, and then the server broadcasts the login message to all the clients that are currently connected

Step 5: You can get the data from Sharedobj.data in the Syncevent.sync event.

Step 6: Define a function that gets the return value after the server is called, which needs to be declared public

Ps:

Server side and client, both are in Operation Sharedobject object, client is read, server side is write (only server performs write to ensure data is correct)

Server-side Send message to each client, divided into client request and server-side request implementation

* Client requests are typically called sharedobj.send ("Getserverdata", "message") to implement

* Server-side is called Netconnection.call ("background method", NULL, "message"), then the background method is defined via Souser.sendmessage ("Getserverdata", New object[]{" Message "}) to send messages

Server-side code (. NET):

Using system;using system.collections.generic;using system.text;using fluorinefx.messaging.adapter;using Fluorinefx.messaging.api;using Fluorinefx.messaging.api.so;namespace servicelibrary5{ PublicclassMychatapp:applicationadapter { Publicoverride bool AppConnect (iconnection connection, object[] parameters) {string name = Parameters[0].            ToString (); Isharedobject Souser = This. Getsharedobject (connection. Scope,"Onlineuserlist"); if (Souser = = null) { This. Createsharedobject (connection. Scope,"Onlineuserlist",false); Souser = This. Getsharedobject (connection. Scope,"Onlineuserlist"); } souser.setattribute (name, name);//This is to provide parameters that are passed when you need to access the connection without the parameters parameterConnection. Client.setattribute ("Currentname", name);return true; } Publicoverride void Appdisconnect (Iconnection connection) {string name = connection. Client.getattribute ("Currentname").            ToString (); Isharedobject Souser = This. Getsharedobject (connection. Scope,"Onlineuserlist");            if (souser! = null) {Souser.removeattribute (name); } souser.sendmessage ("Getserverdata",NewObject[] {name +"Disconnected"}); Base.        Appdisconnect (connection); }//Using a common method of sending messages on the server side, the client needs to call the method via Netconnection.call ()           Publicvoid SendMessage (iconnection connection, String msg) {string userName = connection. Client.getattribute ("Currentname") asString Isharedobject Souser = Getsharedobject (connection. Scope,"Onlineuserlist"); Souser.sendmessage ("Getserverdata",NewObject[] {msg}); }    }}

Client code (FLEX):

<?xml version="1.0"encoding="Utf-8"? ><mx:application xmlns:mx="Http://www.adobe.com/2006/mxml"layout="Absolute"Backgroundcolor="0xFFFFFF"><mx:list x="404"y="122"Backgroundcolor="#3A6EA5"Width=" the"height="289"Id="Dlonlineusers"></mx:list><mx:label x="420"y=" the"text="Current Online user"Fontsize="very"/><mx:label x=" the"y="122"text="Your nickname:"Fontsize="very"/><mx:textinput id="Txtname"x="134"y="123"/><mx:button x="302"y="121"Label="Login"click="Onlogin ()"Fontsize=" A"/><mx:label x=" the"y="225"text="System Information"Fontsize="very"/><mx:textarea id="Txtmessage"x=" the"y="279"Width="287"Backgroundcolor="#3A6EA5"Color="0xFFFFFF"/><mx:button x=" the"y="420"Label="Logout"Fontsize=" A"click="OnDisconnect ()"/><mx:script><! [cdata[PrivatevarNc:netconnection;PrivatevarSouser:sharedobject;PrivatefunctionOnlogin ():void{NC =NewNetconnection (); Nc.addeventlistener (netstatusevent.net_status,onnetstatus); Nc.connect ("Rtmp://localhost:8323/mychatroom", This. txtname.text); nc.client = this;}PrivatefunctionOnnetstatus (event:netstatusevent):void{if(Event.info.code = ="NetConnection.Connect.Success") {Souser = Sharedobject.getremote ("Onlineuserlist", Nc.uri,false);if(Souser! = null) {Souser.addeventlistener (Syncevent.sync,onsync); Souser.connect (NC); souser.client = this;} Souser.send ("Getserverdata"," user" "+ This. txtName.Text +" Connected");}}PrivatefunctionOnDisconnect ():void{Nc.close ();}PrivatefunctionOnsync (event:syncevent):void{varArruser:array =NewArray (); for(varStr:stringinchSouser.data) {Arruser.push (souser.data[str]);} This. Dlonlineusers.dataprovider = Arruser;} PublicfunctionGetserverdata (Message:object):void{ This. Txtmessage.text + = message.tostring () +"\ n";}]] ></mx:Script></mx:Application>

Instant Messaging (RPC) rtmp Implementation--code implementation Chapter

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.