Return to Snippet

Revision: 32807
at October 2, 2010 09:20 by bshantz


Initial Code
package  {
	
	import flash.display.Sprite;
	import flash.net.NetConnection;
	import flash.events.Event;
	import flash.events.NetStatusEvent;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	
	
	public class FMSConnection extends Sprite {
		public static const CONNECTED:String = 'connected';
		private var nc:NetConnection;
		
		public function FMSConnection() {
			// constructor code
			connect('rtmp://YOUR-SERVERSIDE-APP', 'USERNAME');
		}
		
		public function connect(url:String, username:String):void 
		{
			// Create new net connection
			nc = new NetConnection();
			// Set this instance/class to receive callback functions.
			nc.client = this;
			// Call the connect method on the FMS and pass in the username.
			nc.connect(url, username);
			
			nc.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus);
			nc.addEventListener(IOErrorEvent.IO_ERROR, onIoError);
			nc.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
		}
		
		private function onNetStatus(event:NetStatusEvent):void
		{
			if(event.info.code == "NetConnection.Connect.Success")
			{
				trace("Connected To server");
				// Dispatch event letting the application know it connected.
				// Do Stuff here!
				dispatchEvent(new Event(FMSConnection.CONNECTED, true));
			}else{
				throw new Error("Failed to connect to server!");
			}
		}
		
		private function onIoError(event:IOErrorEvent):void
		{
			throw new Error("IO Error on connection.");
		}
		
		private function onSecurityError(event:SecurityErrorEvent):void
		{
			throw new Error("Security Error on connection.");
		}
	}
	/////////////////////////////////////////
	/* Brian Shantz -- 2010 */
	/* http://www.brianshantz.com */
	/* Flash Media Server Connection Script */
	/////////////////////////////////////////
}

Initial URL

                                

Initial Description
This is the standard code needed when connecting to a media server.

Initial Title
Basic FMS Connection AS3

Initial Tags
actionscript

Initial Language
ActionScript 3