[ Team LiB ] |
Recipe 17.1 Communicating with Other Movieson the Same Computer17.1.1 ProblemYou want to communicate from one Flash movie to one or more Flash movies playing on the same client computer. 17.1.2 SolutionUse a LocalConnection object to invoke a function in the receiving movie from the sending movie. Use LocalConnection.connect( ) to listen for messages in the receiving movie and define the function that will be invoked. Use LocalConnection.send( ) from the sending movie to invoke a function on the remote computer. Both the sending and receiving movies must specify the same named channel for communication. 17.1.3 DiscussionWhen two or more Flash movies are playing on the same client computer, they can communicate with each other via a local connection created with the LocalConnection class. As long as the movies are playing on the same computer, a connection can be made regardless of the domains from which the movies are being served.
To successfully communicate between multiple movies on the same computer you must do three things:
A receiving movie must listen for messages over a specific, named local connection. To establish this communication channel, you should create a local connection object in the receiving movie and tell it to listen to a named connection using the connect( ) method: // Create the local connection object in the receiving movie. receiving_lc = new LocalConnection( ); // Instruct the local connection object to listen for messages sent over the // "_myConnection" channel. receiving_lc.connect("_myConnection"); As shown in the preceding example, the best practice is to name your communication channel (not the local connection object) with an initial underscore (_). Naming your connections in this way also simplifies communicating across domains. All communications are targeted to specific, custom methods of the receiving local connection object. For example, if the sending movie sends a communication that looks for a method named myMethod( ), you should define myMethod( ) on the receiving movie's local connection object, as follows: receiving_lc.myMethod = function ( ) { _root.output_txt.text = "communication received!"; }; To set up the sending movie, you must first create a local connection object: // Create the local connection object in the sending movie. sending_lc = new LocalConnection( ); Then use the LocalConection.send( ) method to send a communication. The first parameter of the send( ) method is a string that specifies the name of a connection over which to send the communication, enabling you to create multiple discrete connections. The second parameter of the send( ) method is a string specifying the name of the method to call in the receiving movie. When a movie receives a message, it invokes the method of the same name on the receiving local connection object. This example invokes a remote method: // Send a communication across the "_myConnection" channel that invokes a method // named myMethod( ) in the receiving movie. sending_lc.send("_myConnection", "myMethod"); When a sending movie sends a communication over a connection, any movie listening to that connection receives the communication. Therefore, you can have more than one receiving movie for a single sending movie. Conversely, multiple movies can send messages to a single receiving movie over a single named connection. Note that both the sending and receiving movies have to be playing on the same computer at the same time. To communicate between movies that play on the same computer at different times, use a local shared object. See Recipe 16.1 and Recipe 16.5. 17.1.4 See AlsoTo send data over a local connection, see Recipe 17.2 and Recipe 17.5. |
[ Team LiB ] |