On the server side, make sure you start by loading the WebServices class with the following:
load("webservices/WebServices.asc");
In this particular example, you are using remote method invocation (RMI) from the client to the server method getWeather:
Client.prototype.getWeather = function (zip){
Then you need a var of type string and array as well as the path to your service:
var aDetail=[]; var Detailing=""; var WSDLuri = "http://www.webservicex.net/WeatherForecast.asmx?WSDL";
Instantiate the WebServices class as follows, passing in the path to your service:
WeatherService = new WebService(WSDLuri);
Keep in mind that the WebServices class of Flash Media Server 2 provides you with two event handlers: onLoad and onFault for both the WSDL and the result.
So you could use the following:
WeatherService.onLoad = function(Wsdl){
trace(Wsdl);
}
// onFault is triggered if the above load fails
WeatherService.onFault = function(fault){
trace( fault.faultstring );
clientNow.call("failed", null, fault.faultstring);
}
Once your WebService object is set, you need to fire an event on it using a function provided by the web services provider. Pointing your browser to the WSDL URL, you can view the XML definition as well as other descriptions. This allows you to view all available methods and their corresponding results.
As shown in Listing 1, pointing the browser to the URL reveals which methods are available to call within the XML code definition presented by the browser and how they will return the results.
Currently, you are concerned with the GetWeatherByZipCode method:
forcast =WeatherService.GetWeatherByZipCode(zip);
Once your method is called, you need to parse the result when onResult is triggered:
forcast.onResult = function(returning) {
//convert result into an array aDetail
for (var i in returning) {
if (i == "Details") {
for (var j in returning[i]) {
switch (j) {
case "xmlNodes" :
//get rid of commas
for (var p in returning[i][j]) {
if (returning[i][j][p] != undefined) {
aDetail.push(returning[i][j][p].toString());
}
}
case "length" :
var total = returning[i][j];
}
}
}
}
var tot= aDetail.length;
//push each array slot into the string Detailing to form xml string
for (var i = 1; i<tot; ++i) {
Detailing += aDetail[i];
}
aDetail.splice(0);
//now send xml string to client
clientNow.call("forcast", null, Detailing);
}
This process is rather elementary. All you're doing is working around the current WebServices class to handle an array. You use a "for in" loop to grab each element in the xmlNodes—part of the Details result—and pass it into the aDetail array you created earlier:
for (var p in returning[i][j]) {
if (returning[i][j][p] != undefined) {
aDetail.push(returning[i][j][p].toString());
}
}
Once you have passed in all nodes, you can manipulate the array directly. Keep in mind that every web service returns results in its own way, so it is important to find a way to unfold your result. I have found that the most reliable way is to use the "for in" loop. Applying a "for in" loop showed some promise to the wealth of data returned. I then found other objects and set up "for in" loops in them, as well, until I had all the data I needed.
Note: If you handle the processing on the client side, the "for in" loops will read backwards. Therefore, it is important to reverse the array (using array.reverse()), once you have pushed the nodes into the array, to correct the order.
At this point you could use just the array to do what you want but my main objective is to get the data into its proposed XML format:
for (var i = 1; i<tot; ++i) {
Detailing += aDetail[i];
}
Push each node into the Detailing string and send to the client:
clientNow.call("forcast", null, Detailing);