简体   繁体   English

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

[英]How do I get the server endpoint in a running flex application?

I need a way of getting the active server address, port, and context during runtime from my flex application. 我需要一种在运行时从我的flex应用程序获取活动服务器地址,端口和上下文的方法。 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. 由于我们在构建过程中使用ant,因此在构建属性文件中动态指定服务器连接信息,并在services-config中使用{server.name},{server.port}和{context.root}占位符。 .xml文件而不是实际值。

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

I have found that I can at least get the context root by adding the following to our main application MXML file: 我发现通过在主应用程序MXML文件中添加以下内容,我至少可以获取上下文根目录:

<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. 但是,我仍然需要一些获取服务器地址和端口的方法,如果我通过给出-context-root = http://myserver.com:8080/mycontext指定整个地址,那么flex应用程序会尝试连接到http :// localhost / http://myserver.com:8080 / mycontext / messagebroker / amf ,这当然是完全错误的。 What is the proper way to specify the context root and server URL, and how can I retrieve them from our application? 指定上下文根和服务器URL的正确方法是什么,以及如何从应用程序中检索它们?

We use an Application subclass that offers the following methods : 我们使用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 
 }

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). 这个通用应用程序支持channelEndPointContextchannelEndPointPathInfolocalServerRootURI性质(通常为“mycontext”和“/ messagebroker / AMF /”在你的例子中,当应用程序通过Flex Builder中执行所使用的本地服务器根,在这种情况下,它有一个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). 然后使用localServerRootURI属性或使用应用程序url来执行完整端点URI的确定,因为我们的服务由为应用程序的SWF提供服务的同一服务器公开(据我所知,也是如此)。

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. 从这里开始,还可以从应用程序URL自动确定channelEndPointContext ,而不是硬编码,如本例所示。

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();

I've used FlashVars to pass urls in before with success. 我已经使用FlashVars在成功之前传递了URL。 In your template html: 在你的模板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: 然后在flex中:

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

The nice thing is you can really pass in whatever you like from the server this way. 好的一点是,您可以通过这种方式从服务器传递任何您喜欢的内容。

No need to do all these hard work. 不需要做所有这些艰苦的工作。 Just use URLUtil provided by Flex framework itself ;) 只需使用Flex框架本身提供的URLUtil;)

Why not call a javascript function in the wrapper via ExternalInterface to return the value of location.hostname? 为什么不通过ExternalInterface在包装器中调用javascript函数来返回location.hostname的值?

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

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

javascript in wrapper: 包装器中的javascript:

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

You can use the BrowserManager to get the information about the url. 您可以使用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 另见http://livedocs.adobe.com/flex/3/html/deep_linking_7.html#251252

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM