Flash Media Server Developer Documentation

NetConnection.connect()

public connect(targetURI:String, [, p1,...,pN]) : Boolean

Creates a connection between Flash Player and an application on Flash Media Server by using one of the following versions of Real-Time Messaging Protocol (RTMP):

Protocol

Description

RTMP

Real-Time Messaging Protocol

RTMPT

Real-Time Messaging Protocol tunneled over HTTP

RTMPE

128-bit encrypted Real-Time Messaging Protocol

RTMPTE

128-bit encrypted Real-Time Messaging Protocol tunnelled over HTTP

RTMPS

Real-Time Messaging Protocol over SSL

Availability

Flash Communication Server 1.0; Flash Player 6.

The RTMPE and RTMPTE protocols are available in Flash Media Server 3; Flash Player 9 Update 3.

Parameters

targetURI The Uniform Resource Identifier (URI) of the application on the Flash Media Server that runs when the connection is made. Use the following syntax: protocol:[//host][:port]/appname/[instanceName].

For protocol, specify rtmp, rtmpt, rtmps, rtmpe, or rtmpte.

If Flash Player fails to connect to the server over an RTMP connection on the default port, 1935, it automatically tries to establish a connection by using the following sequence of ports and protocols:

my_nc.connect("rtmp://myserver/myapp"); // uses the default port 1935
my_nc.connect("rtmp://myserver:443/myapp");
my_nc.connect("rtmp://myserver:80/myapp");
my_nc.connect("rtmpt://myserver:80/myapp");

This connection sequence can enable connections to succeed that otherwise would not. However, during this connection sequence, users may wait several seconds for multiple connection attempts to time out. This automatic retry sequence occurs only if the initial connection specifies the RTMP protocol and uses the default port--for example, my_nc.connect("rtmp://myserver/myapp").

If clients are connecting from behind a firewall or through HTTP proxy servers that prohibit direct RTMP socket connections, use the RTMPT or RTMPTE protocol so that clients don't have to wait through the connection sequence.

You can omit the host parameter if the SWF file is served from the same host where Flash Media Server is installed. If the instanceName parameter is omitted, Flash Player connects to the application's default instance (definst). The following URIs are formatted correctly:

rtmp://www.example.com/myMainDirectory/groupChatApp/HelpDesk
rtmp://sharedWhiteboardApp/June2002

p1 ... pN Optional parameters of any type to be passed to the application specified in targetURI.

Returns

A Boolean value of true if you passed in a valid URI; otherwise, false. (To determine if the connection was successfully completed, use NetConnection.onStatus.)

Details

When you call NetConnection.connect(), the NetConnection.onStatus() event handler is invoked with an information object that specifies whether the connection succeeded or failed. For more information, see NetConnection.onStatus(). If the connection is successful, NetConnection.isConnected is set to true.

Because of network and thread timing issues, it is better to place a NetConnection.onStatus() handler in a script before a NetConnection.connect() method. Otherwise, the connection might complete before the script executes the onStatus() handler initialization. Also, all security checks are made within the NetConnection.connect() method, and if the onStatus() handler is not yet set up, notifications are lost.

If the specified connection is already open when you call this method, an implicit NetConnection.close() method is invoked, and then the connection is reopened.

During the connection process, any server responses to a subsequent NetConnection.call(), NetStream.send(), or SharedObject.send() method are queued until the server authenticates the connection and NetConnection.onStatus() is invoked. The call() or send() method is then processed in the order received. Any pending updates to remote shared objects are also queued until the connection is successful, at which point they are transmitted to the server.

Video and audio, however, are not queued during the connection process. Any video or audio that is streaming from the server is ignored until the connection is successfully completed. For example, confirm that the connection was successful before enabling a button that calls the NetStream.publish() method.

If your connection fails, make sure that you have met all the requirements for connecting successfully:

  • You are specifying the correct protocol name.
  • If you are using RTMPE or RTMPTE, you are using the correct server and Flash Player version.
  • You are connecting to a valid application on the correct server.
  • You have a subdirectory in the applications directory with the same name as the application specified in the connection URL.
  • The server is running.
  • You are connecting to the port on which the server is listening. This is specified in the ADAPTOR.HOSTPORT parameter in the fms.ini file.

To distinguish among different instances of a single application, pass a value for instanceName as part of targetURI. For example, you may want to give different groups of people access to the same application without having them interact with each other. To do so, you can open multiple chat rooms at the same time, as the following example shows:

my_nc.connect("rtmp://www.myserver.com/chatApp/peopleWhoSew")
my_nc.connect("rtmp://www.myserver.com/chatApp/peopleWhoKnit")

In another case, you may have an application named lectureSeries that records and plays back classroom lectures. To save individual lectures, pass a different value for instanceName each time you record a lecture, as shown in the following example:

// Record Monday's lecture.
my_nc.connect("rtmp://www.myserver.com/lectureSeries/Monday");
// Later ...
my_ns.connect(my_nc);
my_ns.publish("lecture", "record");

// Record Tuesday's lecture.
my_nc.connect("rtmp://www.myserver.com/lectureSeries/Tuesday");
// Later ...
my_ns.connect(my_nc);
my_ns.publish("lecture", "record");
// and so on

// Play back one of the lectures.
my_nc.connect("rtmp://www.myserver.com/lectureSeries/Monday");
// Later ...
my_ns.connect(my_nc);
my_ns.play("lecture")

You can also nest instance names, as shown in the following example:

my_nc.connect("rtmp://www.myserver.com/chatApp/peopleWhoSew/Monday")
my_nc.connect("rtmp://www.myserver.com/chatApp/peopleWhoSew/Tuesday")

For information on where recorded streams are stored on the server, see NetStream.publish().

To use this connection to publish or play audio or video in real time, or to publish or play previously recorded audio or video streams, you must connect to the server, create a NetStream object, and pass it the NetConnection object. For more information, see the NetStream class entry.

To use this connection for synchronizing data among multiple clients or between the client and a server, you must connect to the server, and then create a remote shared object and pass it the NetConnection object. For more information, see the SharedObject class entry.

Example

The following example connects over RTMP to the default instance of the funAndGames application by using the default port:

my_nc = new NetConnection();
my_nc.connect("rtmp://www.example.com/funAndGames");

The following example connects over the RTMP protocol to the room_01 application instance of the chat application using the default port. In this example, the SWF file is served from the same host and domain as Flash Media Server, so the host parameter is omitted.

my_nc = new NetConnection();
my_nc.connect("rtmp:/chat/room_01");

See also

NetConnection.onStatus()