Using WebServices in FMS

This one is a quick beginner tutorial on using WebServices in Flash Media Server. Here you will learn how to retrieve web-service data from server side and pass it to all users connected to application. As an example I’m going to retrieve weather forecast.

Let’s start from creating a new application(folder) on server and
call it “webservice-tutorial” or whatever you like.
Create main.asc file and copy the following code.
// Load WebService component.
load("webservices/WebServices.asc");

application.onAppStart = function()
{
trace ("Application Started");
// Load WebService.
this.loadWS();
}

application.onConnect = function(client)
{
// Handle client functionality...
application.acceptConnection(client);
}

application.loadWS = function()
{
// Define WebService.
weatherService = new WebService("http://www.webservicex.net/WeatherForecast.asmx?WSDL");

weatherService.onLoad = function()
{
trace ("weatherService loaded...");
// Invoke getWeatherByName method and set interval
// to invoke it every 10 seconds for example.
application.getWeatherByName("New York");
setInterval(application.getWeatherByName, 10000, "New York");
}

weatherService.onFault = function(fault)
{
// Handle error here.
trace (fault.faultstring);
}
}

application.getWeatherByName = function(location)
{
// Invoke web-service method passing it the location.
weatherByName = weatherService.GetWeatherByPlaceName(location);

weatherByName.onResult = function(result)
{
var newObj = new Array();

// Loop throught result and push needed fields to array.
for ( var i = 0; i < result.Details.length; i++ )
{
newObj.push({day:result.Details[i].Day, min:result.Details[i].MinTemperatureC, max:result.Details[i].MaxTemperatureC});
}

var c = application.clients;

if ( c.length )
{
// Loop throught clients and pass weather data.
for ( var j = 0; j < c.length; j++ )
{
c[j].call("setWeather", null, newObj);
}
}

}

weatherByName.onFault = function(fault)
{
// Handle error here.
trace(fault.faultstring);
}
}Everything should be clear if you have followed the comments. On Application startup we call loadWS function which loads web-service and invokes getWeatherByName function which in turn gets weather forecast, loops throught connected clients and calls a method on client side passing data.
Next step, is to create new Flex Project
Copy the following code in Main.mxml file
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="init()">

<mx:Script>
<![CDATA[

private var nc:NetConnection;

[Bindable]
private var weatherData:Array;

private function init():void
{
nc = new NetConnection();
nc.addEventListener(NetStatusEvent.NET_STATUS, onStatus);
nc.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
nc.objectEncoding = ObjectEncoding.AMF0;
nc.client = CustomClient; // Just an empty class here.
nc.client.setWeather = function(result:Array):void
{
weatherData = result;
}
nc.connect("rtmp://localhost/webservice-tutorial");
}

private function onStatus(event:NetStatusEvent):void
{
trace ( event.info.code );
}

private function onSecurityError(event:SecurityErrorEvent):void
{
trace ( event );
}

]]>
</mx:Script>

<mx:DataGrid dataProvider="{weatherData}" width="100%" height="100%">
<mx:columns>
<mx:DataGridColumn dataField="day" headerText="Date" />
<mx:DataGridColumn dataField="min" headerText="Min Temperature C" />
<mx:DataGridColumn dataField="max" headerText="Max Temperature C" />
</mx:columns>
</mx:DataGrid>

</mx:Application>So, what happens here? On creationComplete we create new NetConnection, add status and security listeners, then we define a setWeather function which FMS would invoke. In this function we assign the result to weatherData variable which is binded to DataGrid component that whould display it.
That’s it! By now you might have a question - why would I do that on FMS, when I can do the same from client part? Well, I agree that weather example isn’t very practical, but imagine that you have a sms voting, for example, in some chat app or in whatever other one, and you want to display results every 5 seconds. So what would happen if you have 200 users and they all request a result from web-service every 5 seconds? Crash, or server slowdown maybe? Instead you can request it just once every 5 seconds from FMS and then spread the result to users.

The above image illustrates the benefits of using web-services from Flash Media Server rather than directly from client side.
原文地址:http://xinsync.xju.edu.cn/index.php/archives/1865

posted @ 2009-05-17 12:28  Andy  阅读(447)  评论(0编辑  收藏  举报