EasyManua.ls Logo

MACROMEDIA FLASH MX 2004 - ACTIONSCRIPT - Sending Messages to and from Flash Player

MACROMEDIA FLASH MX 2004 - ACTIONSCRIPT
816 pages
Print Icon
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
Sending messages to and from Flash Player 185
You can use the connect() and send() methods of the XMLSocket class to transfer XML to and
from a server over a socket connection. The
connect() method establishes a socket connection
with a web server port. The
send() method passes an XML object to the server specified in the
socket connection.
When you invoke the
connect() method, Flash Player opens a TCP/IP connection to the server
and keeps that connection open until one of the following happens:
The close() method of the XMLSocket class is called.
No more references to the XMLSocket object exist.
Flash Player exits.
The connection is broken (for example, the modem disconnects).
The following example creates an XML socket connection and sends data from the XML object
myXML. To understand the script, read the commented lines (indicated by the characters //):
// Create a new XMLSocket object
sock = new XMLSocket();
// Call its connect() method to establish a connection with port 1024
// of the server at the URL
sock.connect("http://www.myserver.com", 1024);
// Define a function to assign to the sock object that handles
// the server’s response. If the connection succeeds, send the
// myXML object. If it fails, provide an error message in a text
// field.
function onSockConnect(success){
if (success){
sock.send(myXML);
} else {
msg="There has been an error connecting to "+serverName;
}
}
// Assign the onSockConnect() function to the onConnect property
sock.onConnect = onSockConnect;
For more information, see the XMLSocket class entry in Chapter 12, “ActionScript Dictionary,
on page 205.
Sending messages to and from Flash Player
To send messages from a SWF file to its host environment (for example, a web browser, a
Macromedia Director movie, or the stand-alone Flash Player), you can use the
fscommand()
function. This function lets you extend your SWF file by using the capabilities of the host. For
example, you could pass an
fscommand() function to a JavaScript function in an HTML page
that opens a new browser window with specific properties.
To control a SWF in Flash Player from web browser scripting languages such as JavaScript,
VBScript, and Microsoft JScript, you can use Flash Player methods—functions that send
messages from a host environment to the SWF. For example, you could have a link in an HTML
page that sends your SWF file to a specific frame.

Table of Contents

Related product manuals