PDA

View Full Version : is xml a legal plugin return value?



RansomedCaptive
11-11-2005, 10:44 AM
is the following legal (java plugin snippet):



...

Map params = new HashMap();

// generate xml for orbs
StringBuffer xml = new StringBuffer();
xml.append("<orbs>");
for (int i = 0; i < numOrbs; i++) {
orb = (Orb)orbs.get(i + "");

// create orb element
xml.append("<orb>");
xml.append("<id>" + orb.getId() + "</id>");
xml.append("<x>" + orb.getX() + "</x>");
xml.append("<y>" + orb.getY() + "</y>");
xml.append("</orb>");
}
xml.append("</orbs>");

params.put("orbsXml", xml.toString());

// tell clients to draw orbs
helper.sendPublicMessage("drawOrbs", params);

...

webgeek
11-11-2005, 11:00 AM
Yup, should be fine :)

RansomedCaptive
11-11-2005, 08:39 PM
That's what I thought, but no such luck:

ES Debug:


-----incomming----
<XmlDoc>
<Action>SendPluginMessage</Action>

<User>OrbFestPlugin</User>
<Message>drawOrbs</Message>
<Variables>
<Variable>
<Name>orbsXML</Name>
<Value><orbs><orb><id>0</id><x>31</x><y>58</y></orb></orbs></Value>
</Variable>
</Variables>
</Parameters>
</XmlDoc>


ActionScript:


es.pluginMessageReceived = function(plugin:String, action:String, variables:Object) {
switch (action) {
case "drawOrbs":
drawOrbs(variables);
break;
case "startGame":
startGame(variables);
break;
}
}

...

function drawOrbs(params:Object) {
for(var key in params) {
trace(key);
}

trace("----");

trace(params["orbsXML"]);
trace(params.orbsXML);

...
}


Trace output:


orbsXML
----
undefined
undefined


While I don't discount that I might be doing something wrong, my best guess is that something is breaking in the ES Actionscript: We can see the variable value is sent from the server and that the variable exists in the "params" object, but its value is undefined.

I read through ElectroServer.as (AS2.0). I think it has to do with the recursive nature of parseParameters() and the fact my value is XML. In applyAction(), in the if block for "SendPluginMessage":



} else if (action == "SendPluginMessage") {
var plugin:String = params.User.value;
var pluginAction:String = params.Message.value;
var variables:Array = params.Variables.Variables;
var ob:Object = new Object();
for (var i = 0; i<variables.length; ++i) {
var name:String = variables[i].Name.value;
var value = variables[i].Value.value;
trace(variables[i].Value.value);
trace(variables[i].Value);
ob[name] = value;
}
pluginMessageReceived(plugin, pluginAction, ob);
}


The first trace outputs "undefined" and the second "[object Object]" (presumably an XML or XMLNode object). It seems there should be a check to determine the type of variables[i].Value to determine what should be used for "value";

Wow, this is has proven to be a pretty extensive investigation--it would prove embarassing if I am completely off from the get go!

RansomedCaptive
11-11-2005, 08:56 PM
I did some fooling around and added this in the "SendPluginMessage" if block before "ob[name] = value;"



if (value == undefined) {
value = variables[i].Value;
}


My trace output is now:

orbsXML
----
[object Object]
[object Object]

As it turns out, the object is not an instance of XML or XMLNode, but just an Object that has implict properties in the same hierarchy as my xml:

params.orbXML.orbs

However, <orbs><orb/><orb/>...</orbs> produces only one params.orbXML.orbs.orb


...getting closer, but still not there yet

webgeek
11-12-2005, 09:59 PM
I know the server has no problem sending it. I'll have Jobe take a look at what you have found to determine if this is a bug in the electroserver.as code and what the work around could be. Thanks!

jobem
11-13-2005, 12:12 AM
Hi Daniel,

Like you pointed out, this is probably an issue with the recrusive parsing in the ElectroServer class. It is most likely seeing the value of your variable as actually being XML, not a string. Try putting your XML in CDATA tags. Like this:
<![CDATA[your_xml_here]]>

I'm pretty sure that will work. When the parser gets to the value of <Value></Value> it should see it as a string instead of more XML. Let us know either way.