Basic FMS Connection AS3


/ Published in: ActionScript 3
Save to your folder(s)

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


Copy this code and paste it in your HTML
  1. package {
  2.  
  3. import flash.display.Sprite;
  4. import flash.net.NetConnection;
  5. import flash.events.Event;
  6. import flash.events.NetStatusEvent;
  7. import flash.events.IOErrorEvent;
  8. import flash.events.SecurityErrorEvent;
  9.  
  10.  
  11. public class FMSConnection extends Sprite {
  12. public static const CONNECTED:String = 'connected';
  13. private var nc:NetConnection;
  14.  
  15. public function FMSConnection() {
  16. // constructor code
  17. connect('rtmp://YOUR-SERVERSIDE-APP', 'USERNAME');
  18. }
  19.  
  20. public function connect(url:String, username:String):void
  21. {
  22. // Create new net connection
  23. nc = new NetConnection();
  24. // Set this instance/class to receive callback functions.
  25. nc.client = this;
  26. // Call the connect method on the FMS and pass in the username.
  27. nc.connect(url, username);
  28.  
  29. nc.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus);
  30. nc.addEventListener(IOErrorEvent.IO_ERROR, onIoError);
  31. nc.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
  32. }
  33.  
  34. private function onNetStatus(event:NetStatusEvent):void
  35. {
  36. if(event.info.code == "NetConnection.Connect.Success")
  37. {
  38. trace("Connected To server");
  39. // Dispatch event letting the application know it connected.
  40. // Do Stuff here!
  41. dispatchEvent(new Event(FMSConnection.CONNECTED, true));
  42. }else{
  43. throw new Error("Failed to connect to server!");
  44. }
  45. }
  46.  
  47. private function onIoError(event:IOErrorEvent):void
  48. {
  49. throw new Error("IO Error on connection.");
  50. }
  51.  
  52. private function onSecurityError(event:SecurityErrorEvent):void
  53. {
  54. throw new Error("Security Error on connection.");
  55. }
  56. }
  57. /////////////////////////////////////////
  58. /* Brian Shantz -- 2010 */
  59. /* http://www.brianshantz.com */
  60. /* Flash Media Server Connection Script */
  61. /////////////////////////////////////////
  62. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.