ActionScript Cross-post: Flash Media Server is CRAZY!

This is a cross-post from my Labs site that I set up for code tidbits from school projects.

And not in a Alicia Silverstone and Liv Tyler 90's music video hot kinda way, either. The kinda way that makes you think, "What the hell were they thinking when they implemented this crap?" After that you think, "And why didn't they make good tutorials?"

Let's talk FMS + AS3. Since the tutorials are in some kind of AS1/2 moon language, I thought I should post the AS3 code I finally managed to get working, so that you all won't have to waste your time (hopefully). Here goes.

Client-Side
The client-side is pretty straight forward if you know AS3, but if you've been trying to use the guides Adobe gives you, there's a slight twist.

 
package {
  import flash.events.NetStatusEvent;
  import flash.events.SyncEvent;
 
  //This bit is important!
  import CustomNC;
 
  public class MediaServer {
    //Note the use of a custom class (CustomNC) instead of NetConnection!
    private var client_nc:CustomNC;
    private var rtmpAddress:String = "rtmp://rtmp.address/my_rtmp_app_folder";
 
    public function MediaServer() {
      //trace("New MediaServer");
      init();
    }
 
    public function init() {
      client_nc = new CustomNC();
      client_nc.connect(rtmpAddress);
      client_nc.addEventListener(NetStatusEvent.NET_STATUS,onClientStatus);
    }
 
    private function onClientStatus(e:NetStatusEvent) {
      trace(e.info.code);
      if(e.info.code == "NetConnection.Connect.Success") {
        trace("Successfully connected to RTMP server.");
      }
    }
  }
}
 

This is the class I created to deal with Flash Media Server. One key piece here is to create a custom NetConnection class (which I'll provide in a sec) to allow the server to send down function calls.

 
package {
  import flash.net.NetConnection;
 
  public class CustomNC extends NetConnection {
    public function CustomNC() {
      //trace("New Custom NetConnection");
    }
 
    public function myCustomFunction() {
      trace("Sup");
    }
  }
}
 

That's it! I know it's not much, but it's the only way I've found to allow the server to pass down orders. This bit of code is intended to replace this (because it's no longer valid syntax in AS3):

 
myNetConnection_nc.myFunction = new function() {
  //Stuff!
}
 

Server-Side

Now for the server-side stuff. Honestly, this is looking way easier than it was to figure out. Oh well.

 
application.onConnect = function(newClient) {
  application.acceptConnection(newClient);
 
  trace("Accepted connection");
 
  newClient.call("myCustomFunction");
}
 

That's pretty much it! Save that as main.asc and pop it into your RTMP folder and it should run like a charm. Theoretically. If you need to troubleshoot, don't forget to reboot your application (just the app, not the server) after overwriting your main.asc file. Otherwise, it won't use the new code!

Leave a comment if anything is unclear or you're still having problems. Or email me at chris at iamnotgoodattheinter dot net.

Oh, I almost forgot, the ActionScript syntax made possible by this cool WordPress plugin. Go nuts!

http://ideathinking.com/wiki/index.php/WordPress:CodeHighlighterPlugin

Leave a Reply