5

我需要一种在运行时从我的 flex 应用程序中获取活动服务器地址、端口和上下文的方法。由于我们在构建过程中使用了 ant,服务器连接信息在我们的构建属性文件中动态指定,并且在 services-config 中使用了 {server.name}、{server.port} 和 {context.root} 占位符.xml 文件而不是实际值。

我们有一些其他 Java servlet 与我们的 blazeDS 服务器在同一台机器上运行,我想要某种方式以编程方式确定服务器端点信息,因此我不需要将 servlet URL 硬编码到 XML 文件中(这就是我们目前正在做)。

我发现至少可以通过将以下内容添加到我们的主应用程序 MXML 文件中来获取上下文根:

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

但是,我仍然需要某种方式来获取服务器地址和端口,如果我通过给出 -context-root= http://myserver.com:8080/mycontext来指定整个地址,那么 flex 应用程序会尝试连接到http ://localhost/http://myserver.com:8080/mycontext/messagebroker/amf,这当然是完全错误的。指定上下文根和服务器 URL 的正确方法是什么,如何从我们的应用程序中检索它们?

4

6 回答 6

3

We use an Application subclass 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 
 }

This generic application supports the channelEndPointContext, channelEndPointPathInfo and localServerRootURI properties (typically "mycontext" and "/messagebroker/amf/" in your example, the local server root being used when the application is executed via Flex Builder, in such cases it has a file:// URL).
The determination of the complete endpoint URI is then performed using either the localServerRootURI property or using the application url as our services are exposed by the very same server that serves the application's SWF (which is, as far as I understand your case too).

So, in your example, one would write :

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

Starting from here, one can also automatically determine the channelEndPointContext from the application URL instead of hardcoding it as shown in this example.

于 2009-04-15T12:58:10.687 回答
2

I've used FlashVars to pass urls in before with success. In your template html:

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

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

And then in flex:

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

The nice thing is you can really pass in whatever you like from the server this way.

于 2009-04-17T04:27:04.000 回答
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();
于 2011-09-20T23:24:11.353 回答
1

为什么不通过 ExternalInterface 调用包装器中的 javascript 函数来返回 location.hostname 的值?

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

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

包装器中的javascript:

<script type="text/javascript">
    function getHostName()
    {
        return location.hostname;
    }
</script>
于 2009-04-08T15:37:09.533 回答
1

您可以使用 BrowserManager 获取有关 url 的信息。

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

于 2009-04-13T01:43:58.407 回答
1

No need to do all these hard work. Just use URLUtil provided by Flex framework itself ;)

于 2010-11-01T11:14:37.867 回答