Community MX publishes a web service that will display information about the latest content available from the site. This service can be used by anyone who knows how to consume a web service. Content of Community MX is copyright © 2003 by Community MX and cannot be redistributed, however you may use the web service on your own site to show a list of what is available on CMX. This article will be in two parts. The first part will show a simple retrieval of content using Flash Remoting.
A Web service is a service that is created to communicate, using standard XML formats, to any application that knows how to talk to it. The Web service will have a public interface written in Web Service Description Language (WSDL). The interface describes what the Web service does and how to communicate with it. The actual communication is done with XML, typically using the SOAP protocol. Because XML is basically just text, an application that can read text can read and interpret a Web service. Because of this, Web services can be created by one type of server and utilized by anyone who can read the XML.
Flash MX has made working with Web services even easier with Flash Remoting. A Flash Remoting developer can call a remote web service by allowing the ColdFusion or ASP.NET server to automatically create a proxy to the web service. To use Flash Remoting, you will need the Flash Remoting components installed into Flash MX. You can obtain these at http://www.macromedia.com/software/flashremoting/downloads/components/. If you have not worked with Flash Remoting before, check out the introductory article on Flash Remoting at Community MX.
Flash MX Pro 2004 contains built-in SOAP translation for web services, but there is a huge amount of overhead when using SOAP from a client. A typical SOAP call and response is 4 times larger or more that a typical Flash Remoting call and response. For that reason, it is better to allow the server to parse the huge SOAP packets, and just return what you need to the client in a small AMF (Flash Remoting) binary packet. Also, web services in Flash Pro will only work if they originate on your own domain, or have a policy file set up with the service provider. Using Flash Remoting, you do not have to worry about the policy file, as Flash Remoting can work with web services on any domain.
The Community MX web service utilizes a standard .wsdl file that can be consumed by any technology that can consume a web service. The web service is located at http://www.communitymx.com/services/cmxfeed.wsdl. The web service exposes 4 methods:
*not implemented yet
The getContent method simply returns the latest content to your service without any kind of filtering, or filtering by contentType only. It is essentially a dump of the the latest content descriptions from the site. The next section will show how to consume the service using Flash MX.
// Include the required classes for Flash Remoting
#include "NetServices.as"
// Set up variable for the Flash Remoting URL
var myURL = "http://localhost/flashservices/gateway";
// Set up variable for the Community MX wsdl descriptor file
var servicePath = "http://www.communitymx.com/services/cmxfeed.wsdl";
// Connection hasn't been initialized; create connection and service objects
if (initialized == null) {
initialized = true;
NetServices.setDefaultGatewayURL(myURL);
var myConnection_conn = NetServices.createGatewayConnection();
var myService = myConnection_conn.getService(servicePath);
}
The location of the Community MX .wsdl file is at http://www.communitymx.com/services/cmxfeed.wsdl. As you can see, this is set in the code. The path to the .wsdl file becomes our service path in Flash. This will cause the ColdFusion server (or ASP.NET server) to generate a proxy for the web service on the server.
// Define the custom SimpleResult class to display the results
function SimpleResult() {
}
// Set up onResult() and onStatus() handlers as methods of SimpleResult class
SimpleResult.prototype.onResult = function (myResults) {
var temp = "";
var theLength = myResults.length;
for (var i=0; i < theLength; i++) {
temp += makeLink(myResults[i].TITLE,myResults[i].URL) + "<br>";
temp += "Author: " + myResults[i].AUTHOR + "<br>";
temp += "Category: " + myResults[i].CATEGORY + "<br>";
temp += "<br>";
}
content_txt.htmlText = temp;
};
SimpleResult.prototype.onStatus = function (myError) {
content_txt.text = myError.description;
};
_global.System.onStatus = SimpleResult.prototype.onStatus;
The onResult method of the SimpleResult object will handle all results from the call to the web service. It makes use of a utility function, makeLink(), to create a link from the TITLE field and the URL field. We are also using the AUTHOR field and the CATEGORY field in the results. You are also free to use the other fields that are not being used here.
We are also creating an onStatus method to handle any errors from the remote call, and also creating a handler for the _global.System.onStatus method, which usually indicates a failure to communicate.
// Make a clickable link out of some text, given the text and a location
function makeLink(theText,theLink) {
var temp = '<font color="#00cc00"><a href="';
temp += unescape(theLink);
temp += '" target="_blank">' + theText + '</a></font>';
return temp;
}
content_txt.text = "...working";
// Call the service on load
myService.getContent(new SimpleResult(), "all");
The results can be seen here.
More information about Flash Remoting can be found at http://www.flash-remoting.com/ and at Macromedia's site. The next part of this series will show how to use the searchContent method, and also how to tie some of the new Flash MX 2004 components to Flash Remoting.