Skip to content

Server communication

Mark Knol edited this page May 22, 2014 · 7 revisions

There are multiple ways to communicate with a server.

Sending to server using Http class

This example sends a Http request to "server.php" with some variables. This works in all target platforms. Http is a Haxe class, this is not Flambe specific code.

var urlLoader:Http = new Http("server.php");

// add parameters
urlLoader.addParameter("myVar", "myValue");
urlLoader.addParameter("userName", "Mark");

// callbacks
urlLoader.onError = function (msg) trace('Http error; message=$msg');
urlLoader.onStatus = function(status) trace('Http onStatus; status=$status');
urlLoader.onData = function(data)
{
   trace('Sending data completed! data=$data');
}

// sends to data using GET
urlLoader.request();

// sends to data using POST
urlLoader.request(true);

Communication with HTML page

With HTML page I mean to Javascript window on the html-page. This is Flambe specific code, it uses ExternalSystem that can be accessed by System.

Communication from Flambe

Flambe:

Call an external function on Javascript window with the given parameters, and returns the result. Works in Javascript and Flash build (using ExternalInterface).

var url = System.external.call("location.href.toString");

Communication to Flambe

You have to bind a Javascript-function in Flambe to be called by external code.

Flambe:
System.external.bind("setFlambeParameter", function(type:String, value:Dynamic) 
{
	trace('set parameter: $type=$value');
});
Javascript:

The binding is on the on Javascript window, so you have to do your call on that.

// send some values
window.setFlambeParameter("myValue", "myValue");
window.setFlambeParameter("myNumber", 123);
window.setFlambeParameter("myObject", {userName:"Mark", age:29});
// send parameter from url (when url = http://localhost:7000/?id=123)
window.setFlambeParameter("id", swfObject.getQueryParam("id")); 

Creating server and client in Haxe?

Haxe can be also used to write serverside code. If you plan to use this, you could give Haxe Remoting a try. Haxe remoting provides a communication between a client (Flash or JavaScript) and a Server (PHP, Java, Neko). Note: the flambe serve command creates a node-js server, that does not run PHP. You have to install/use Apache for this, and do requests to that server.

Haxe PHP:

This needs to be a separate haxe project with PHP as target. Let's say you create a new project that compiles into a "api" folder of your game. If you navigate to the page (http://localhost:80/myGame/api/), you should see "server works".

class Server 
{
	static function main() 
	{
		var context = new Context();
		context.addObject("Server", new Server());
		var handledRequest = HttpConnection.handleRequest(context);
		if (!handledRequest) trace("Server works!");
	}
	
	// This function is magically available if it's public
	// If you return a value, it will be the parameter in the client.
	public function saveUser(name:String)
	{
		return 'User $name is saved!';
	}
}
In Flambe:
// Point to PHP server
connection = HttpAsyncConnection.urlConnect("http://localhost:80/myGame/api/");

var userName = "Mark";
// Call MyServer.saveUser function with provided username.
// Note; You will likely not get auto-completion on MyServer nor saveUser
connection.MyServer.saveUser.call([userName], function(result)
{
	trace(result);
});

More about Haxe Remoting here

Clone this wiki locally