Java and BlazeDS by Example

The interaction between Java and Flash
(part 3 - BlazeDS)

BlazeDS - Introduction

Let's see how to make a simple example in order to get in communication a java server side(with blazeDS) and action script 3 client side inside Flash CS3 (No Flex!!)

Server Side:

Download the BlazeDS binary distribuition

The BlazeDS binary distribuition is a war, so it is a web application.
Decompress it in a directory.
Make a new java webproject and update the web-inf directory in the empty project with the files contained in decompressed war into web-inf.

Try to deploy it into the server and be sure that it is deployed correctly.

I'm take the example step by reading - RPC service example

Now in the project make the class EchoService like the example in the link:

package remoting; 

public class EchoService 
    public String echo(String text) 
    { return "Server says: I received '" + text + "' from you"; } 

If you build the project, then you see that inside WEB-INF/classes/remoting there is the EchoService.class file!

Now you have to define a destination and reference one or more channels for data transport.
So make EchoService.class the remoting destination by editing WEB-INF/flex/remoting-config.xml and adding the following code:

<destination id="echoServiceDestination" channels="my-amf">

With the latest xml you reference my-amf channel, but it isn't setup yet.
So add the following code to WEB-INF/flex/services-config.xml

<channel-definition id="my-amf" class="mx.messaging.channels.AMFChannel"> 
<endpoint url="http://{}:{server.port}/{context.root}/messagebroker/amf" class="flex.messaging.endpoints.AMFEndpoint"/> 

This code specifes also that client uses a non-pooling AMFChannel.
Note that you must replace, server.port, context.root with the correct values

Client Side:

Make An ActionScript file and inside the actions put this source code:


//replace with you server ip or servername
var url:String = "";
var nc:NetConnection;
var responder:Responder;                   
nc = new NetConnection();
nc.connect(url);"echoServiceDestination.echo", new Responder( onResult, onError ), "TEST!");

function onResult( text:String ):void 
     trace("Result: " + text);
function onError( error:* ):void 
        trace("CALL ERROR:");               
        for(var i:String in error)
        trace(i + " :: " + error[i]);


BlazeDS seems to be a powerful solution, this example show only a simple RPC example.


KIDJIM said...
This comment has been removed by the author.
Gaian Helmers said...

This is a really good little tutorial on using BlazeDS without Flex.

I'm trying to learn really well so I have a question:

I see that:"echoServiceDestination.echo", new Responder( onResult, onError ), "TEST!");

Is what is sharing info between the server and the flash application. But other than the .echo function call, I dont know what the other parameters are and why you're using them.

I'm trying to make a live action online game in flash so I need to REALLY understand the message handler and how to use it so I can best utilize its abilities to update the game state.

Thanks for your time.