How to get server endpoint in running flex application?

I need a way to get the active address, port and server context at runtime from my flex application. Since we use ant for our build process, server connection information is dynamically specified in our build properties file, and the services use the mail servers {server.name}, {server.port} and {context.root} -config.xml instead actual values.

We have several other Java servlets running on the same computer as our blazeDS server, and I would like to somehow programmatically determine the server endpoint information, so I don’t need to hardcode the servlet URL into an XML file (which is what we are doing now).

I found that I can at least get the context root by adding the following to our main application MXML file:

<mx:Application ... >
  <mx:HTTPService id="contextRoot" rootURL="@ContextRoot()"/>
</mx:Application>

However, I still need a way to get the server address and port, and if I specify the entire address by specifying -context-root = http://myserver.com:8080/mycontext , then the flex application will try to connect to http: // localhost / http://myserver.com:8080/mycontext/messagebroker/amf , which, of course, is completely wrong. What is the correct way to specify the root URL and the context URL and how to get it from our application?

+5
source share
6 answers

We use a subclass of the application that offers the following methods:

 /**
  * The URI of the AMF channel endpoint. <br/>
  * Default to #rootURI + #channelEndPointContext + #this.channelEndPointPathInfo
  */
 public function get channelEndPointURI() : String
 {
    return this.rootServerURI + ( this.channelEndPointContext ? this.channelEndPointContext : "" ) + this.channelEndPointPathInfo
 }

 /**
  * The root URI (that is scheme + hierarchical part) of the server the application
  * will connect to. <br/>
  * If the application is executing locally, this is the #localServerRootURI. <br/>
  * Else it is determined from the application #url. <br/>
  */ 
 public function get rootServerURI() : String
 {
      var result : String = ""
      if ( this.url && ( this.url.indexOf("file:/") == -1 ) )
      {
           var uri : URI = new URI( this.url )
           result = uri.scheme + "://" + uri.authority + ":" + uri.port
      }
      else
      {
           result = this.localServerRootURI
      }

      return result 
 }

channelEndPointContext, channelEndPointPathInfo localServerRootURI ( "mycontext" "/messagebroker/amf/" , , Flex Builder, URL file://).
URI localServerRootURI, url, , SWF (, , ).

, :

 <SuperApplication ...> <!-- SuperApplication is the enhanced Application subclass -->
    <mx:HTTPService id="myHTTPService" url="{this.channelEndPointURI}"/>
 </SuperApplication>

, channelEndPointContext URL- , , .

+3

FlashVars URL- . html:

var rootURL = location.href.substring(0,location.href.indexOf("flexBin"));    
...

AC_FL_RunContent(
    "src", "${swf}",
    "FlashVars", "rootURL="+rootURL,
    "width", "${width}",
...

flex:

service.rootURL = Application.application.parameters.rootURL;

, , , .

+2
var url:String = (FlexGlobals.topLevelApplication as Application).url 
            //var fullURL:String = mx.utils.URLUtil.getFullURL(url, url);

            var serverName:String = mx.utils.URLUtil.getServerNameWithPort(url);
            listContents.url = mx.utils.URLUtil.getProtocol(url)+"://"+serverName+"/custom_message.html";
            //isSecure = mx.utils.URLUtil.isHttpsURL(url);   

            //pageURL = pageURL.substring(0, pageURL.indexOf("/"));
            listContents.send();
+2
source

Why not call the javascript function in the shell via ExternalInterface to return location.hostname?

<mx:Script>
    <![CDATA[
        private var hostname:String;

        private function getHostName():void
        {
            hostname = ExternalInterface.call(getHostName);
        }
    ]]>
</mx:Script>

javascript in wrapper:

<script type="text/javascript">
    function getHostName()
    {
        return location.hostname;
    }
</script>
+1
source

You can use BrowserManager to get URL information.

var bm:IBrowserManager = BrowserManager.getInstance();
bm.init(Application.application.url);
var url:String = bm.base;

see also http://livedocs.adobe.com/flex/3/html/deep_linking_7.html#251252

+1
source

No need to do all this hard work. Just use the URLUtil provided by the Flex environment itself;)

+1
source

All Articles