Basics of networking and communication

Introduction to networking and communication

When you build more complex ActionScript applications, you often need to communicate with server-side scripts, or load data from external XML or text files. The flash.net package contains classes to send and receive data across the Internet--for example, to load content from remote URLs, to communicate with other Flash Player instances, and to connect to remote websites.

In ActionScript 3.0, you can load external files with the URLLoader and URLRequest classes. You then use a specific class to access the data, depending on the type of data that was loaded. For instance, if the remote content is formatted as name-value pairs, you use the URLVariables class to parse the server results. Alternatively, if the file loaded using the URLLoader and URLRequest classes is a remote XML document, you can parse the XML document using the XML class's constructor, the XMLDocument class's constructor, or the XMLDocument.parseXML() method. This allows you to simplify your ActionScript code because the code for loading external files is the same whether you use the URLVariables, XML, or some other class to parse and work with the remote data.

The flash.net package also contains classes for other types of remote communication. These include the FileReference class for uploading and downloading files from a server, the Socket and XMLSocket classes that allow you to communicate directly with remote computers over socket connections, and the NetConnection and NetStream classes, which are used for communicating with Flash-specific server resources (such as Flash Media Server and Flash Remoting servers) as well as for loading video files.

Finally, the flash.net package includes classes for communication on the users' local computer. These include the LocalConnection class, which allows you to communicate between two or more SWF files running on a single computer, and the SharedObject class, which allows you to store data on a user's computer and retrieve it later when they return to your application.

Common networking and communication tasks

The following list describes the most common things you'll want to do related to external communication from ActionScript; these tasks are described in this chapter:

Important concepts and terms

The following reference list contains important terms that you will encounter in this chapter:

Working through in-chapter examples

While you're working through this chapter you might want to test the example code listings. Several of the code listings in the chapter load external data or perform some other type of communication; often these samples include trace() function calls, so the results of running the example are displayed in the Output panel. Other examples actually perform some function, such as uploading a file to a server. Testing those examples will involve interacting with the SWF and confirming that they perform the action they claim to perform.

The code examples fall into two categories. Some of the example listings are written assuming the code is in a standalone script, such as attached to a keyframe in a Flash document. To test those examples:

  1. Create a new Flash document.
  2. Select the keyframe on Frame 1 of the Timeline, and open the Actions panel.
  3. Copy the code listing into the Script pane.
  4. From the main menu, choose Control > Test Movie to create the SWF file and test the example.

Other example code listings are written as a class; the expectation is that the example class will serve as the document class for the Flash document. To test those examples:

  1. Create an empty Flash document and save it to your computer
  2. Create a new ActionScript file and save it in the same directory as the Flash document. The file's name should match the name of the class in the code listing. For instance, if the code listing defines a class named "UploadTest," save the ActionScript file as "UploadTest.as".
  3. Copy the code listing into the ActionScript file and save the file.
  4. In the Flash document, click on a blank part of the Stage or pasteboard to activate the document Property inspector.
  5. In the Property inspector, in the Document Class field, enter the name of the ActionScript class you copied from the text.
  6. Run the program using Control > Test Movie and test the example.

Finally, some of the examples in the chapter involve interacting with a program running on a server. These examples include code that can be used to create the necessary server program to test the example; you will need to set up the appropriate applications on a web server computer in order to test those examples.


Flash CS3


 

Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/9.0/main/00000315.html