The NetConnection class creates a two-way connection between a client and a server. The client can be a Flash Player or AIR application. The server can be a web server, Flash Media Server, an application server running Flash Remoting, or the <a href="http://labs.adobe.com/technologies/stratus/" scope="external">Adobe Stratus service. Call NetConnection.connect() to establish the connection. Use the NetStream class to send streams of media and data over the connection. For security information about loading content and data into Flash Player and AIR, see the following:

To write callback methods for this class, extend the class and define the callback methods in the subclass, or assign the client property to an object and define the callback methods on that object.

Events:

asyncError

Dispatched when an exception is thrown asynchronously × that is, from native asynchronous code.

ioError

Dispatched when an input or output error occurs that causes a network operation to fail.

netStatus

Dispatched when a NetConnection object is reporting its status or error condition. The netStatus event contains an info property, which is an information object that contains specific information about the event, such as whether a connection attempt succeeded or failed.

securityError

Dispatched if a call to NetConnection.call() attempts to connect to a server outside the caller's security sandbox.

Constructor

new()

Creates a NetConnection object. Call the connect() method to make a connection. If an application needs to communicate with servers released prior to Flash Player 9, set the NetConnection object's objectEncoding property.

The following code creates a NetConnection object:

var nc = new NetConnection();

Methods

@:value({ p5 : null, p4 : null, p3 : null, p2 : null, p1 : null })connect(command:String, ?p1:Unknown, ?p2:Unknown, ?p3:Unknown, ?p4:Unknown, ?p5:Unknown):Void

Creates a two-way connection to an application on Flash Media Server or to Flash Remoting, or creates a two-way network endpoint for RTMFP peer-to-peer group communication. To report its status or an error condition, a call to NetConnection.connect() dispatches a netStatus event. Call NetConnection.connect() to do the following:

  • Pass "null" to play video and mp3 files from a local file system or from a web server.
  • Pass an "http" URL to connect to an application server running Flash Remoting. Use the NetServices class to call functions on and return results from application servers over a NetConnection object. For more information, see the <a href="http://www.adobe.com/support/documentation" scope="external">Flash Remoting documentation.
  • Pass an "rtmp/e/s" URL to connect to a Flash Media Server application.
  • Pass an "rtmfp" URL to create a two-way network endpoint for RTMFP client-server, peer-to-peer, and IP multicast communication.
  • Pass the string "rtmfp:" to create a serverless two-way network endpoint for RTMFP IP multicast communication.

Consider the following security model:

  • By default, Flash Player or AIR denies access between sandboxes. A website can enable access to a resource by using a URL policy file.
  • Your application can deny access to a resource on the server. In a Flash Media Server application, use Server-Side ActionScript code to deny access. See the <a href="http://www.adobe.com/go/learn_fms_docs_en" scope="external">Flash Media Server documentation.
  • You cannot call NetConnection.connect() if the calling file is in the local-with-file-system sandbox.
  • You cannot connect to commonly reserved ports. For a complete list of blocked ports, see "Restricting Networking APIs" in the OpenFL Developer's Guide.
  • To prevent a SWF file from calling this method, set the allowNetworking parameter of the the object and embed tags in the HTML page that contains the SWF content.

However, in Adobe AIR, content in the application security sandbox (content installed with the AIR application) are not restricted by these security limitations.

For more information about security, see the Adobe Flash Player Developer Center: <a href="http://www.adobe.com/go/devnet_security_en" scope="external">Security.

Parameters:

command

Use one of the following values for the command parameter: To play video and mp3 files from a local file system or from a web server, pass null. To connect to an application server running Flash Remoting, pass a URL that uses the http protocol. (Flash Player 10.1 or AIR 2 or later) To create a serverless network endpoint for RTMFP IP multicast communication, pass the string "rtmfp:". Use this connection type to receive an IP multicast stream from a publisher without using a server. You can also use this connection type to use IP multicast to discover peers on the same local area network (LAN). This connection type has the following limitations: Only peers on the same LAN can discover each other. Using IP multicast, Flash Player can receive streams, it cannot send them. Flash Player and AIR can send and receive streams in a peer-to-peer group, but the peers must be discovered on the same LAN using IP multicast. This technique cannot be used for one-to-one communication.

