Recipe 17.3 Validating Receipt of Communication Over a Local Connection

17.3.1 Problem

You want a sending movie to receive confirmation that the communication was successfully received.

17.3.2 Solution

Configure the receiving movie to return a receipt to the sending movie.

17.3.3 Discussion

If you need to confirm that a communication was received, you can have the receiving movie send a message back to the original sending movie. Here are the steps for confirming receipt of a communication:

  1. Set up the sending and receiving movies, as described in Recipe 17.1.

  2. In addition to whatever code you include in the method that is invoked on the receiving movie, write code to send a receipt over a new channel (e.g., "_myConnectionReceipt"). You can use this.send( ) within the local connection's method to send a receipt back to the original sender.

  3. In the sending movie, call the connect( ) method, passing it the name of the channel over which the receiving movie sends the receipt (again, "_myConnectionReceipt").

  4. In the sending movie, create a method on the local connection object to handle the receipt communication. Make sure this method name matches the name of the method that the receiving movie invokes when sending the receipt.

The following is an example of some code from a sending movie and a receiving movie.

First, the receiving movie code:

// Create the receiving code to listen on the "_myConnection" channel. lc = new LocalConnection(  ); lc.connect("_myConnection"); lc.myMethod = function (  ) {   // In addition to whatever other code goes in the    // receiving method, add this code to issue a receipt    // back to the sending movie over the "myConnectionReceipt" channel.    // The this keyword refers to the current local connection object.   this.send("_myConnectionReceipt", "onReceipt"); };

Then, the sending movie code:

// Create the local connection object for sending over the "_myConnection" channel. sending_lc = new LocalConnection(  ); sending_lc.send("_myConnection", "myMethod"); // Tell the local connection to listen on the "_myConnectionReceipt" channel for the // receipt broadcast by the receiving movie. sending_lc.connect("_myConnectionReceipt"); // Define the onReceipt(  ) method that gets called from the receiving movie. sending_lc.onReceipt = function (  ) {   _root.output_txt.text = "received"; };

The key point is that the name of the channel on which a local connection object listens (using connect( )) must be the same as the name of the channel over which another movie's local connection object sends a message (using send( )). Also, notice that the two movies do not communicate back and forth over the same channel. This is because a movie ignores any broadcasts over any channel to which it is also listening. So we establish two one-way channels because a single channel is not two-way in practice.

17.3.4 See Also

Recipe 17.1



ActionScript Cookbook
ActionScript 3.0 Cookbook: Solutions for Flash Platform and Flex Application Developers
ISBN: 0596526954
EAN: 2147483647
Year: 2005
Pages: 425

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net