XML variables

From EsWiki

Jump to: navigation, search

XML variables are often used to pass information to programs. They are easy to edit, and you don't need to know how to program to just change a value. One common use is in the Extension.xml file, to pass values to a server-level plugin. Another common use is a ServerSettings.xml file that is read by a Flash client to get the IP and port for connecting with the server.

Contents

Formatting XML variables

Formatting the XML variables can be tricky, but ElectroServer provides tools to help. Simply write a small Flash program that builds a sample EsObject that would hold the variables you want, then use the new EsObject.toXML() method to convert the EsObject to XML. Output it, then copy and paste where needed.

Java programmers will have EsObject.toXML() with release 4.0.5, as well as EsObject.toString() and EsObject.fromXML.

Example XML variables in Extension.xml

            <Variables>
                 <Variable name="poolname" type="string">mysqlpool</Variable>
                 <Variable name="trace" type="boolean">true</Variable>
            </Variables>

Example XML variables read from Web Admin

Both plugins with server-level components and persistent rooms may have XML variables created from the web admin. The format for this is:

<Variables>
   <Variable name='life' type='string' >42</Variable>
</Variables>

Reading the XML variables

Server-level plugin

Any Server-level Plugin will have an init(EsObjectRO parameters) method. The parameters object will hold any variables from this particular plugin's section of the Extension.xml file. Simple!

Flash client

ServerSettings.xml

Here is one way to pass the connection information to a Flash client using XML. First, make a ServerSettings.xml file containing the settings. For a local only connection, this would be something like:

 <Listeners>
       <Listener ip="127.0.0.1" port="9898" protocol="text" />
       <Listener ip="127.0.0.1" port="1935" protocol="rtmp" />
       <Listener ip="127.0.0.1" port="9899" protocol="binary" />
 </Listeners>

You only need to mention the protocols that you will be using. If the client will only need text, then one line is enough.

Read the XML

These lines can be put in the Actions layer of frame 1, or they can be placed in Main.as (the imports at the top, and the rest inside appropriate methods).

 import flash.events.Event;
 import flash.net.URLLoader;
 import flash.net.URLRequest;
 var serverInfo:Object;
 var loader:URLLoader = new URLLoader();
 loader.addEventListener(Event.COMPLETE, serverSettingsComplete);
 var request:URLRequest = new URLRequest("ServerSettings.xml");
 loader.load(request);

Build a serverInfo object

If you read the XML in a frame, use a function similar to this to build a serverInfo object:

 function serverSettingsComplete(e:Event) {
   var loader:URLLoader = URLLoader(e.target);
   var result:XML = new XML(loader.data);
   serverInfo = new Object();
   for each (var lis:XML in result.Listener) {
      var protocol:String = lis.@protocol;
      var ip:String = lis.@ip;
      var port:Number = Number(lis.@port);
      serverInfo[protocol] = new Object();
      serverInfo[protocol].ip = ip;
      serverInfo[protocol].port = port;
   }
   gotoAndStop("Main");
 }

If you read the XML in Main.as, simply replace gotoAndStop("Main"); with a call to the method that then uses the serverInfo object to connect to ES4.

Using the serverInfo object

Any method that needs to establish a connection to ES4 can use a line similar to this one:

  es.createConnection(serverInfo.text.ip, serverInfo.text.port);

The serverInfo object will need to be available to the method of course, either as a class variable or passed as a parameter.

Personal tools
download