NetConnection and remoting in BlazeDS/LCDS

Normally, people use RemoteObject to talk to a remoting destination in BlazeDS which is a class part of Flex framework but once in a while, I get questions on whether it’s possible to do remoting with Flash’s NetConnection directly. Although using NetConnection has no immediate benefits, it’s helpful in case one does not want to use Flex framework or simply prefer using NetConnection.

Thanks to LegacyFilter in BlazeDS, this is indeed possible. Here’s an outline of what you need to do. First, create a NetConnection and connect to the URL of the endpoint you need to connect (which is defined in services-config.xml)

var nc:NetConnection = new NetConnection();
nc.connect("http://localhost:8400/team/messagebroker/amf" );

Then, do the NetConnection call with a special parameter, result/fault responders, and arguments to the remoting method.

nc.call( "remoting_AMF.echo", new Responder( resultHandler, faultHandler ), "hello");

The first parameter is important, the format is destinationId and method name seperated by a dot. In this example, we’re asking BlazeDS to direct the request to destination with id “remoting_AMF” and the “echo” method of the class that the destination exposes. LegacyFilter handles the rest. A full example is here in BlazeDS source tree.

14 thoughts on “NetConnection and remoting in BlazeDS/LCDS

  1. Once the NetConnection#call is invoked on the client side, LegacyFilter transforms that call on the server side into a RemotingMessage for RemotingService to handle.

  2. hey, thanks for the help

    I’m up and running, I can return a string, but how do I return a custom object? flex had a way of “automagically” casting custom objects (and arrays of custom objects) that were defined in java and had AS3 counterparts.

    so, a single string comes back ok, then I tried a java.util.List of strings and I got error #2173 (“unable to read object in stream”)

    I figured I’d get lists>arrays going, then try for custom objects

    tips?

  3. sorry, I put xml in there and it disappeared

    my question was on how to configure the serialization switches for a channel. I want to have java collections map to as3 arrays, and I believe if I can set that in services-config.xml, but I cant figure out exactly how. what I get from the docs is:

    channel-definition/properties/serialization/legacy-collection

    but it doesnt seem to do anything

    thanks

    thanks!

  4. ooops, sorry, wasnt cycling tomcat between changes, just reloading context. looks like I can map java Collections to As3 arrays now, which is probably going to solve my first problem, a few posts back. thanks anyway!

  5. I am in the process of converting application over to Railo from CF. The application use flash remoting provided by the CF server.

    I have followed your post and I am having problems connecting to my server object, (cfc ruining on Railo)

    I have a the following destination id set up.

    test_code.com.test

    ________________________________

    And I am running the as3 code below
    _________________________________.

    var nc:NetConnection = new NetConnection();
    nc.connect(“http://xxxxxxx/flex2gateway” );

    nc.call( “testCF.process”, new Responder( onResult, onFault ), “hello”);

    function onResult(responder:Object):void {
    trace(‘success’);

    }

    function onFault(responder:Object):void {
    var i=0;
    for (i in responder) {
    trace(responder[i]);
    //ect.
    }
    }

    But I get the following error.

    Server.Processing
    null
    [object Object]
    flex.messaging.MessageException: java.lang.NullPointerException : null
    null

    Any help would be very much appreciated,

    Thanks,
    Heath

    1. You’re getting NullPointerException on the server. I’d check the server logs to get more details on the NullPointerException.

  6. The only benefit is that if you use NetConnection, you’re sticking with Flash only APIs, whereas RemoteObject needs Flex APIs and need rpc.swc in your application. I don’t think there’s any other benefit in terms of performance etc.

  7. Hi,

    I have been trying to figure out one simple question.
    Can I do multiple calls on the same gateway?
    I mean, let say I call a method “getItemDetails” and waiting its response, but before I get the response, call another one or the same?

    We use amfphp right now, but we are seriously looking at the java environment to use in the future.

    Another big question to me is the real meaning of netConnection.connect. Does it just store the gateway url or does it really tries to call the server for the first time?
    I have seen some examples, when they always initiate a new netConnection just to call one method and get response – I don’t think it is the right approach.

    Thanks!
    Benjamin

  8. Hi Benjamin,

    I think you should be able to submit multiple NetConnection calls.

    I don’t recall whether NetConnection#connect makes an actual connect. I seem to remember that NetConnection#connect simply sets things up on the client, and then NetConnection#call makes the actual request.

  9. Is it possible to use the default flex2gateway endpoint with the NetConnection?

    We are having an issue with ROs and blaze duplicate sessions, and saw moving to NetConnection as a possible solution. However, I am unable to get the base case working to the default flex gateway and remote-object-y ways of passing the RO call location.
    EX (the below always returns a fault, with the services-config on the server only having the default setup for flex2gateway):
    private function init():void {
    connection = new NetConnection(“http://test.somewhere.org/flex2gateway/”);
    }

    private function fireRequest():void {
    connection.call(“webservices.ApplicationSubfolder.ComponentName.getFunctionName”,
    new Responder(resultHandler, faultHandler),
    ‘109023224’);
    }
    private function resultHandler(result:Object):void
    {
    ta.text += “Server responded: “+ ObjectUtil.toString(result) + “\n”;
    }
    private function faultHandler(fault:Object):void
    {
    ta.text += “Received fault: ” + ObjectUtil.toString(fault) + “\n”;
    }

Leave a comment