This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
138
|
Chapter 4: Applications, Instances, and Server-Side ActionScript
PDF format from the URL cited earlier and also in LiveDocs format from http://
livedocs.macromedia.com/flashcom/mx2004) for a listing of all the properties and
methods of the Client class.
Various Client properties can be output to illustrate their values. The ping( ) and get-
Stats( ) methods of the Client class do more than provide static information about the
client. Calling ping( ) immediately sends a test message that makes the round-trip
from the server to the Flash client and back again. The message may take some time
to return, so the ping( ) method returns
true if the previous ping message was
returned by the client (or the server believes the client is still connected) and
false if
the client was not connected:
if (client.ping( )) {
return client.getStats( ).ping_rtt / 2;
}
else {
return "Client is not connected."
}
If ping( ) returns true, calling the getStats( ) method returns an information object
whose
ping_rtt property contains the round-trip time in milliseconds from the last
available ping( ) call. The ping( ) method should be used with caution because it
sends each ping message at the highest available priority—potentially delaying other
RTMP messages. When ping( ) is used to determine network latency, it should not be
called too frequently. As a guideline, Macromedia’s ConnectionLight component’s
default ping interval is 2 seconds.
Instance-to-Instance Communications
The NetConnection class is available to server-side scripts. Analogous to the NetCon-
nection class available to client-side scripts, it can be used to establish a network con-
nection between instances on a single server or between instances on separate
FlashCom Servers. When one instance attempts to connect to another, it creates and
uses a NetConnection object in an almost identical manner to the way client-side
ActionScript does. An instance that attempts to connect to another instance is
treated as a client by the second instance. The instance that receives another
instance’s connection request will be passed a Client object in its application.onCon-
nect( ) method. In this case, the Client object represents the first server-side Flash-
Com instance rather than a client-side Flash movie.
While an instance can request or close a connection to another instance at any time,
the connection is typically made when the instance first starts and closed when the
instance is about to be disposed. For example, a chat room instance may connect to
a lobby instance in order to let the lobby know how many users are in the room and
how active they are. This SSAS code example illustrates a chat room application con-
necting to a lobby on startup and disconnecting from the lobby on shutdown:
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Instance-to-Instance Communications
|
139
NetConnection.prototype.onStatus = function (info) {
trace("NetConnection.onStatus> info.code: " + info.code);
if (info.code == "NetConnection.Connect.Success") {
// Initialize remote shared objects here.
}
else if (!this.isConnected) {
// Handle close and other connection problems here.
}
if (info.code == "NetConnection.Call.Failed") {
// Handle failed remote method calls here.
}
};
application.onAppStart = function ( ) {
trace(application.name + " is starting at " + new Date( ));
lobby_nc = new NetConnection( );
lobby_nc.connect("rtmp://localhost/chapter4/lobby", "Room", "secretPassword6");
};
application.onAppStop = function ( ) {
if (lobby_nc.isConnected) {
lobby_nc.close( );
}
trace(application.name + " is stopping at " + new Date( ));
};
The only difference between this server-side code and the client-side code you might
find in a Flash movie is that a relative URI cannot be used (there must be a host-
name) and HTTP tunneling is not available. The connecting instance is represented
by the Client object passed into the receiving instance’s onConnect( ) method. If a
username and password system is being used, the connection can be accepted or
rejected based on those credentials.
On the receiving end of the connection attempt, no special code is required to han-
dle connection requests from other instances. However, sometimes it is useful to dis-
tinguish whether the connection request originated from a FlashCom application
instance or a Flash movie. One way to differentiate is to check the
ip property of the
incoming
client object. If the IP address is 127.0.0.1, then the connection is from
the same server. However, if a poorly configured proxy server is running on the same
host, all clients may appear to be from 127.0.0.1. Similarly, if connections are
expected from another FlashCom Server, the IP address of the remote server can be
checked for. There are other ways to distinguish between client types:
Pass different information in the optional parameters of the connect( ) method.
Check the
client.agent property for a value such as “FlashCom/1.5.2”.
Check the
client.referrer property for a value such as “rtmp://_defaultVHost_:
1935/chapter4/room”.

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.