如何在正在运行的Flex应用程序中获取服务器端点?
我需要一种在运行时从我的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的正确方法是什么,以及如何从应用程序中检索它们? 解决方法
我们使用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 } 此通用应用程序支持channelEndPointContext,channelEndPointPathInfo和localServerRootURI属性(在您的示例中通常为“mycontext”和“/ messagebroker / amf /”,当通过Flex Builder执行应用程序时使用本地服务器根,在这种情况下它具有文件:// URL). 所以,在你的例子中,人们会写: <SuperApplication ...> <!-- SuperApplication is the enhanced Application subclass --> <mx:HTTPService id="myHTTPService" url="{this.channelEndPointURI}"/> </SuperApplication> 从这里开始,还可以从应用程序URL自动确定channelEndPointContext,而不是硬编码,如本例所示. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |