This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
82
|
Chapter 3: Managing Connections
Each one of these URIs is an address of a separate instance of the chatRooms
application even though two of them contain the name “room8”. The instance
name
/chatRooms/motionPicture/film001/room8 is not the same as chatRooms/
science/phys325/room8
.
Relative URIs
If a .swf file is loaded into a browser from the same server that FlashCom is running
on, you can use a relative URI in which the
// characters and hostname are omitted.
Here is the format for a relative URI:
rtmp[t]:[:port]/applicationName[/instanceName]
In the earlier rtmp:/testLobby example, the protocol is rtmp, the single slash indicates
a relative URI, and testLobby is the application name. No application instance name
is provided, so the
_definst_ instance is assumed.
Waiting to Connect
After a successful connect( ) method invocation (one in which the URI is properly
formed), the NetConnection object must wait to see if the connection attempt itself is
successful. This can take some time as a number of things can happen:
The server may be down or unreachable, in which case the connection attempt
will eventually fail.
The network may be slow or the server may be busy.
The application instance may accept the TCP network connection but place it in
a pending state while it decides if it should reject or accept the connection.
Eventually, the NetConnection object is notified as to what happened via its onSta-
tus( ) method. By default, there is no onStatus( ) method, so you should define an
onStatus( ) handler before you invoke NetConnection.connect( ). The onStatus( )
method is known as an event handler or callback handler because it is invoked in
response to an event occurring (in this case, the server responding to the connection
attempt). The onStatus( ) handler receives an object containing information about
the event that helps interpret the response.
Here is a basic onStatus( ) method that simply outputs the contents of the informa-
tion object it receives:
NetConnection.prototype.onStatus = function (info) {
trace("this.isConnected: " + this.isConnected);
trace(" info.level: " + info.level);
trace(" info.code: " + info.code);
trace("info.description: " + info.description);
if (info.application) {
for (var prop in info.application) {
trace("info.application." + prop + ": " + info.application[prop]);
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Making a Connection
|
83
}
}
trace("\n");
};
The object received by onStaus( ), named info in this example, has three properties:
level, code, and description. The level property indicates whether the event is an
“error” or a “status” report. The
code property provides the most meaningful infor-
mation and should be used in onStatus( ) handlers to decide what action to take as
events occur. The preceding example also outputs the
isConnected property of the
NetConnection object. If the connection has been established,
isConnected is true;
otherwise,
isConnected is false.
Example 3-1 shows a simple test movie, followed by the output messages displayed if
the connection is successful.
If the connection is established, the
code property of the information object passed to
the onStatus( ) handler contains the string “NetConnection.Connect.Success”:
Attempting connection...
this.isConnected: true
Example 3-1. A short test script to display connection messages
// Some credentials to pass to the application.
userName = "Guest";
password = "Guest";
// First, define an onStatus( ) handler.
NetConnection.prototype.onStatus = function (info) {
trace("this.isConnected: " + this.isConnected);
trace(" info.level: " + info.level);
trace(" info.code: " + info.code);
trace("info.description: " + info.description);
if (info.application) {
for (var prop in info.application) {
trace("info.application." + prop + ": " + info.application[prop]);
}
}
trace("\n");
};
// Next, create a NetConnection object.
lobby_nc = new NetConnection( );
// Finally, attempt to make the connection.
// The onStatus( ) handler will be invoked when the operation completes.
if (lobby_nc.connect("rtmp:/testLobby/", userName, password)) {
trace("Attempting connection...");
}
else {
trace("Can't attempt connection. Is the URI correct?");
}

Get Programming Flash Communication Server now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.