Today, I spent few hours to play with Facebook Javascript client library. While I was playing with it, I started to build a little library to fit my needs for a game project.
I built something compact, there’s only 2 classes at the moment (client class and its event class). Sobriety !
It uses a JavaScript proxy like FaceBookBridge library from ZeroFractal, but the approach is a bit different. That’s a matter a lot of design and code perspective. I wanted simplicity and visibility, like remoting API.
Let me show you !
You have to make direct method calls on the client (as you would with JavaScript library). It returns a responder instance on each call:
var responder : EventDispatcher = client.Friends.get() as EventDispatcher;
To get some request results, you can suscribe listeners (as many as you want) to any responder:
responder.addEventListener(FacebookClientEvent.ON_RESULT, onFriends); responder.addEventListener(FacebookClientEvent.ON_FAULT, onError);
And guess what, results are encapsulated in events passed as arguments to your callback methods:
public function onFriends( e : FacebookClientEvent ) : void { trace ( e.methodName + " call: " + e.result.length ); }
Clean and easy as I like it.
Last but not least, you got some errors reporting. I had hard time on this part, coz the JS implementation was acting weird. I’m not even sure at this time to have understood the whole error process. I must finalize this part, few errors aren’t caught at this time but every transaction is logged in firebug with Debug.dump JS calls.
public function onError( e : FacebookClientEvent = null ) : void { trace ( "onGetInfoError(" + "method:" + e.methodName + ", error_code:" + e.result.error_code + ", error_msg:" + e.result.error_msg + ")" ); }
Here’s a short demo to end the teasing around this project.
In this example, client retrieves all the name and profile photos urls of a user friends list through 2 API calls.
package { import com.bourre.facebookflashapi.FacebookClient; import com.bourre.facebookflashapi.FacebookClientEvent; import flash.display.Sprite; import flash.events.EventDispatcher; public class FacebookClientTest extends Sprite { protected var client : FacebookClient; public function FacebookClientTest() { client = new FacebookClient(); var responder : EventDispatcher = (client.Friends.get() as EventDispatcher); responder.addEventListener(FacebookClientEvent.ON_RESULT, onFriends); responder.addEventListener(FacebookClientEvent.ON_FAULT, onError); } public function onError( e : FacebookClientEvent = null ) : void { trace ( "onGetInfoError(" + "method:" + e.methodName + ", error_code:" + e.result.error_code + ", error_msg:" + e.result.error_msg + ")" ); } public function onFriends( e : FacebookClientEvent ) : void { trace ( e.methodName + " call" ); ( client.users.getInfo( {uids:e.result, fields:["name", "pic_big"]} ) as EventDispatcher).addEventListener(FacebookClientEvent.ON_RESULT, onGetInfo); } public function onGetInfo( e : FacebookClientEvent ) : void { for each ( var o : Object in e.result ) trace ( "Nom:" + o.name + ", Image:" + o.pic_big ); } } }
I will release the source code under an open source licence when it’ll be a bit more polished for people who want to try it, and maybe I’ll make a video tutorial, who knows !
Share on Twitter
un Domingo con as3 con @francisbourre: Facebook – Flash Client Library | http://tinyurl.com/6vcejm
Seems great !!!
Any chance to see it in a near futur ? :)
Regards
sounds good Francis! let me know if you found some time to make this ressource and/or video available :) … et bon courage pour ton dem’ si ce n’ est deja fait!
biz
@florent and elimak « Something’s coming, don’t know when, but it’s soon… Will it be? Yes, it will.
Maybe just by holding still, It’ll be there! »
I’m done with my house moving shit, so I should plan some dedicated time soonto polish the whole thing.
Is the lib ready for public release?