如何在正在运行的 Flex 应用程序中获取服务器端点?
我需要一种在运行时从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我们使用提供以下方法的应用程序子类:
此通用应用程序支持
channelEndPointContext
、channelEndPointPathInfo
和localServerRootURI
属性(通常为“mycontext”和“ /messagebroker/amf/”在您的示例中,通过 Flex Builder 执行应用程序时使用的本地服务器根目录,在这种情况下它具有file://
URL)。然后,使用
localServerRootURI
属性或使用应用程序url
来确定完整的端点 URI,因为我们的服务由为应用程序的 SWF 提供服务的同一台服务器公开(据我了解你的情况也是如此)。因此,在您的示例中,可以这样写:
从这里开始,我们还可以从应用程序 URL 自动确定
channelEndPointContext
,而不是像本示例中所示对其进行硬编码。We use an Application subclass that offers the following methods :
This generic application supports the
channelEndPointContext
,channelEndPointPathInfo
andlocalServerRootURI
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 afile://
URL).The determination of the complete endpoint URI is then performed using either the
localServerRootURI
property or using the applicationurl
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 :
Starting from here, one can also automatically determine the
channelEndPointContext
from the application URL instead of hardcoding it as shown in this example.我之前曾使用 FlashVars 成功传递 url。 在你的模板 html: 中
,然后在 flex: 中,
好处是你可以通过这种方式从服务器传递你喜欢的任何内容。
I've used FlashVars to pass urls in before with success. In your template html:
And then in flex:
The nice thing is you can really pass in whatever you like from the server this way.
为什么不通过ExternalInterface调用包装器中的javascript函数来返回location.hostname的值?
包装器中的 JavaScript:
Why not call a javascript function in the wrapper via ExternalInterface to return the value of location.hostname?
javascript in wrapper:
您可以使用 BrowserManager 来获取有关 url 的信息。
也可以看看
http://livedocs.adobe.com/flex/3/html/ deep_linking_7.html#251252
You can use the BrowserManager to get the information about the url.
see also
http://livedocs.adobe.com/flex/3/html/deep_linking_7.html#251252
不需要做所有这些艰苦的工作。
只需使用 Flex 框架本身提供的 URLUtil 即可;)
No need to do all these hard work.
Just use URLUtil provided by Flex framework itself ;)