Flash Media Server from Flex 2

Flex2 with FMS2

OK, this one caused me to waste a bit of time so here I’m going to describe the problem in detail.  Hopefully it will save someone else the trouble I had.  Basically Flex 2 defaults to AMF3 and Flash Media Server 2 will only support AMF0.

On the FMS server side the application was simply an empty folder. On the client side I had the following actionscript:

var nc NetConnection = new NetConnection();
nc.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
private function netStatusHandler(event: NetStatusEvent): void
{
trace(event.info.code + " for " +event.currentTarget.uri);
}
When I would build this code into a flex2 app, I always got NetConnection.Connect.Failed. If I compiled the code through Flash CS3 it worked. I eventually used the flex debugger to inspect the event object in the netStatusHandler function. event.info.description was "objectEncoding error" and event.currentTarget.objectEncoding was 3! So I found that the following line of code easily solved all my headache:

nc.objectEncoding = flash.net.ObjectEncoding.AMF0;

You could also set the default for all instances of NetConnection and SharedObject as follows:

NetConnection.defaultObjectEncoding = flash.net.ObjectEncoding.AMF0;
SharedObject.defaultObjectEncoding = flash.net.ObjectEncoding.AMF0;

But I decided not to do this since my app also connect to a java backend through AMF.