如何在正在运行的Flex应用程序中获取服务器端点?

Nik*_*man 5 apache-flex endpoints

我需要一种在运行时从我的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>
Run Code Online (Sandbox Code Playgroud)

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

Gui*_* G. 3

我们使用提供以下方法的 Application 子类:

 /**
  * 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 
 }
Run Code Online (Sandbox Code Playgroud)

此通用应用程序支持channelEndPointContextchannelEndPointPathInfolocalServerRootURI属性(在您的示例中通常为“mycontext”和“/messagebroker/amf/”,当通过 Flex Builder 执行应用程序时使用本地服务器根目录,在这种情况下它具有 URL file://)。然后,使用该属性或使用应用程序
来确定完整的端点 URI ,因为我们的服务是由为应用程序的 SWF 提供服务的同一台服务器公开的(据我了解您的情况也是如此)。localServerRootURIurl

所以,在你的例子中,人们会写:

 <SuperApplication ...> <!-- SuperApplication is the enhanced Application subclass -->
    <mx:HTTPService id="myHTTPService" url="{this.channelEndPointURI}"/>
 </SuperApplication>
Run Code Online (Sandbox Code Playgroud)

从这里开始,我们还可以channelEndPointContext从应用程序 URL 自动确定,而不是像本示例中所示对其进行硬编码。