NetConnection onStatus not called
You ever have one of those problems where you spend so much time trying to figure it out that you feel mentally and physically exhausted by the end? And when you figure it out, not only is it a huge relief but it was something so simple that you just want to punch yourself for not figuring it out sooner?
I was starting a new project using Flash Media Server 2 and Flash 8. I started with a little boiler plate code: NetConnection.onStatus handler on the client then establishing a connection with NetConnection.connect. The server had your basic application.onConnect / application.onDisconnect set of handlers in main.asc.
Server code: main.asc
application.onAppStart = function()
{
trace("onAppStart");
}
application.onConnect = function(client)
{
trace("onConnect> " + client.ip);
}
application.onDisconnect = function(client)
{
trace("onDisconnect> " + client.ip);
}
Client code
var nc:NetConnection = new NetConnection();
nc.onStatus = function(info)
{
for (var i in info)
trace(i + ":" + info[i]);
};
var result = nc.connect("rtmp:/consonation");
Standard stuff, right? Well, my client side onStatus was not being called. The typical answer you find to this problem is that the onStatus handler is being defined after your call to NetConnection::connect() but that's not the case here. I even tried coding this up in Flex 2 to double check it. Nothing worked.
I could see my trace code on the server being called when the application connected and disconnected. I knew I was making a successful connection. This is what was throwing me off.
Know what the answser was? I was so irritated that I missed this. You need to explicitly call application.acceptConnection(client); in your onConnect handler and I wasn't. Too bad there isn't a separate onStatus callback for successful socket connection and connection accepted. This would have made it obvious.
Fixed server code: main.asc
application.onAppStart = function()
{
trace("onAppStart");
}
application.onConnect = function(client)
{
trace("onConnect> " + client.ip);
application.acceptConnection(client);
}
application.onDisconnect = function(client)
{
trace("onDisconnect> " + client.ip);
}