To connect to Flash Media Server, pass the URI of the application on the server. Use the following syntax (items in brackets are optional): protocol:[//host][:port]/appname[/instanceName]

Use one of the following protocols: rtmp, rtmpe, rtmps, rtmpt, rtmpte, or rtmfp. If the connection is successful, a netStatus event with a code property of NetConnection.Connect.Success is returned. See the NetStatusEvent.info property for a list of all event codes returned in response to calling connect().

If the file is served from the same host where the server is installed, you can omit the //host parameter. If you omit the /instanceName parameter, Flash Player or AIR connects to the application's default instance.

(Flash Player 10.1 or AIR 2 or later)To create peer-to-peer applications, use the rtmfp protocol.

Throws:

ArgumentError

The URI passed to the command parameter is improperly formatted.

IOError

The connection failed. This can happen if you call connect() from within a netStatus event handler, which is not allowed.

SecurityError

Local-with-filesystem SWF files cannot communicate with the Internet. You can avoid this problem by reclassifying the SWF file as local-with-networking or trusted.

SecurityError

You cannot connect to commonly reserved ports. For a complete list of blocked ports, see "Restricting Networking APIs" in the OpenFL Developer's Guide.

Inherited Variables

Inherited Methods

Defined by EventDispatcher

@:value({ useWeakReference : false, priority : 0, useCapture : false })addEventListener<T>(type:EventType<T>, listener:T ‑> Void, useCapture:Bool = false, priority:Int = 0, useWeakReference:Bool = false):Void

Registers an event listener object with an EventDispatcher object so that the listener receives notification of an event. You can register event listeners on all nodes in the display list for a specific type of event, phase, and priority.

After you successfully register an event listener, you cannot change its priority through additional calls to addEventListener(). To change a listener's priority, you must first call removeListener(). Then you can register the listener again with the new priority level.

Keep in mind that after the listener is registered, subsequent calls to addEventListener() with a different type or useCapture value result in the creation of a separate listener registration. For example, if you first register a listener with useCapture set to true, it listens only during the capture phase. If you call addEventListener() again using the same listener object, but with useCapture set to false, you have two separate listeners: one that listens during the capture phase and another that listens during the target and bubbling phases.

You cannot register an event listener for only the target phase or the bubbling phase. Those phases are coupled during registration because bubbling applies only to the ancestors of the target node.

If you no longer need an event listener, remove it by calling removeEventListener(), or memory problems could result. Event listeners are not automatically removed from memory because the garbage collector does not remove the listener as long as the dispatching object exists(unless the useWeakReference parameter is set to true).

Copying an EventDispatcher instance does not copy the event listeners attached to it.(If your newly created node needs an event listener, you must attach the listener after creating the node.) However, if you move an EventDispatcher instance, the event listeners attached to it move along with it.

If the event listener is being registered on a node while an event is being processed on this node, the event listener is not triggered during the current phase but can be triggered during a later phase in the event flow, such as the bubbling phase.

If an event listener is removed from a node while an event is being processed on the node, it is still triggered by the current actions. After it is removed, the event listener is never invoked again(unless registered again for future processing).

Parameters:

type

The type of event.

useCapture

Determines whether the listener works in the capture phase or the target and bubbling phases. If useCapture is set to true, the listener processes the event only during the capture phase and not in the target or bubbling phase. If useCapture is false, the listener processes the event only during the target or bubbling phase. To listen for the event in all three phases, call addEventListener twice, once with useCapture set to true, then again with useCapture set to false.

priority

The priority level of the event listener. The priority is designated by a signed 32-bit integer. The higher the number, the higher the priority. All listeners with priority n are processed before listeners of priority n-1. If two or more listeners share the same priority, they are processed in the order in which they were added. The default priority is 0.

useWeakReference

Determines whether the reference to the listener is strong or weak. A strong reference(the default) prevents your listener from being garbage-collected. A weak reference does not.

Class-level member functions are not subject to garbage collection, so you can set useWeakReference to true for class-level member functions without subjecting them to garbage collection. If you set useWeakReference to true for a listener that is a nested inner function, the function will be garbage-collected and no longer persistent. If you create references to the inner function (save it in another variable) then it is not garbage-collected and stays persistent.

Weak references are supported on some OpenFL targets only, including html5, cpp, and flash/air. On other targets, this parameter is ignored, and the reference will be strong instead.

Throws:

ArgumentError

The listener specified is not a function.

dispatchEvent(event:Event):Bool

Dispatches an event into the event flow. The event target is the EventDispatcher object upon which the dispatchEvent() method is called.

Parameters:

event

The Event object that is dispatched into the event flow. If the event is being redispatched, a clone of the event is created automatically. After an event is dispatched, its target property cannot be changed, so you must create a new copy of the event for redispatching to work.

Returns:

A value of true if the event was successfully dispatched. A value of false indicates failure or that preventDefault() was called on the event.

Throws:

Error

The event dispatch recursion limit has been reached.

hasEventListener(type:String):Bool

Checks whether the EventDispatcher object has any listeners registered for a specific type of event. This allows you to determine where an EventDispatcher object has altered handling of an event type in the event flow hierarchy. To determine whether a specific event type actually triggers an event listener, use willTrigger().

The difference between hasEventListener() and willTrigger() is that hasEventListener() examines only the object to which it belongs, whereas willTrigger() examines the entire event flow for the event specified by the type parameter.

When hasEventListener() is called from a LoaderInfo object, only the listeners that the caller can access are considered.

Parameters:

type

The type of event.

Returns:

A value of true if a listener of the specified type is registered; false otherwise.

@:value({ useCapture : false })removeEventListener<T>(type:EventType<T>, listener:T ‑> Void, useCapture:Bool = false):Void

Removes a listener from the EventDispatcher object. If there is no matching listener registered with the EventDispatcher object, a call to this method has no effect.

Parameters:

type

The type of event.

useCapture

Specifies whether the listener was registered for the capture phase or the target and bubbling phases. If the listener was registered for both the capture phase and the target and bubbling phases, two calls to removeEventListener() are required to remove both, one call with useCapture() set to true, and another call with useCapture() set to false.

toString():String

willTrigger(type:String):Bool

Checks whether an event listener is registered with this EventDispatcher object or any of its ancestors for the specified event type. This method returns true if an event listener is triggered during any phase of the event flow when an event of the specified type is dispatched to this EventDispatcher object or any of its descendants.

The difference between the hasEventListener() and the willTrigger() methods is that hasEventListener() examines only the object to which it belongs, whereas the willTrigger() method examines the entire event flow for the event specified by the type parameter.

When willTrigger() is called from a LoaderInfo object, only the listeners that the caller can access are considered.

Parameters:

type

The type of event.

Returns:

A value of true if a listener of the specified type will be triggered; false otherwise.