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

发布于 2024-07-17 04:48:19 字数 937 浏览 13 评论 0原文

我需要一种在运行时从 Flex 应用程序获取活动服务器地址、端口和上下文的方法。 由于我们在构建过程中使用 ant,因此服务器连接信息是在构建属性文件中动态指定的,并且 {server.name}、{server.port} 和 {context.root} 占位符在 services-config 中使用.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 的正确方法是什么,以及如何从我们的应用程序中检索它们?

I need a way of getting the active server address, port, and context during runtime from my flex application. Since we are using ant for our build process, the server connection information is dynamically specified in our build properties file, and the {server.name}, {server.port} and {context.root} placeholders are used in the services-config.xml file instead the actual values.

We have some other Java servlets running on the same machine as our blazeDS server, and I'd like some way to programmatically determine the server endpoint information so I don't need to hardcode the servlet URL's into an XML file (which is what we are presently doing).

I have 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 some way of fetching the server address and port, and if I specify the entire address by giving -context-root=http://myserver.com:8080/mycontext, then the flex application attempts to connect to http://localhost/http://myserver.com:8080/mycontext/messagebroker/amf, which is of course totally wrong. What is the proper way to specify the context root and server URL, and how can I retrieve them from our application?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

夜空下最亮的亮点 2024-07-24 04:48:19

我们使用提供以下方法的应用程序子类:

 /**
  * 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,而不是像本示例中所示对其进行硬编码。

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.

枫林﹌晚霞¤ 2024-07-24 04:48:19

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

好处是你可以通过这种方式从服务器传递你喜欢的任何内容。

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.

林空鹿饮溪 2024-07-24 04:48:19
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();
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();
刘备忘录 2024-07-24 04:48:19

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

Why not call a javascript function in the wrapper via ExternalInterface to return the value of 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>
戈亓 2024-07-24 04:48:19

您可以使用 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

You can use the BrowserManager to get the information about the 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

只是在用心讲痛 2024-07-24 04:48:19

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文