【问题标题】:How do I get the server endpoint in a running flex application?如何在正在运行的 flex 应用程序中获取服务器端点?
【发布时间】:2023-03-05 01:01:01
【问题描述】:

我需要一种在运行时从我的 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 的正确方法是什么?如何从我们的应用程序中检索它们?

【问题讨论】:

  • 那么您使用的是 java servlet 还是只是 httpservice?我不确定我是否完全按照您的问题或您正在做什么,但是对于远程对象,您也可以在代码中完成所有这些操作,而不必使用 services-config.xml 文件或将其放入编译器选项中。你能详细说明一下吗?
  • 我们都在使用。我们有一个 HTTP 端点,并为一些额外的任务单独的 Java servlet。我们使用远程对象;所有消息处理都是通过 Cairngorm 完成的。

标签: apache-flex endpoints


【解决方案1】:

我们使用提供以下方法的 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 
 }

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

因此,在您的示例中,可以这样写:

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

从这里开始,也可以自动从应用程序 URL 中确定channelEndPointContext,而不是像本示例中所示的硬编码。

【讨论】:

    【解决方案2】:

    我之前使用 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;
    

    好消息是您可以通过这种方式真正从服务器传递您喜欢的任何内容。

    【讨论】:

      【解决方案3】:
      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();
      

      【讨论】:

        【解决方案4】:

        为什么不通过 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>
        

        【讨论】:

        • 这不是我要问的。另外,您可以通过 Application.application.url 和解析字符串轻松获得它。
        【解决方案5】:

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

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

        另见 http://livedocs.adobe.com/flex/3/html/deep_linking_7.html#251252

        【讨论】:

          【解决方案6】:

          不需要做所有这些艰苦的工作。 只需使用 Flex 框架本身提供的 URLUtil ;)

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-11-03
            • 2019-10-04
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多