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!!)


Example
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.

Note:
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">
<properties> 
<source>remoting.EchoService</source> 
</properties> 
</destination>

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.name}:{server.port}/{context.root}/messagebroker/amf" class="flex.messaging.endpoints.AMFEndpoint"/> 
<properties> 
<polling-enabled>false</polling-enabled>
</properties> 
</channel-definition>


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


Client Side:

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

import flash.net.NetConnection;
import flash.net.Responder;

//replace with you server ip or servername
var url:String = "http://89.96.213.148/blazeDS/messagebroker/amf";
var nc:NetConnection;
var responder:Responder;                   
     
nc = new NetConnection();
nc.connect(url);
nc.call("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]);
}

Conclusion:

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

2 comments:

KIDJIM said...
This comment has been removed by the author.
Unknown 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: nc.call("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.