cinqetdemi.remoting: A tiny but useful Remoting library

Posted on Thursday 9 March 2006

cinqetdemi.remoting.RemotingService is a tiny layer designed to run on top of Remoting that wraps mx.remoting.Service and adds quite a few useful features. RemotingService works well with amfphp but can work with any other Remoting implementation, ColdFusion, Fluorine, OpenAMF, etc.

Notable features:

  • Timeout and retry on failure. If a packet gets lost, or there's a temporary hickup, oftentimes the solution is simply to send the call again. RemotingService does this automatically. Timeout time and number of retries are customizable
    per call.
  • Argument remembering. In addition to the standard ResultEvent and FaultEvent, callbacks receive the original arguments sent to Remoting as an array in the second parameter. This is useful in larger applications where you may call the same method lots of times and the results are out of context. For example, if you call service.getCategory(id), you'll get back that id as a parameter in your callback
  • Events. In addition to standard callbacks, RemoteService dispatches system events. Among those:
    • result, fault (like global callbacks)
    • authFault (will catch authentication failures in the upcoming AMFPHP 1.2
    • timeout, when a call has timed out
    • busy, when a call has been taking more than 750 ms, which is the the perfect time to show a loading bar
    • clear, when busy has been called and the call returns, you will want to hide the loading bar at that point
  • Pass by ref callbacks. Contrarily to PendingCall which uses two strings for callbacks, RemotingService uses two references for callbacks, which get checked by the compiler and saves you from the usual typo.

Usage example:


import mx.rpc.*;
import mx.remoting.debug.NetDebug;
import cinqetdemi.remoting.RemotingService;

service = new RemotingService('http://localhost/amfphp/gateway.php', 'MyService');

//Retrieve category 5 in ascending order
service.getCategory([5, 'ASC'], this, handleGetCategory, handleFault);

function handleGetCategory(re:ResultEvent, args:Array)
{
     NetDebug.trace(re.result);
     NetDebug.trace(args);
}

function handleFault(fe:FaultEvent, args:Array)
{
     NetDebug.trace(fe.fault);
}
 

As for the events:


service.addEventListener('timeout', this);
service.addEventListener('busy', this);
service.addEventListener('clear', this);

function timeout()
{
     trace('Man, that sucks');
}

function busy()
{
     trace('I should really show a progress bar right about now');
}

function clear()
{
     trace('I should really hide that progress bar now');
}
 

By default, it will retry 3 times and wait 5 seconds each time If you want to override this on a per-call basis, use the 5th argument when you call a method:


service.payByCreditCard(['my-credit-card-no'], null, null, null, {maxAttempts:1, timeout:20});
 

The {maxAttempts:1, timeout:20} object is conveniently available as the constant: RemotingService.NO_RETRY

If you want to use authentication, just use service.setCredentials(user, pass);

Download it here (v1.0.0).


WordPress database error: [Can't open file: 'wp_comments.MYD'. (errno: 145)]
SELECT * FROM wp_comments WHERE comment_post_ID = '190' AND comment_approved = '1' ORDER BY comment_date

No comments have been added to this post yet.

Leave a comment




Your e-mail address is never displayed. If you run into issues with SpamKarma blocking you, email me at $patrick->5etdemi(com)


RSS feed for comments on this post | TrackBack URI