Reformulating the previous example to take
advantage of the binding feature, you must rewrite the server script as follows. This time in the
compilation command, it is necessary to use the - main switch to be sure that the binding occurs. The
Connection.bind() static method requires a string that acts as a binding identifier and an
object instance.
// content of the file Server.hx
import haxe.remoting.Connection;
class Server
{
public static function main()
{
Connection.bind(???calc???, new Calc());
}
}
class Calc
{
public function new() {}
public function sum(x : Int, y : Int) : Int
{
return x + y;
}
}
On the client side, the connection must now be created using the binding name calc or else the class
instance won ??™ t be accessible. The try/catch block has been removed to simplify the readability.
// content of the file Client.hx
class Client
{
public static function main()
{
var calc = haxe.remoting.Connection.jsConnect(???calc???);
trace(calc.sum.call([5,4]));
}
}
Part II: Server Side, JavaScript, and Flash: Oh My!
432
JavaScript - to - Flash Connection
Reversing the Flash - to - JavaScript example using Flash as server and JavaScript as client does not
present any particular inconvenience; the only thing to take care of is the timing of the communication.
Pages:
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811