Remote connecting. AMF, JSON

Hello everyone. Since it’'s my first post at first I would like to introduce myself. I am From Buenos Aires, Argentina and I run an small developement company since the year 2000. Our core business are focused on game developement. On that days we started using Macromedia Director / Shockwave and we evolved to flash. Now we feel that the Natural evolution is Haxe/Open FL.
We started porting our current developement and everything flows until the moment we need to interact with Server Side. In flash we are very comfortable with 2 technologies. AMFPHP to interact with Database and persistent data, and OpenFire ( XMPP ) for realtime Communication (if required).

We realized that AMF is only compatible with flash and since the idea of migrate to this technology is to add multiplatform benefit, we got stucked. A first approach that comes to my mind is keep using AMF for flash and if compiled for HTML5 / Other use a conditional to Use a “JSON” layer in both client and server to exchange data.

Basically I’m posting this to know if someone has a better approach to do this, or if they face a similar project share the experience with the potential problems encountered.

That’s all for now. From now you would probably read posts from me often. And I would be happy to help in anything I could ( probably at first I would be able to answer questions more related to general problem solving & programming techniques)

Thanks in advance and CU soon.

Luis

Partially, this may depend on how you configure your server. Haxe has built in serialization and some mechanisms for communicating with a Haxe server (such as Neko) or there is some support for AMF in the Haxe format library (https://github.com/haxefoundation/format) which might be worth giving a try?

and welcome! :slight_smile:

Thanks so much for the response. Basically I had a lot of serverSide scripts made in PHP using AMF, and need a simple and fast solution. Till now I implemented this:

Making the call:

		#if flash9
			var urit:String = 'http://backend.' + Constants.SERVER_HOST + '/index.php';
			var c:AMFConnection = AMFConnection.urlConnect(urit);
			c.slot.getUserData.call(aParams, onUserResult);
		#else		
			var urit:String = 'http://backend.' + Constants.SERVER_HOST + '/indexJS.php';
			var c:Http = new Http(urit);
			c.addParameter('class', 'slot');
			c.addParameter('method', 'getUserData');
			var toSnd:String = Json.stringify(aParams);
			c.addParameter('params', toSnd);
			c.addParameter('callback', 'onUserResult');
			this.addEventListener('loadRemote', onUserResult);
			c.onData = parseJSResponse;
			c.request(true);			
		#end

On server Side, I implemented this PHP code:

<?php

include('./Services/'.$_POST['class'].'.php');
$c = new $_POST ['class'] ();
$params = json_decode($_POST['params']);
$rs = $c->$_POST['method']($params);
$rs->callback = $_POST['callback'];
print(json_encode($rs));
?>

Back in client:

public function parseJSResponse(rs:String) {
		var result = Json.parse(rs);
		var cb = Reflect.field(this, result.callback);
		Reflect.callMethod(this, cb, [result]);
}

And thats it from this point again is the same Path than AMFPHP…seems like a thin layer. I would let you know how it behaves once I implement several functions.

HTH

Regards
Luis

An issue regarding this solution is when you use -final for publishing. There is a simple walkaround, overriding the parsJSRsponse:

	override public function parseJSResponse(rs:String) {
		var result = Json.parse(rs);
		switch (result.callback) 
		{
			case 'onUserResult': // you should add one case for each method call
				this.onUserResult(result);
			default:
				var cb = Reflect.field(this, result.callback);
				this.remoteAction = result.action;
				Reflect.callMethod(this, cb, [result]);				
		}
	}

not sure if the best solution but at least it would work.

1 Like

Let me know if you ever need anything from the standard OpenFL/Lime library here :slight_smile: