AS3 concatenate rtmp connection string from xml be

2019-09-07 05:09发布

I keep getting a 'null' for the instance of the application under flash media server. I can't seem to pass the value from one function to another function

XML Sample

<bsettings>
<obj title="instance">19046</owner>
<obj title="id">uniqueid</owner>
<obj title="name">somename</owner>
<obj title="date">08/01/2012</owner>
<obj title="gender">female</owner>
</bsettings>

AS3 Code

<mx:Application 
    xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
    applicationComplete="initConnection(event)">

private var xinstance:String;
private var xmlstring:String = "http://www.blah.com/blahblah.xml";

protected function getXML():void{
XML.ignoreWhitespace = true;
var myXML:XML;
var myLoader:URLLoader=new URLLoader();
myLoader.load(new URLRequest(xmlstring));
myLoader.addEventListener(Event.COMPLETE, processXML);

function processXML(e:Event):void {
myXML = new XML(e.target.data);
for (var i:int = 0; i<myXML.*.length(); i++){
xinstance = myXML.obj[0];
xblah = myXML.obj[1];
xblah1 = myXML.obj[2];
xblah2 = myXML.obj[3];
xblah3 = myXML.obj[4];
}
}
}

private function initConnection(event:FlexEvent):void{
getXML();

//problem here, the xinstance isn't saved in the fmsstring
var fmsstring:String = "rtmp://blah.com/appname/" + xinstance;

nc = new NetConnection();
nc.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus);
nc.connect(fmsstring);
nc.client = this;
}

1条回答
时光不老,我们不散
2楼-- · 2019-09-07 05:58

Your problem is you're not waiting for the XML to load before using the xinstance string. On the initConnection function you call getXML() - which starts to load the xml, you need to wait until the xml is loaded before doing the rest of the code in the initConnection function. Here is a suggested change: (I added the go() function which is called AFTER the xml is processed)

private var xinstance:String;

protected function getXML():void {
    var xmlstring:String = "http://www.blah.com/blahblah.xml";
    XML.ignoreWhitespace = true;
    var myLoader:URLLoader=new URLLoader();
    myLoader.load(new URLRequest(xmlstring));
    myLoader.addEventListener(Event.COMPLETE, processXML);
}

protected function processXML(e:Event):void {
    var myXML:XML = XML(e.target.data)

    //THIS IS NOT A GOOD WAY TO DO THIS, use e4x
    //for (var i:int = 0; i<myXML.*.length(); i++){
    //xinstance = myXML.obj[0];
    //xblah = myXML.obj[1];
    //xblah1 = myXML.obj[2];
    //xblah2 = myXML.obj[3];
    //xblah3 = myXML.obj[4];
    //}

    //MUCH cleaner/easier/more efficient
    xinstance = myXML.bsettings.obj.(@title == "instance");

    go();
}

private function initConnection(event:FlexEvent):void{
    getXML();
}

private function go():void {
    var fmsstring:String = "rtmp://blah.com/appname/" + xinstance;

    nc = new NetConnection();
    nc.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus);
    nc.connect(fmsstring);
    nc.client = this;
}
查看更多
登录 后发表回答