Posted By

bshantz on 10/02/10


Tagged

actionscript as3 fms


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

tspitzr
dubogii


Basic FMS Connection AS3


 / Published in: ActionScript 3
 

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

  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  

You need to login to post a comment.