ProblemYou want a sending movie to receive confirmation that the communication was successfully received. SolutionConfigure the receiving movie to return a receipt to the sending movie. DiscussionTo confirm that a communication was received, you can have the receiving movie send a message back to the original sending movie. Due to the nature of local connections, a single channel cannot be used for two-way communication. For this reason, we have to create a local connection channel for each direction of communication. Use the following steps to confirm receipt of a message:
The following is an example of some code from a sending movie and a receiving movie. First, the receiving movie code, using the DynamicLocalConnection class: // Create the receiving code to listen on the "_exampleChannel" // channel. var receiver:DynamicLocalConnection = new DynamicLocalConnection( ); receiver.connect( "_exampleChannel" ); receiver.example = function( ):void { // 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 "_exampleChannelReceipt" // channel. The this keyword refers to the current local // connection object. this.send( "_exampleChannelReceipt", "receipt" ); }; Then, the sending movie code: // Create the local connection object for sending over the // "_exampleChannel" channel. var sender:DynamicLocalConnection = new DynamicLocalConnection( ); seder.send( "_exampleChannel", "example" ); // Tell the local connection to listen on the // "_exampleChannelReceipt" channel for the receipt broadcast // by the receiving movie. sender.connect( "_exampleChannelReceipt" ); // Define the receipt( ) method that gets called from the // receiving movie. sender.receipt = function( ):void { output.text = "Receiver has delivered sent receipt"; }; The key 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( )). To take this one step further, you can tell the receiving local connection the name of the channel to send a receipt to. This is useful when multiple movies are sending data to the same movie and the receiving movie does not necessarily know which movie delivered the data. To achieve this effect, pass the channel name to the receiver by using code like this: sender.send( "_exampleChannel", "example", "_exampleChannelReceipt1" ); The sender movie would send the name of the channel it's listening for a receipt on. The receiving movie then sends the receipt over that channel name: receiver.example = function( receiptChannelName:String ):void { this.send( receiptChannelName, "receipt" ); } By passing around the receipt channel name, the receipt channel is never hardcoded into the receiving movie, making the approach more flexible. See AlsoRecipe 18.1 |