Flash P2P source code

Source: Internet
Author: User
Original address: http://www.fmser.com /? P = 93 flash P2P source code

 

 

 

After this page is opened on machines A and B with a static IP address, the program first generates a random username myname and automatically connects to the official Adobe stratus server. After the connection is successful, the program automatically obtains a myid. Copy and paste the myname and myid in a to oppname and oppid in B, and click the call button in B to call a. When a has a call prompt, click the accept button in, in this way, the P2P connection is established! After a P2P connection is established, the peer video can be played and texts can be chatted.

 

In this example, you can:

1. Learn how to use the new rtmfp protocol to connect to a remote server.

2. Learn how to establish P2P connections.

3. Learn how to publish and receive P2P streams.

4. Learn how to chat in text.

The source code is as follows:

Package {
Import flash. display. movieclip;
Import Fl. Managers. stylemanager;
Import flash. Text. textformat;
Import flash.net. netconnection;
Import flash.net. netstream;
Import flash.net. objectencoding;
Import flash.net. urlloader;
Import flash.net. URLRequest;
Import flash.net. urlrequestmethod;
Import flash.net. urlrequestheader;
Import flash.net. urlvariables;
Import flash. Events. event;
Import flash. Events. ioerrorevent;
Import flash. Events. mouseevent;
Import flash. Events. asyncerrorevent;
Import flash. Events. securityerrorevent;
Import flash. Events. netstatusevent;
Import flash. Media. soundtransform;
Import flash. Media. camera;
Import flash. Media. Microphone;
Public class P2P extends movieclip {
Private var fmspath: string;
Private var mync: netconnection;
Private var controlstream: netstream;
Private var outgoingstream: netstream;
Private var incomingstream: netstream;
Private var listenerstream: netstream;
Private var yourname: string;
Private var yourid: string;
Private var oppname: string;
Private var oppid: string;
Private var webserviceurl: string;
// Officially started
Public Function P2P (){
Setcommonstyle ();
Init ();
}
Private function setcommonstyle (){
// Set the style of all components
VaR mytf: textformat = new textformat;
Mytf. size = 12;
Stylemanager. setstyle ("textformat", mytf );
}
// Initialization
Private function Init (){
Fmspath = "rtmfp: // stratus.adobe.com/hawkprerelease-4e4efa13755c/fmser.cn.pdf ";
Webserviceurl = "http: // 76.74.170.61/cgi-bin/Reg";
Mync = new netconnection ();
Mync. Client = this;
Mync. objectencoding = objectencoding. amf3;
Mync. addeventlistener (netstatusevent. net_status, netstatus );
Mync. addeventlistener (securityerrorevent. security_error, securityerror );
Mync. addeventlistener (asyncerrorevent. async_error, asyncerror );
// Start connecting to the server
Mync. Connect (fmspath );
// Generate username randomly
Mynametxt. Text = "fmser" + int (math. Random () * 100 );
Sendbtn. addeventlistener (mouseevent. Click, sendchatmsg );
}

Private function asyncerror (E: asyncerrorevent ){
}
Private function securityerror (E: securityerrorevent ){
}
// Send your user name and ID to the Web server for temporary storage after the connection Function
Private function netstatus (E: netstatusevent ){
Switch (e.info. Code ){
Case "netconnection. Connect. Success ":
Trace ("connection successful !");
Myidtxt. Text = mync. nearid;
Callwebservice ();
Break;
Case "netconnection. Connect. Failed ":
Trace ("Connection Failed !");
Break;
Case "netconnection. Connect. Rejected ":
Trace ("Connection Failed !");
Break;
Case "netconnection. Connect. Closed ":
Trace ("Connection interrupted !");
Break;
}

}
// Complete user information submission
Private function callwebservice (){
VaR urlloader: urlloader = new urlloader ();
Urlloader. addeventlistener (event. Complete, completehandler );
Urlloader. addeventlistener (ioerrorevent. io_error, ioerrorhandler );
VaR URLRequest: URLRequest = new URLRequest (webserviceurl );
VaR parameter: urlvariables = new urlvariables;
Parameter. Username = mynametxt. text;
Parameter. Identity = myidtxt. text;
URLRequest. Data = parameter;
Urlloader. Load (URLRequest );
}
// Prepare and be called
Private function completehandler (E: Event ){
Callbtn. addeventlistener (mouseevent. Click, startcall );
Completeregistration ();
}
Private function ioerrorhandler (E: ioerrorevent ){
}
// Start the call
Private function startcall (E: mouseevent ){
Oppname = oppnametxt. text;
Oppid = oppidtxt. text;
Placecall (oppname, oppid );
}
// Call the main function
Private function placecall (tmpoppname: String, tmpoppid: string ){
Msgtxt. Text = "calling:" + tmpoppname + "...";
// Try to play the target video
Controlstream = new netstream (mync, tmpoppid );
Controlstream. addeventlistener (netstatusevent. net_status, controlhandler );
Controlstream. Play ("control" + tmpoppname );
// Publish point-to-point videos externally
Outgoingstream = new netstream (mync, netstream. direct_connections );
Outgoingstream. addeventlistener (netstatusevent. net_status, outgoingstreamhandler );
Outgoingstream. Publish ("media-Caller ");
VaR O: * = new object ();
O. onpeerconnect = function (tmpns: netstream ){
Msgtxt. Text = "Creating a P2P connection ..."
};
Outgoingstream. Client = O;
Startaudio ();
Startvideo ();
// Receives external point-to-point videos
Incomingstream = new netstream (mync, tmpoppid );
Incomingstream. addeventlistener (netstatusevent. net_status, incomingstreamhandler );
Incomingstream. Play ("media-callee ");
VaR ST: * = new soundtransform (50 );
Incomingstream. soundtransform = sT;
// This event is triggered when the caller accepts the connection
VaR I: * = new object ();
I. oncallaccepted = function (tmpoppname: string ){
Msgtxt. Text = tmpoppname + "has accepted your call ..."
};
// This event is triggered when the caller accepts the connection
I. onim = function (username: String, chatmsg: string ){
Chatcontent. Text = chatcontent. Text + (username + ":" + chatmsg + "/N ");
Chatcontent. verticalscrollposition = chatcontent. textheight;
}
Incomingstream. Client = I;
Remotevideodisplay. attachnetstream (incomingstream );
}
Private function controlhandler (E: netstatusevent ){
Trace (e.info. Code );
}
Private function outgoingstreamhandler (E: netstatusevent ){
Outgoingstream. Send ("onincomingcall", mynametxt. Text );
Trace (e.info. Code );
}
Private function startaudio (){
VaR mymic: microphone = microphone. getmicrophone (0 );
Outgoingstream. attachaudio (mymic );
}
Private function startvideo (){
VaR mycam: Camera = camera. getcamera ();
Localvideodisplay. attachcamera (mycam );
Outgoingstream. attachcamera (mycam );
}

Private function incomingstreamhandler (E: netstatusevent ){
Trace (e.info. Code );
}
// Prepare the connection after submitting the information to the Web server
Private function completeregistration (){
Listenerstream = new netstream (mync, netstream. direct_connections );
Listenerstream. addeventlistener (netstatusevent. net_status, listenerhandler );
Listenerstream. Publish ("control" + mynametxt. Text );
VaR C: * = new object ();
C. onpeerconnect = function (tmpns: netstream ){
VaR Caller: * = tmpns;
Incomingstream = new netstream (mync, caller. Farid );
Incomingstream. addeventlistener (netstatusevent. net_status, incomingstreamhandler );
Incomingstream. Play ("media-Caller ");
VaR ST: soundtransform = new soundtransform (50 );
Incomingstream. soundtransform = sT;
Incomingstream. receiveaudio (false );
Incomingstream. receivevideo (false );
VaR I: Object = new object ();
I. onincomingcall = function (tmpoppname: string ){
Msgtxt. Text = tmpoppname + "is calling you. Do you accept this ?"
Acceptbtn. addeventlistener (mouseevent. Click, acceptcall)
}
I. onim = function (username: String, chatmsg: string ){
Chatcontent. Text = chatcontent. Text + (username + ":" + chatmsg + "/N ");
Chatcontent. verticalscrollposition = chatcontent. textheight;
}
Incomingstream. Client = I;
};
Listenerstream. Client = C;
}
Private function listenerhandler (E: netstatusevent ){
Trace (e.info. Code );
}
// Receives the call
Public Function acceptcall (E: mouseevent ){
Msgtxt. Text = "you have accepted the call from the other party ...";
Incomingstream. receiveaudio (true );
Incomingstream. receivevideo (true );
Remotevideodisplay. attachnetstream (incomingstream );
Outgoingstream = new netstream (mync, netstream. direct_connections );
Outgoingstream. addeventlistener (netstatusevent. net_status, outgoingstreamhandler );
Outgoingstream. Publish ("media-callee ");
VaR O: * = new object ();
O. onpeerconnect = function (tmpns: netstream ){
Trace (tmpns. Farid );
}
;
Outgoingstream. Client = O;
Outgoingstream. Send ("oncallaccepted", mynametxt. Text );
Startvideo ();
Startaudio ();
}
// Send chat Information
Private function sendchatmsg (E: mouseevent ){
VaR tmpmsg: * = chattxt. text;
If (tmpmsg! = 0 & outgoingstream ){
Chatcontent. Text = chatcontent. Text + mynametxt. Text + ":" + tmpmsg + "/N ";
Outgoingstream. Send ("onim", mynametxt. Text, tmpmsg );
Chattxt. Text = "";
} Else {
Chatcontent. appendtext ("the sent content is blank or the connection has not been established !" + "/N ");
Chattxt. Text = "";
}
}

}
}

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.