LocalConnection Object

 <  Day Day Up  >  

LocalConnection Object

In this chapter, we have covered a variety of ways to get data into a Flash movie from servers and local storage. In this section, we talk about how to get data from one Flash movie into another.

To communicate between two movies, both should be running on the same system. Each movie will be running in a separate instance of the Flash Player and can be running in different applications. For example, one movie can be running in Netscape while the other is running in Internet Explorer or a standalone Player.


Movie-to-movie communication on different systems (across a network) can be achieved using the XML object or Flash Communication Server (Comm Server). For more information on XML, see Chapter 12, and see Chapter 14, "Using Client-Side Data Integration," for more on the Flash Communications Server.

To implement movie-to-movie communication, the LocalConnection object is used. For communication to take place, there will be at least two instances of the LocalConnection object, one in the sender (the movie to send the data) and one in the receiver (the movie receiving the data). Table 11.4 shows the first line of code in each movie.

Table 11.4. Create a LocalConnection



sender = new LocalConnection();

receiver = new LocalConnection();

For one movie to communicate with the other, a connection name must be established and agreed upon. Table 11.5 shows how the movies will find each other. The receiver will listen for data from a given sender using the connect() method of the LocalConnection object. The sender will send the data using the send() method of the LocalConnection object. Notice that the receiver is listening for a connection using the named connection myData and the sender is sending the data using the same named connection. Many different movies can tie into the same named connection.

Table 11.5. Connect to a LocalConnection Object



sender = new LocalConnection();

receiver = new LocalConnection(); receiver.connect(" myData ")

sender.send(" myData ","methodName", data);


Security on the Receiving Movie

By default, data can be sent between movies loaded from the same domain. To exchange data between movies of different domains, we can use the event, allowDomain() , to specifically allow a domain. allowDomain () is triggered when data is received from a movie though a LocalConnection object.

Listing 11.11 shows the allowDomain() event used to allow data from www.myDomain.com to be read into a movie loaded from www.anotherDomain.com .

Listing 11.11. AllowDomain() Example
 domain: www.myDomain.com sendObject = new LocalConnection(); var rateObject:LoadVars = new LoadVars(); rateObject.load("bankRates.txt"); rateObject.onLoad = function(){    sendObject.send("LoanData", "populateBankList", rateObject); } domain: www.anotherDomain.com receiveObject = new LocalConnection(); receiveObject.allowDomain = function(domain){       if(domain == "www.myDomain.com"){             return true; }else{ return false; } } receiveObject.populateBankList = function(bankData, rateData){     //do work here } receiveObject.connect("LoanData") //Open for communication 

If the movie is hosted at a domain using a secure protocol (Hypertext Transfer Protocol Secure [HTTPS]), it can be accessed only by other SWF files hosted using the same protocol (HTTPS). This implementation maintains the integrity provided by the HTTPS protocol.

Using the allowInsecureDomain() method, you can override the default behavior. This is not recommended, however, because it compromises HTTPS security by allowing any domain without exclusion. A better choice would be to enumerate the allowed domains instead of allowing all domains.

When you are done with a connection object, you can close the connection using LocalConnection.close() . Because an application can maintain only one local connection at a time, it is not necessary to name the connection when closing.

Security on the Sending Movie

We saw that allowDomain() protects a receiving movie when receiving data from another movie. We might also want to receive status as to whether the sent message was received. The onStatus() event can be used to determine if a message was received. onStatus() is triggered whenever LocalConnection.onStatus() is called. When the event is triggered, an object is available with the status of the operation. The object has a property called level . It can have one of two possible values: "status" if the connection was found and "error" if the connection was not found. Listing 11.12 shows an example of the onStatus() event.

Listing 11.12. Using Local Connection.onStatus()
 var sendObject:LocalConnection = new LocalConnection(); var rateObject:LoadVars = new LoadVars(); rateObject.load("bankRates.txt"); rateObject.onLoad = function(){  triggers onStatus()    sendObject.send("LoanData", "populateBankList", rateObject); } sendObject.onStatus = function(infoObject:object){ if (infoObject.level == "error"){       //try again }else (infoObject.level == "error"){       //connection found } } 
 <  Day Day Up  >  

Object-Oriented Programming with ActionScript 2.0
Object-Oriented Programming with ActionScript 2.0
ISBN: 0735713804
EAN: 2147483647
Year: 2004
Pages: 162

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