P2P functions in Flash player10.1 + fms4

Source: Internet
Author: User

Before fms4, Adobe only allowed to use the P2P feature in stratus. Fortunately, in the latest fms4 release, P2P functions have been integrated, which will bring higher efficiency to real-time video applications. Adobe is very powerful this time!

To use P2P, the flex SDK for development must be at least 4.1 (of course, the highest version is hero 4.5, which can be downloaded from Adobe's official website ), fms4 is also required (You can also download the development version from the Adobe website ).

Complete firstCodeRight:

Package {import FL. controls. button; import FL. controls. label; import FL. controls. textarea; import flash. display. sprite; import flash. events. mouseevent; import flash. events. netstatusevent; import flash.net. groupspecifier; import flash.net. netconnection; import flash.net. netgroup; import flash.net. netgroupreplicationstrategy; import flash. text. textformat; public class p2p_helloworld extends sprite {private VaR _ LBL: Label; private VaR _ btnaddtowant: button; private VaR _ btngendata: button; private VaR _ btnaddtohave: button; private VaR _ txtobj: textarea; private VaR _ txtoutput: textarea; private VaR _ DATA: vector. <string>; private VaR _ datalength: uint = 100; private VaR _ NC: netconnection; private VaR _ ng: netgroup; private VaR _ SPEC: groupspecifier; private VaR _ Server: string = "rtmfp: // localhost/helloserver"; private VaR _ groupname: String = "mygroup"; private VaR _ connected: Boolean = false; Public Function p2p_helloworld () {Init ();} private function Init (): void {This. _ btnaddtowant = btnaddtowant; this. _ btnaddtohave = btnaddtohave; this. _ btngendata = btngendata; this. _ txtobj = txtobj; this. _ txtoutput = txtoutput; this. _ LBL = LBL; var style: textformat = new textformat ("", 12, 0x000000, false, null, null, 5); this. _ btnaddtohave. setstyle ("textformat", style); this. _ btnaddtowant. setstyle ("textformat", style); this. _ btngendata. setstyle ("textformat", style); this. _ txtobj. setstyle ("textformat", style); this. _ txtoutput. setstyle ("textformat", style); this. _ LBL. setstyle ("textformat", style); this. _ btngendata. addeventlistener (mouseevent. click, _ btngendata_click); this. _ btnaddtohave. addeventlistener (mouseevent. click, _ btnaddtohave_click); this. _ btnaddtowant. addeventlistener (mouseevent. click, _ btnaddtowant_click); // First connect to the server _ NC = new netconnection (); _ NC. addeventlistener (netstatusevent. net_status, _ nc_net_status); _ NC. connect (_ server); output ("connecting" + _ SERVER + "... ");} private function _ nc_net_status (E: netstatusevent): void {output (e.info. code); Switch (e.info. code) {Case "netconnection. connect. success ": // after the connection is successful, set netgroupthis. _ spec = new groupspecifier (this. _ groupname); _ spec. serverchannelenabled = true; // set the channel _ spec that can be created on the server. objectreplicationenabled = true; // allows object replication _ ng = new netgroup (_ NC, _ spec. groupspecwithauthorizations (); _ ng. addeventlistener (netstatusevent. net_status, _ nc_net_status); break; Case "netgroup. connect. success ": _ connected = true; _ ng. replicationstrategy = netgroupreplicationstrategy. lowest_first; // when setting the data block for transmission, first pass the break with the smallest index number; Case "netgroup. replication. fetch. sendnotify ": // This processing output (" --> notification: data block "+ e.info. index + "to be received soon"); break; Case "netgroup. replication. fetch. failed ": //" when the receiver fails to receive data, this processing output ("--> error: data block" + e.info. index + "failed to receive"); break; Case "netgroup. replication. fetch. result ": //" receiver "triggers the output (" --> data block "+ e.info. index + "received successfully; Value:" + e.info. object); _ ng. addhaveobjects (e.info. index, e.info. index); // After receiving is complete, add the received data to the list of objects to be sent. The more people there are, the more stable the transmission, faster. If (_ DATA = NULL) {_ DATA = new vector. <string> (this. _ datalength);} _ DATA [e.info. index] = e.info. object. tostring (); // indicates that all received if (e.info. index = This. _ datalength-1) {for (var I: Int = 0; I <_ datalength; I ++) {_ DATA [I] = "This is data" + I. tostring (); this. _ txtobj. appendtext ("index:" + I. tostring () + ", data:" + _ DATA [I] + "|") ;}} break; Case "netgroup. replication. request ": // when there is a data transmission request," provider "will trigger this processing _ ng. writerequestedobject (e.info. requestid, _ DATA [e.info. index]); // here is the real response "receiver", send the specified data to the output ("--> data block" + e.info. index + "request sent. ID of this request:" + e.info. requestid); break; default: break;} // initialize the private function _ btngendata_click (E: mouseevent): void {This. _ txtobj. TEXT = ""; if (_ DATA = NULL) {_ DATA = new vector. <string> (this. _ datalength) ;}for (VAR var I: Int = 0; I <_ datalength; I ++) {_ DATA [I] = "This is data" + I. tostring (); this. _ txtobj. appendtext ("index:" + I. tostring () + ", data:" + _ DATA [I] + "|") ;}// the generated initial data, add the private function _ btnaddtohave_click (E: mouseevent): void {This. _ ng. addhaveobjects (0, _ datalength-1);} // Private function _ btnaddtowant_click (E: mouseevent): void {This. _ ng. addwantobjects (0, _ datalength-1);} // output result private function output (S: string): void {This. _ txtoutput. appendtext (S + "\ n ");}}}

In this Code, we see a brand newNetgroupObject. To use P2P, the "receiver" and "receiver" must first be added to the "netgroup with the same name. In addition, the data to be sent must be divided into ordered parts (these data blocks are usually saved in an ordered array), and the "sender" calls the addhaveobjects method to set the data block to be sent, the "receiver" calls the block to be received by the addwantobjects request.

Once the "receiver" calls the addwantobjects method, "sender" enters "netgroup. replication. request "status. In this case, the" sender "responds to the" receiver "request and sends the required data blockUDPAfter the protocol is sent, the "receiver" will receive "netgroup. replication. fetch. sendnotify data Arrival notification. If the data is successfully received, it enters "netgroup. replication. fetch. result Status. After receiving all the results, developers can merge these blocks into the original object as needed.

The process is as follows:

The final running of the Code in this article:

Test method: the sender clicks "generate initial data", then "add data to be sent", and then the receiver clicks "receive data"

In addition, if you open more "receivers", you can verify whether the "receiver" can become a data provider after receiving the data and provide data to other receivers, that is, P2P"The more people, the faster the speed, the more stable the transmission"Symptom

However, P2P in fms4 is not perfect. In actual tests, it is not possible to make a hole. That is, if all peer terminals are in the same network segment, the transmission is normal, however, if it is not the same network segment, P2P cannot be performed.

However, if more P2P machines are involved, the client that receives the data can also process the data according to the code in the text, this indicates that other users in the same CIDR block may have no data source, but as long as one user in This CIDR block receives data (for example, this user has multiple networks ), other users in this section can also receive data, which can solve the conflict between holes to a certain extent.

Sample source file download: http://files.cnblogs.com/yjmyzz/p2pTest.7z

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.