Recipe 18.3.
Validating Receipt of Communication over Local Connections
Problem
You want a sending movie to receive confirmation that the
communication was successfully received.
Solution
Configure the receiving movie to return a
receipt to the sending movie.
Discussion
To 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:
-
Set up the sending and receiving movies, as
described in Recipe
18.1.
-
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 (for example,
"_exampleChannelReceipt "). You can use
this.send( ) within the local connection's method to send a
receipt back to the original sender.
-
In the sending movie, call the connect( )
method, passing it the name of the channel over which the receiving
movie sends the receipt ("_exampleChannelReceipt").
-
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, 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 Also
Recipe
18.1
|