 |
|
Macromedia Flash Article |
 |
 |
 |
 |
|
Web services, in Ben Forta's words, "allow
remote execution—a host can make a request,
have it executed remotely, and receive results."
Request in, data out—with all calculations
and look-ups done behind the scenes on the server
hosting the web service. In our case, we'd like
to specify a path to the SVG file and have the
web service pass back a) an array of the contents
of each path's d attribute, nicely
formatted, or even b) an array of shape objects
that contain only quadratic bezier data which
could be used directly by the Macromedia Flash
drawing API.
|
| |
| |
| With
ColdFusion MX, web services can be written in either
CFML (good for developers familiar with ColdFusion
or other similar server-side scripts), or in Server-Side
ActionScript (SSAS) (good for Macromedia Flash developers
familiar with ActionScript). |
| |
Web
services based on ColdFusion MX components
Because I'm a believer in starting small and building
up, I think the best way to develop a service with
Cold Fusion is to start with a CFM file (a standard
Cold Fusion page) which accepts whatever input you
wish to pass to the service, does a little manipulation,
and then does a <cfoutput>
or <cfdump>
of the results. When that appears to be working,
you can add more code to the CFM page until it dumps
out to the browser whatever content it is that you
wish to have returned from the service (or some
string equivalent thereof). Once you have a working
input-in, data-out CFM page, you can turn all of
the content into a function (or several functions,
if your service will perform more than one function),
and wrap that inside a <cfcomponent>
tag. Here is the code I started with initially in
my CFM file. It reads in a file, parses it to an
XML object (svgObject),
searches for all path tags in that object, and puts
them all into an array (arPaths).
Then it displays the number of paths it found, to
confirm that it's working as I expect: |
| |
<cfset
svgFile = ExpandPath("candle.svg")>
<cffile action="READ" file="#svgFile#"
variable="svgContents">
<cfset svgObject = XmlParse(svgContents)>
<!-- Find all path tags in the SVG
file, no matter what depth -->
<cfset arPaths = XmlSearch(svgObject,
"//path")>
<!-- Display number of paths; for
debugging original .cfm -->
<cfoutput>Number of paths found:
#ArrayLen(arPaths)#</cfoutput>
(etc) |
| |
Once
that part worked, I went on to add the code to loop
through the array and do the operations I needed
to do: scan for fill and stroke attributes and convert
them to CF struct's (which Macromedia Flash MX can
read as ActionScript objects), and convert the d
attribute to a Macromedia Flash-readable array of
drawCommands.
All of that is displayed in the browser window with
<cfoutput>
and <cfdump>
tags, as may be seen in the svgToArrayTest.cfm
file included in the downloadable sample files from
the first page of this
tutorial. |
| |
| Using
the working CFM file, I removed the output tags
(changing the final cfdump to a cfreturn instead),
wrapped the content inside a function tag, and put
that inside a cfcomponent tag, to define the function
as part of a CFC (ColdFusion Component). The final
CFC file is included in the sample file folder as
svgToArray.cfc. |
| |
Once
the function has been defined in a CFC it can be
called as a web service with the steps outlined
below. Because ColdFusion MX can parse XML, it did
the parsing and array creation in half the time
it took Macromedia Flash alone (using people.svg
as a test file). The CFC file replaces the functions
loadingDone,
findPathNode,
defineShape and
drawSVG in the
original Macromedia Flash-only drawsvg.as
file. |
| |
| To use
this service from a Macromedia Flash movie, the
following things need to be done in the FLA: |
| |
|
· |
establish
a connection to the Macromedia Flash Remoting
gateway |
|
· |
create
an object which holds parameters to be passed
to the service |
|
· |
create
an object with methods to deal with data (or
errors) passed back from the service |
|
· |
define
the WSDL for the service |
|
· |
tell
the gateway connection object where the service
is |
|
· |
call
the service, specifying the function to execute
|
|
| |
| Following
is the code that does this (from the file
drawsvg_s1.as): |
| |
var gw =
NetServices.createGatewayConnection("http://localhost:8500/flashservices/gateway/");
// svgtoarray service needs filename (or path to
file) passed as a string
// so set up params object to send it as a property
var params = new Object();
params.filename = "people.svg";
//create an object to catch the response from the
xml-parsing service
var drawInfo = new Object();
drawInfo.onResult = function(pathData) {
trace("number of shapes "
+ pathData.length);
findTime("xml parsed to array (service
1)");
for (var i=0; i<pathData.length;
i++) {
shapes.push(createShapeDef(pathData[i].drawCommands,
null, pathData[i].fill, pathData[i].stroke));
}
drawShapes();
}
//this gets called if an error occurs.
drawInfo.onStatus = function(error)
{
trace("drawInfo Error
: " + error.description);
}
//the path to the web service's WSDL file
var webServiceWSDL = "http://localhost:8500/com/ht/svgToArray.cfc?wsdl";
// returned object is drawInfo, defined above
var getDrawCommands = gw.getService(webServiceWSDL,
drawInfo);
...
getDrawCommands.svgtoarray(params); |
| |
| Next,
let's examine how to use web services that are based
on Server-Side Actionscript. |
| |
| |
| |
|
|
|
|
|
|