Fixing the NetStatusEvent.info.code ugliness in Actionscript 3

Everything in AS3 is pretty straight forward once you get your head around it but the guys at Adobe seem to have become sloppy when they wrote the NetstatusEvent structure.
Ever since I got started with Red5, remote shared objects and all kind of different remote animals about 3 years ago, I ran into this ugly ‘NetstatusEvent.info.code‘ thing which is basically just a string. In order to know which one it is you have to use a switch statement to figure this out using real strings (instead of class constants) (which looks like the following).

private function onNetStatus(e:NetStatusEvent):void{
	switch (e.info.code){
		case"Netconnection.Connect.Success":
			trace("You connected!");
		break;
		case "Netconnection.Connect.Closed":
			trace("You disconnected!");
		break;
	}
}

As a very lazy developer, I tend to think that comparing strings like this is just crazy. And ugly! So I made my life just a little better by creating my own class that holds ‘InfoObjects‘ with the ‘code‘, ‘level‘ and ‘meaning‘ properties. That way you can just compare the ‘event.code.info’ to a class constant. No need for copy-pasting codes from the Adobe site, or debugging your code in order to find you made a typo in one of those strings…

I ran the table (listing all of the NetStatus codes) through a simple XML parser and ended up with a very neat Actionscript class (which I’m sharing, so it’s up for grabs! Download link at the end of this blogpost!). Using it looks way more natural!

private function onNetStatus(e:NetStatusEvent):void{
	switch (e.info.code){
		case NetStatusInfo.NETCONNECTION_CONNECT_SUCCESS.code:
			trace("You connected!");

			// returns the code property (same as 'toString()')
			trace(NetStatusInfo.NETCONNECTION_CONNECT_SUCCESS.code); 

			// returns the level property
			trace(NetStatusInfo.NETCONNECTION_CONNECT_SUCCESS.level); 

			 // returns the meaning of this status change
			trace(NetStatusInfo.NETCONNECTION_CONNECT_SUCCESS.meaning);
		break;
		case NetStatusInfo.NETCONNECTION_CONNECT_CLOSED.code:
			trace("You disconnected!");
		break;
	}
}

However….

Why isn’t this just event based? Right now the Netconnection dispatches a NetStatusEvent.NET_STATUS. Most people find this very acceptable. I, myself, tend to think it’s funny. The classname (NetStatusEvent) is pretty darn clear about the fact that the event triggered is related to the netstatus… Duh?!
Anyway: after having listened to that event and having caught it, we now need to evaluate the event.info.code property (which will return a string like ‘NetConnection.Connect.Success‘)… Why is that??
Why not just dispatch NetStatusEvent.NETCONNECTION_CONNECT_SUCCESS? Wouldn’t this be more consistent with the rest of Actionscript?

It would only make life better:

  1. No more copy-pasting of ‘codes‘. They’re just constants in the NetStatusEvent class.
  2. No chance of runtime bugs because of typo’s made in the string itself. The use of constants makes sure the compiler knows when you’re doing something horrible, and allows him to throw that error right in your face!
  3. Auto-completion! Because life should be made easier by computers; not harder!

I think that an implementation like mentioned above would be pretty cool (and perhaps more logical). At least to me…

Download

Feel free to use, modify, marry this class (and pass it on onto your children) if needed. If I overlooked anything or there’s a better way of doing this, feel free to ping me and I shall correct myself asap!

Enjoy!

Download: Click

Ronny

view all posts

Ronny is a freelance frontend developer with a wild passion for creativity and a relentless hate against flat design. Ronny spent years as a Flash developer before moving to HTML5 and rediscovering fun and happiness.

8 Comments Join the Conversation →


  1. Bozo

    Nice post. I agree with all your points.

    Reply
  2. gaurav

    thats really very helpful…………………..thankssssss

    Reply
  3. Thomas

    Thank you so much for this tutorial, it helped me alot to get started with red 5. Have now installed it on Windows, Linux and that horrible Apple thingie, works every time.

    Good Game

    Reply
  4. zorou

    Hey, this piece of code really helps me a lot.

    Many thanks for the distribution to us!

    Reply
  5. Guinetik

    i still think there should be different event types for each status dispatched. when you’re working with two net stream instances and you need to compare those info objects, you’ll know what i mean. great job, btw đŸ˜€

    Reply
  6. Konstantin

    Well, it would be much better if it returned objects created just once instead of creating new objects everytime. Not very good for realtime interaction with tens or hundreds of messages per second

    Reply
    • Ronny

      Hey Konstantin,

      I wrote this about 5 years ago, when I was way less experienced with large applications. Of course you are completely right. However, since I no longer do any Actionscript work, I’m afraid I won’t be able to help you out. But I’m sure you know what to do đŸ˜‰ Feel free to share your updated version here as well.
      Thanks!

      Reply (in reply to Konstantin)

Leave a Reply

Your email address will not be published. Required fields are marked *