-
Notifications
You must be signed in to change notification settings - Fork 6
Server communication
There are multiple ways to communicate with a server.
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);
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.
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");
You have to bind a Javascript-function in Flambe to be called by external code.
System.external.bind("setFlambeParameter", function(type:String, value:Dynamic)
{
trace('set parameter: $type=$value');
});
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"));
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.
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!';
}
}
// 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
Documentation guide for Flambe - Targeted to version 4.0+
Flambe | Installation | Demo projects | Showcase | API Reference | Forum
Flambe is MIT licensed and available for free. Feel free to contribute!
- Home / Installation
- Entity / Components
- Core
- Assets
- Publish your game
- Other
- Editors
- Plugins, tools, extensions
- Help
- More Flambe