Quick Tip: Working with NetStream

While working on my brand new P2P project I have come to some issues and solutions, that may help you with your projects. First thing, the NetStatusEvent codes. You may expect that codes are dispatched by the instance of the class they beigins with (e.g. “NetStream.Connect.Success” dispatched by NetStream), but in fact, this does not happen (“NetStream.Connect.Success” is dispatched by NetConnection). I bellieve its not a bug but a feature by flash player developer team. To make this work as expected you may do the following:

netConnection.addEventListener(type, onNetStatus);

function onNetConnectionNetStatus(event:NetStatusEvent):void
{
	switch(event.info.code){
		case "NetStream.Connect.Success":
			event.info.stream.dispatchEvent(event);
			break;
		...
	}
}

This way your NetStream dispatches the same NetStatusEvent into itself. You may now wonder why am I doing it? There are 2 reasons for that:

  • This way I can easily handle multiple NetStreams (in multiple objects)
  • This event may be used for attaching camera/microphone into your NetStream (I guess due to peer-assisted security restrictions).
netGroup.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus);
netStream.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus);

function onNetStatus(event:NetStatusEvent):void
{
	switch(event.info.code)
	{
		case "NetStream.Connect.Success":
			netStream.attachCamera(camera);
			netStream.attachAudio(microphone);
			netStream.publish("someName");
			break;
		case "NetGroup.Neighbor.Connect":
			subscribers++;
			break;
		case "NetGroup.Neighbor.Disconnect":
			subscribers--;
			break;
	}
}

attaching camera / microphone after NetStream is connected, prevents you from follofing:

var camera:Camera = Camera.getCamera();
var netStream:NetStream = new NetStream(netConnection,
    groupspec.groupspecWithAuthorizations());
stream.attachCamera(cam);

…will result in runtime error:

Error #2154: The NetStream Object is invalid. This may be due to a failed NetConnection.

NetGroup.Connect.Closed

Getting this annoying, undocumented error code?

class
{
    private function onConnect():void
    {
        var netGroup:NetGroup = new NetGroup(netConnection, groupspec);
        netGroup.addEventListener(type, onNetStatus);
    }
    // dispatches NetStatusEvent with code NetGroup.Connect.Closed
}

… workaround:

class
{
    private var var netGroup:NetGroup; // using class variable!

    private function onConnect():void
    {
        netGroup = new NetGroup(netConnection, groupspec);
        netGroup.addEventListener(type, onNetStatus);
    }
}

This seems like flash player bug. I guess this bug has something to do with reference to a NetGroup instance. If you use function variable, this get cleared after function finishes even with attached listener (guess), while class variable reference remains.

Leave a comment

Please be polite and on topic. Your e-mail will never be published.

Get Adobe Flash playerPlugin by wpburn.com wordpress themes