Client/Server Remoting Sample
Client/Server Remoting is performed between a Flash or JavaScript client and a haXe Server. Clients are calling methods on the Server.
In this sample, we will see how to implement Client/Server remoting in async/unconnected mode. For this we will use the haxe.remoting.AsyncConnection.urlConnect contructor which is available on all platforms. Here's the Client.hx file, which can be compiled either in JavaScript or Flash :
class Client { static function display(v) { trace(v); } static function main() { var URL = "http://localhost:2000/remoting.n"; var cnx = haxe.remoting.AsyncConnection.urlConnect(URL); cnx.onError = function(err) { trace("Error : "+Std.string(err)); }; cnx.Server.foo.call([1,2],display); } }
Here's the Server code. Please note that classes can't be automaticaly accessed on the Server-Side, but need first to be made available explicitly by using addObject. This way you can manage the security by only making accessible the API you want.
class Server { function new() { } function foo(x,y) { return x + y; } static function main() { var r = new neko.net.RemotingServer(); r.addObject("Server",new Server()); if( r.handleRequest() ) return; // handle normal request neko.Lib.print("This is a remoting server !"); } }
Compile the project using the following HXML File and setup the Neko Web Server to point to the current directory. If you navigate to http://localhost:2000/remoting.n it should display the message This is a remoting server !.
-main Client -swf client.swf --next -main Server -neko remoting.n
You can simply open the SWF or the JS compiled from Client.hx to display the traces. The client will do one Server request per call. The Server can store some persistant data in the database by using SPOD.
If you experience any problem see the Flash Player Security page.
Preventing some fields access
When sharing a server object with the addObject method, all its methods will be callable, and all its fields will be accessible even if they are compiled as private. If you want to hide some fields, you can for example prefix them with a double underscore and use the setPrivatePrefix method in order to prevent access to theses fields :
class Server { static var __database : neko.db.Connection; static function main() { var r = new neko.net.RemotingServer(); r.addObject("Server",Server); r.setPrivatePrefix("__"); //... } }
This example will prevent the clients to call methods of the __database object, which could be problematic to say the least.
Arguments types are not ensured
One other thing that you need to be careful about is that there is no warranty of any kind that the arguments types will be respected when a method is called using Remoting. That means that even if you type the arguments of foo to Int, the client will still be able to use strings while calling the method. That can lead in some cases to security issues. If you have doubts, you can simply check the argument type when the function is called by using the Std.is method (see the Std class).