Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 100 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
100
Dung lượng
362,98 KB
Nội dung
LocalConnection class 401 Event handler summary for the LocalConnection class Constructor for the LocalConnection class Availability Flash Player 6. Usage new LocalConnection() : LocalConnection Parameters None. Returns A reference to a LocalConnection object. Description Constructor; creates a LocalConnection object. Example The following example shows how receiving and sending SWF files create LocalConnnection objects. The two SWF files can use the same name or different names for their respective LocalConnection objects. In this example they use different names. // Code in the receiving SWF file this.createTextField("result_txt", 1, 10, 10, 100, 22); result_txt.border = true; var receiving_lc:LocalConnection = new LocalConnection(); receiving_lc.methodToExecute = function(param1:Number, param2:Number) { result_txt.text = param1+param2; }; receiving_lc.connect("lc_name"); The following SWF file sends the request to the first SWF file. // Code in the sending SWF file var sending_lc:LocalConnection = new LocalConnection(); sending_lc.send("lc_name", "methodToExecute", 5, 7); Event handler Description LocalConnection.allowDomain Invoked whenever the current (receiving) LocalConnection object receives a request to invoke a method from a sending LocalConnection object. LocalConnection.allowInsecureDomain Invoked whenever the current (receiving) LocalConnection object, which is in a SWF file hosted at a domain using a secure protocol (HTTPS), receives a request to invoke a method from a sending LocalConnection object that is in a SWF file hosted at a non-secure protocol. LocalConnection.onStatus Invoked after a sending LocalConnection object tries to send a command to a receiving LocalConnection object. 402 Chapter 2: ActionScriptLanguage Reference See also LocalConnection.connect(), LocalConnection.send() LocalConnection.allowDomain 403 LocalConnection.allowDomain Availability Flash Player 6; behavior changed in Flash Player 7. Usage receiving_lc.allowDomain = function([sendingDomain:String]) : Boolean { // Your statements here return true or false } Parameters sendingDomain A string that represents an optional parameter specifying the domain of the SWF file that contains the sending LocalConnection object. Returns Nothing. Description Event handler; invoked whenever receiving_lc receives a request to invoke a method from a sending LocalConnection object. Flash expects the code you implement in this handler to return a Boolean value of true or false . If the handler doesn’t return true , the request from the sending object is ignored, and the method is not invoked. When this event handler is absent, Flash Player applies a default security policy, which is equivalent to the following code: my_lc.allowDomain = function (sendingDomain) { return (sendingDomain == this.domain()); } Use LocalConnection.allowDomain to explicitly permit LocalConnection objects from specified domains, or from any domain, to execute methods of the receiving LocalConnection object. If you don’t declare the sendingDomain parameter, you probably want to accept commands from any domain, and the code in your handler would be simply return true . If you do declare sendingDomain , you probably want to compare the value of sendingDomain with domains from which you want to accept commands. The following examples show both implementations. In files authored for Flash Player 6, the sendingDomain parameter contains the superdomain of the caller. In files authored for Flash Player 7 or later, the sendingDomain parameter contains the exact domain of the caller. In the latter case, to allow access by SWF files hosted at either www.domain.com or store.domain.com, you must explicitly allow access from both domains. // For Flash Player 6 receiving_lc.allowDomain = function(sendingDomain) { return(sendingDomain=="domain.com"); } // For Flash Player 7 or later receiving_lc.allowDomain = function(sendingDomain) { return(sendingDomain=="www.domain.com" || 404 Chapter 2: ActionScriptLanguage Reference sendingDomain=="store.domain.com"); } Also, for files authored for Flash Player 7 or later, you can’t use this method to let SWF files hosted using a secure protocol (HTTPS) allow access from SWF files hosted in nonsecure protocols; you must use the LocalConnection.allowInsecureDomain event handler instead. Occasionally, you might encounter the following situation. Suppose you load a child SWF file from a different domain. You want to implement this method so that the child SWF file can make LocalConnection calls to the parent SWF file, but you don’t know the final domain from which the child SWF file will come. This can happen, for example, when you use load-balancing redirects or third-party servers. In this situation, you can use the MovieClip._url property in your implementation of this method. For example, if you load a SWF file into my_mc , you can then implement this method by checking whether the domain argument matches the domain of my_mc._url . (You must parse the domain out of the full URL contained in my_mc._url .) If you do this, make sure that you wait until the SWF file in my_mc is loaded, because the _url property will not have its final, correct value until the file is completely loaded. The best way to determine when a child SWF file finishes loading is to use MovieClipLoader.onLoadComplete. The opposite situation can also occur: You might create a child SWF file that wants to accept LocalConnection calls from its parent but doesn’t know the domain of its parent. In this situation, implement this method by checking whether the domain argument matches the domain of _parent._url. Again, you must parse the domain out of the full URL from _parent._url . In this situation, you don’t have to wait for the parent SWF file to load; the parent will already be loaded by the time the child loads. Example The following example shows how a LocalConnection object in a receiving SWF file can permit SWF files from any domain to invoke its methods. Compare this to the example in LocalConnection.connect() , in which only SWF files from the same domain can invoke the trace() method in the receiving SWF file. For a discussion of the use of the underscore (_) in the connection name, see LocalConnection.send() . this.createTextField("welcome_txt", this.getNextHighestDepth(), 10, 10, 100, 20); var my_lc:LocalConnection = new LocalConnection(); my_lc.allowDomain = function(sendingDomain:String) { domain_txt.text = sendingDomain; return true; }; my_lc.allowInsecureDomain = function(sendingDomain:String) { return (sendingDomain == this.domain()); } my_lc.sayHello = function(name:String) { welcome_txt.text = "Hello, "+name; }; my_lc.connect("_mylc"); LocalConnection.allowDomain 405 The following example sends a string to the previous SWF file and displays a status message about whether the local connection was able to connect to the file. A TextInput component called name_ti , a TextArea instance called status_ta and a Button instance called send_button are used to display content. var sending_lc:LocalConnection; var sendListener:Object = new Object(); sendListener.click = function(evt:Object) { sending_lc = new LocalConnection(); sending_lc.onStatus = function(infoObject:Object) { switch (infoObject.level) { case 'status' : status_ta.text = "LocalConnection connected successfully."; break; case 'error' : status_ta.text = "LocalConnection encountered an error."; break; } }; sending_lc.send("_mylc", "sayHello", name_ti.text); }; send_button.addEventListener("click", sendListener); In the following example, the receiving SWF file, which resides in thisDomain.com, accepts commands only from SWF files located in thisDomain.com or thatDomain.com: var aLocalConn:LocalConnection = new LocalConnection(); aLocalConn.Trace = function(aString) { aTextField += aString+newline; }; aLocalConn.allowDomain = function(sendingDomain) { return (sendingDomain == this.domain() || sendingDomain == "www.macromedia.com"); }; aLocalConn.connect("_mylc"); When published for Flash Player 7 or later, exact domain matching is used. This means that the example will fail if the SWF files are located at www.thatDomain.com but will work if the files are located at thatDomain.com. See also LocalConnection.connect(), LocalConnection.domain(), LocalConnection.send(), MovieClip._url, MovieClipLoader.onLoadComplete, _parent 406 Chapter 2: ActionScriptLanguage Reference LocalConnection.allowInsecureDomain Availability Flash Player 7. Usage receiving_lc.allowInsecureDomain = function([sendingDomain:String]) : Boolean { // Your statements here return true or false } Parameters sendingDomain A string that represents an optional parameter specifying the domain of the SWF file that contains the sending LocalConnection object. Returns A Boolean value. Description Event handler; invoked whenever receiving_lc , which is in a SWF file hosted at a domain using a secure protocol (HTTPS), receives a request to invoke a method from a sending LocalConnection object that is in a SWF file hosted at a nonsecure protocol. Flash expects the code you implement in this handler to return a Boolean value of true or false . If the handler doesn’t return true , the request from the sending object is ignored, and the method is not invoked. By default, SWF files hosted using the HTTPS protocol can be accessed only by other SWF files hosted using the HTTPS protocol. This implementation maintains the integrity provided by the HTTPS protocol. Using this method to override the default behavior is not recommended, as it compromises HTTPS security. However, you might need to do so, for example, if you need to permit access to HTTPS files published for Flash Player 7 or later from HTTP files published for Flash Player 6. A SWF file published for Flash Player 6 can use the LocalConnection.allowDomain event handler to permit HTTP to HTTPS access. However, because security is implemented differently in Flash Player 7, you must use the LocalConnection.allowInsecureDomain() method to permit such access in SWF files published for Flash Player 7 or later. Example The following example allows connections from the current domain or from www.macromedia.com, or allows insecure connections only from the current domain. this.createTextField("welcome_txt", this.getNextHighestDepth(), 10, 10, 100, 20); var my_lc:LocalConnection = new LocalConnection(); my_lc.allowDomain = function(sendingDomain:String) { domain_txt.text = sendingDomain; return (sendingDomain == this.domain() || sendingDomain == "www.macromedia.com"); LocalConnection.allowInsecureDomain 407 }; my_lc.allowInsecureDomain = function(sendingDomain:String) { return (sendingDomain == this.domain()); } my_lc.sayHello = function(name:String) { welcome_txt.text = "Hello, "+name; }; my_lc.connect("lc_name"); See also LocalConnection.allowDomain, LocalConnection.connect() 408 Chapter 2: ActionScriptLanguage Reference LocalConnection.close() Availability Flash Player 6. Usage receiving_lc.close() : Void Parameters None. Returns Nothing. Description Method; closes (disconnects) a LocalConnection object. Issue this command when you no longer want the object to accept commands—for example, when you want to issue a LocalConnection.connect() command using the same connectionName parameter in another SWF file. Example The following example closes a connection called receiving_lc when you click a Button component instance called close_button : this.createTextField("welcome_txt", this.getNextHighestDepth(), 10, 10, 100, 22); this.createTextField("status_txt", this.getNextHighestDepth(), 10, 42, 100,44); var receiving_lc:LocalConnection = new LocalConnection(); receiving_lc.sayHello = function(name:String) { welcome_txt.text = "Hello, "+name; }; receiving_lc.connect("lc_name"); var closeListener:Object = new Object(); closeListener.click = function(evt:Object) { receiving_lc.close(); status_txt.text = "connection closed"; }; close_button.addEventListener("click", closeListener); See also LocalConnection.connect() LocalConnection.connect() 409 LocalConnection.connect() Availability Flash Player 6. Usage receiving_lc.connect(connectionName:String) : Boolean Parameters connectionName A string that corresponds to the connection name specified in the LocalConnection.send() command that wants to communicate with receiving_lc . Returns A Boolean value: true if no other process running on the same client computer has already issued this command using the same value for the connectionName parameter; false otherwise. Description Method; prepares a LocalConnection object to receive commands from a LocalConnection.send() command (called the sending LocalConnection object). The object used with this command is called the receiving LocalConnection object. The receiving and sending objects must be running on the same client computer. Make sure you define the methods attached to receiving_lc before calling this method, as shown in all the examples in this section. By default, Flash Player resolves connectionName into a value of "superdomain:connectionName" , where superdomain is the superdomain of the SWF file containing the LocalConnection.connect() command. For example, if the SWF file containing the receiving LocalConnection object is located at www.someDomain.com, connectionName resolves to "someDomain.com:connectionName" . (If a SWF file is located on the client computer, the value assigned to superdomain is "localhost" .) Also by default, Flash Player lets the receiving LocalConnection object accept commands only from sending LocalConnection objects whose connection name also resolves into a value of "superdomain:connectionName" . In this way, Flash makes it simple for SWF files located in the same domain to communicate with each other. If you are implementing communication only between SWF files in the same domain, specify a string for connectionName that does not begin with an underscore (_) and that does not specify a domain name (for example, "myDomain:connectionName" ). Use the same string in the LocalConnection.connect(connectionName) command. 410 Chapter 2: ActionScriptLanguage Reference If you are implementing communication between SWF files in different domains, specifying a string for connectionName that begins with an underscore (_) will make the SWF with the receiving LocalConnection object more portable between domains. Here are the two possible cases: • If the string for connectionName does not begin with an underscore (_), Flash Player adds a prefix with the superdomain and a colon (for example, "myDomain:connectionName" ). Although this ensures that your connection does not conflict with connections of the same name from other domains, any sending LocalConnection objects must specify this superdomain (for example, "myDomain:connectionName" ). If the SWF with the receiving LocalConnection object is moved to another domain, the player changes the prefix to reflect the new superdomain (for example, "anotherDomain:connectionName" ). All sending LocalConnection objects would have to be manually edited to point to the new superdomain. • If the string for connectionName begins with an underscore (for example, "_connectionName" ), Flash Player does not add a prefix to the string. This means that the receiving and sending LocalConnection objects will use identical strings for connectionName . If the receiving object uses LocalConnection.allowDomain to specify that connections from any domain will be accepted, the SWF with the receiving LocalConnection object can be moved to another domain without altering any sending LocalConnection objects. For more information, see the discussion of connectionName in LocalConnection.send() and also the LocalConnection.allowDomain and LocalConnection.domain() entries. Note: Colons are used as special characters to separate the superdomain from the connectionName string. A string for connectionName that contains a colon is not valid. Example The following example shows how a SWF file in a particular domain can invoke a method named Trace in a receiving SWF file in the same domain. The receiving SWF file functions as a trace window for the sending SWF file; it contains two methods that other SWF files can call— Trace and Clear . Buttons pressed in the sending SWF files call these methods with specified parameters. // Receiving SWF var aLocalConnection:LocalConnection = new LocalConnection(); aLocalConnection.Trace = function(aString):String{ aTextField += aString + newline; } aLocalConnection.Clear = function(){ aTextField = ""; } aLocalConnection.connect("trace"); stop(); SWF 1 contains the following code, which creates a new Sound object that plays back an MP3 file at runtime. A ProgressBar called playback_pb displays the playback progress of the MP3 file. A Label component instance called song_lbl displays the name of the MP3 file. Buttons in different SWF files will be used to control the playback using a LocalConnection object. var playback_pb:mx.controls.ProgressBar; var my_sound:Sound; [...]... LocalConnection.allowDomain and LocalConnection.domain() See also LocalConnection.allowDomain, LocalConnection.connect(), LocalConnection.domain(), LocalConnection.onStatus 418 Chapter 2: ActionScriptLanguage Reference CHAPTER 2 ActionScriptLanguage Reference Math class Availability Flash Player 5 In Flash Player 4, the methods and properties of the Math class are emulated using approximations and might not be... channelDomain = domainArray.join("."); } } lc.connect("result"); lc.send("mydomain.com:sum", "aSum", channelDomain + ':' + "result", "aResult", 123, 456); See also LocalConnection.allowDomain 414 Chapter 2: ActionScriptLanguage Reference LocalConnection.onStatus Availability Flash Player 6 Usage sending_lc.onStatus = function(infoObject:Object) : Void{ // your statements here } Parameters A parameter defined according... error."; break; } }; sending_lc.send("lc_name", "sayHello", name_ti.text); }; send_button.addEventListener("click", sendListener); See also LocalConnection.send(), System.onStatus 416 Chapter 2: ActionScriptLanguage Reference LocalConnection.send() Availability Flash Player 6 Usage sending_lc.send (connectionName:String, method:String [, p1, ,pN]) : Boolean Parameters connectionName A string that corresponds... www.macromedia.com will be accepted, but those from one posted at // a different subdomain, e.g livedocs.macromedia.com, will not var my_lc:LocalConnection = new LocalConnection(); 412 Chapter 2: ActionScriptLanguage Reference my_lc.allowDomain = function(sendingDomain):String{ return (sendingDomain==this.domain() || sendingDomain=="www.macromedia.com"); } In the following example, a sending SWF file... its diameter (approximately 3.14159) Math.SQRT1_2 The reciprocal of the square root of 1/2 (approximately 0.707) Math.SQRT2 420 Description The square root of 2 (approximately 1.414) Chapter 2: ActionScriptLanguage Reference Math.abs() Availability Flash Player 5 In Flash Player 4, the methods and properties of the Math class are emulated using approximations and might not be as accurate as the non-emulated... 3.14159265358979 trace(Math.acos(0)); // output: 1.5707963267949 trace(Math.acos(1)); // output: 0 See also Math.asin(), Math.atan(), Math.atan2(), Math.cos(), Math.sin(), Math.tan() 422 Chapter 2: ActionScriptLanguage Reference Math.asin() Availability Flash Player 5 In Flash Player 4, the methods and properties of the Math class are emulated using approximations and might not be as accurate as the non-emulated... -0.785398163397448 trace(Math.atan(0)); // output: 0 trace(Math.atan(1)); // output: 0.785398163397448 See also Math.acos(), Math.asin(), Math.atan2(), Math.cos(), Math.sin(), Math.tan() 424 Chapter 2: ActionScriptLanguage Reference Math.atan2() Availability Flash Player 5 In Flash Player 4, the methods and properties of the Math class are emulated using approximations and might not be as accurate as the... a number is the closest integer that is greater than or equal to the number Example The following code returns a value of 13: Math.ceil(12.5); See also Math.floor(), Math.round() 426 Chapter 2: ActionScriptLanguage Reference Math.cos() Availability Flash Player 5 In Flash Player 4, the methods and properties of the Math class are emulated using approximations and might not be as accurate as the non-emulated... + continuouslyCompoundedInterest); /* Output: Beginning principal: $100 Simple interest after one year: $100 Continuously compounded interest after one year: $171.828182845905 */ 428 Chapter 2: ActionScriptLanguage Reference Math.exp() Availability Flash Player 5 In Flash Player 4, the methods and properties of the Math class are emulated using approximations and might not be as accurate as the non-emulated... than or equal to the specified number or expression Example The following code returns a value of 12: Math.floor(12.5); The following code returns a value of -7: Math.floor(-6.5); 430 Chapter 2: ActionScriptLanguage Reference Math.log() Availability Flash Player 5 In Flash Player 4, the methods and properties of the Math class are emulated using approximations and might not be as accurate as the non-emulated . Returns the smaller of the two integers. CHAPTER 2 ActionScript Language Reference 420 Chapter 2: ActionScript Language Reference Property summary for the Math. { return(sendingDomain=="www.domain.com" || 404 Chapter 2: ActionScript Language Reference sendingDomain=="store.domain.com"); } Also,