-1

I am building a flash application which requires loading of an xml file using URLLoader. While developing application in my local machine with flash professional I can easily load it by

        private var myLoader:URLLoader = new URLLoader(new URLRequest("./com/assets/config.xml"));

When I publish the application and click on the html generated and the app loads on browser perfectly.

If I make a server (localhost:1111) that delivers the html file over browser on connect, the html file doesn't load the application (.swf).

While trying to debug it, I found that if I change the myLoader variable as below, the html file loads the swf properly.

    private var myLoader:URLLoader = new URLLoader(new URLRequest("http://localhost:1111/com/assets/config.xml"));

I believe the SWF is making another GET request after the html loads on my browser, that is the reason the SWF doesn't work without the change.

Is there any way I can load the xml file in SWF before it gets delivered over browser. This is to avoid another call to the server. I appreciate any help in clarifying my understanding and suggestion for workaround.

Learner
  • 157
  • 3
  • 15

1 Answers1

1

If you want to upload your SWF and have users access the configuration XML, you will need to host the XML somewhere reachable by your users. Your local machine (localhost:1111) is not reachable by anyone other than yourself (outside of some unusual hosts tweaking on the user's machine).

When you set up hosting and a web-server to actually serve the file over HTTP, you will need to do a few things:

  1. Set up a crossdomain file on the server to define which hosts are allowed to load your configuration XML.
  2. Amend your application to load data from the server, e.g. new URLRequest('http://your_domain_or_ip/config.xml').

The reason you cannot retain your reference to the XML file as a relative ./com/assets/config.xml is because the SWF will only load files over the local filesystem if it is being viewed as a file in the filesystem vs inside a browser.

When the SWF runs, the URLLoader instance you create will perform a HTTP GET to load your XML file.

If you want to avoid performing additional GET requests to fetch the XML, you will have to compile the configuration into the SWF itself using the [Embed] meta tag.

Marty
  • 39,033
  • 19
  • 93
  • 162
  • Isn't `Embed` a compile time option? If the .swf is already compiled and just being loaded from a server, why would any `Embed` directive be involved? – spring Feb 01 '16 at 05:50
  • @1202ProgramAlarm Yes, it's a compile-time option. The OP stated they're developing a Flash app and I assume this means they have control over the compilation process. It sounds like they just want to be able to include the configuration in the SWF rather than loading it from an external source which is exactly what this achieves, unless I am missing something? I don't see any mention of an existing SWF being loaded from a server anywhere - just an XML file being loaded in from a local server. – Marty Feb 01 '16 at 05:52
  • It seemed to me like the OP was wanting to save a network operation (and is likely an instance of misguided/unnecessary optimization) but not remove the option of changing the XML data after compilation. But who knows... – spring Feb 01 '16 at 05:55
  • I interpreted the situation as the OP not being aware of the Embed option and relying on URLLoader to fetch XML configuration. – Marty Feb 01 '16 at 05:57
  • Hi Guys, the local server is just an replica of how the actual process will happen over network. We want to have the option changing the config.xml file at our will without recompiling the swf. URLLoader can achieve the same but it requires another call to server. To summarize, we want to have the option of changing config.xml without recompiling the application and without making another call to server. – Learner Feb 01 '16 at 06:11
  • In that case there is no way to magically avoid fetching the configuration over HTTP the way you do now. – Marty Feb 01 '16 at 06:12
  • If we place the files in AWS S3, we are able to load the application over browser without making change i.e with private var myLoader:URLLoader = new URLLoader(new URLRequest("./com/assets/config.xml")); Does anyone have an explanation for the difference in S3 and local server? – Learner Feb 01 '16 at 06:19
  • If you deploy it somewhere and use `localhost` as the source for the configuration it won't work the way you expect because `localhost` will point to the user's machine not yours. You will have to set up hosting, upload the file and then update the path to point to the location of the uploaded file using the server IP or domain name e.g. `new URLRequest('http://yourdomain.com/config.xml')` or whatever it is. – Marty Feb 01 '16 at 06:32
  • There are also some security concerns you will need to address - the XML will not load unless the SWF is on the same domain or you set up an appropriate crossdomain file on your server. – Marty Feb 01 '16 at 06:34
  • Hi @Marty, thanks for your valuable inputs. However my main concern still remains as follows :If we place the files in AWS S3, we are able to load the application over browser without making change i.e with private var myLoader:URLLoader = new URLLoader(new URLRequest("./com/assets/config.xml")); Does anyone have an explanation for the difference in S3 and local server – Learner Feb 01 '16 at 06:45