I am a Flex newbie and I'm developing a Flex application that needs to talk to an AMF server in order to authenticate the user.
Where should I store the server URL? I don't want to hardcode it in the source 开发者_如何学Ccode, but the URL should be fixed at compile time, because when I compile the app it should be tied to a specific server.
If you need the URL at compile time but you don't want to "hardcode" it, the best thing to do is to store it in inline constants. They work as #IFDEF
or #define
on C/C++ and you can easy change the URL without changing the application (just re-compiling it).
You have to add something like this into your compiler options:
-define+=APP::amfServerUrl,'http://localhost/some/path/'
And then, on your app code:
private static const AMF_SERVER_URL:String = APP::amfServerUrl;
If it's not AIR, you could give your flash the server URL via FlashVars
HTML
<script type="text/javascript">
var flashvars = {gateway:'http://myserver/amfphp'
};
var params = {
menu: "false",
scale: "showAll",
allowFullscreen: "true",
allowScriptAccess: "always",
quality:"best",
bgcolor: "#FFFFFF"
};
var attributes = {
id:"main", name:"main"
};
swfobject.embedSWF("main.swf", "altContent", "100%", "100%", "10.0.0", "expressInstall.swf", flashvars, params, attributes);
</script>
Then to retrieve this parameter, use:
AS3
var gatewayUrl:String = stage.loaderInfo.parameters["gateway"];
I use an external config.xml file that resides on the server. My swf/wrapper are in the root, and the config is in a folder there named 'config'. Here is code from my current project that uses this config: .
override protected function doStart() : void {
var xmlLoader : URLLoader = new URLLoader();
xmlLoader.addEventListener(Event.COMPLETE, result, false, EventPriority.DEFAULT_HANDLER, true);
xmlLoader.addEventListener(IOErrorEvent.IO_ERROR, fault, false, EventPriority.DEFAULT_HANDLER, true);
xmlLoader.load(new URLRequest("config/config.xml"));
}
public function fault(info : *) : void {
error("Failed to load config file.");
trace("Error Loading Configuration");
}
public function result(data : Event) : void {
var xml : XML = new XML(data.target.data);
var settings : Object = XMLMapperUtil.mapAppSettingsModelFromXML(xml);
var appSettingsModel : AppSettingsModel = AppSettingsModel.getInstance();
ExObjectUtil.copyObjectProperties(appSettingsModel, settings);
var secureString : String = (appSettingsModel.serverProtocol == 'https') ? "secure" : "";
var firstURLSegment : String =
appSettingsModel.serverProtocol
+ "://"
+ appSettingsModel.serverIP
+ ":"
+ appSettingsModel.serverPort
+ "/FlexClient/messagebroker/";
var remotingURL : String = firstURLSegment + "amf" + secureString;
var messagingURL : String = firstURLSegment + secureString + "streamingamf";
var remotingChannelSet : ChannelSet = new ChannelSet();
var messagingChannelSet : ChannelSet = new ChannelSet();
if (appSettingsModel.serverProtocol == 'https') {
remotingChannelSet.addChannel(new SecureAMFChannel("my-secure-amf", remotingURL));
messagingChannelSet.addChannel(new SecureStreamingAMFChannel("my-secure-amf-stream", messagingURL));
}
else {
remotingChannelSet.addChannel(new AMFChannel("my-amf", remotingURL));
messagingChannelSet.addChannel(new StreamingAMFChannel("my-streaming-amf", messagingURL));
}
appSettingsModel.remoteObject_ChannelSet = remotingChannelSet;
appSettingsModel.messaging_ChannelSet = messagingChannelSet;
complete();
}
My result function uses SpiceLab's XMLObjectMapper, a singleton, and a special property copying function to get the settings from the XML into a global object that I can use (appSettingsModel). All you really need to do is get that XML, parse it all out, and create some channels with the results. Oh, here is my XML file:
<AppSettingsModelXML
serverIP = "123.123.123.123"
serverPort = "8080"
serverProtocol = "http"/>
精彩评论