haXe

Flash/Javascript Remoting Sample

Here's a sample on how to communicate between Flash and Javascript using haXe Remoting. Here's first the haXe/Javascript code JsMain.hx :

class JsMain {
  static function foo(x,y) { return x + y; }

  static function main() {
    var flash = haxe.remoting.Connection.flashConnect("myFlashObject");
    trace(flash.FlashMain.foo.call([1,2]));
  }
}

This code will connect to the Flash object named myFlashObject, call the method FlashMain.foo(1,2) and display the result.

The FlashMain.hx class is the following :

class FlashMain {
  static function foo(x,y) { return x + y; }

  static function main() {
    var js = haxe.remoting.Connection.jsConnect();
    trace(js.JsMain.foo.call([1,2]));
  }
}

The FlashMain code is the same as JSMain, except that it connects to JavaScript by using haXe Remoting.

You can build the project by using the following single HXML file. This will produce both a remoting.js and a remoting.swf files :

JsMain
-js remoting.js

--next

-main FlashMain
-swf remoting.swf

Now, you can display the traces in Javascript and Flash by using the following HTML. Please note that :

  • this example will only work with Flash Player 8 or more
  • the Flash HTML needs some additional properties in OBJECT and EMBED tags to allow JS access
  • the JsMain.main method is called 200 milliseconds after the onLoad event is fired in order to enable the Flash object to initialize itself correctly.

<html>
  <head>
    <script type="text/javascript" src="remoting.js"></script>
  </head>
  <body bgcolor="#eeeeee" onLoad="setTimeout('JsMain.main()',200)">
    <div id="haxe:trace"></div>
    <object
      classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"
      codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0"
      width="400"
      height="300"
      id="myFlashObject"
    >
      <param name="movie" value="remoting.swf"/>
      <param name="allowScriptAccess" value="always" />
      <param name="quality" value="high" />
      <param name="salign" value="lt" />
      <param name="scale" value="noscale" />
      <param name="menu" value="false" />
      <param name="bgcolor" value="#ffffff"/>
      <embed
        src="remoting.swf"
        quality="high"
        salign="lt"
        width="400"
        height="300"
        align="middle"
        scale="noscale"
        menu="false"
        bgcolor="#ffffff"
        name="myFlashObject"
        swLiveConnect="true"
        allowScriptAccess="always"
        type="application/x-shockwave-flash"
        pluginspage="http://www.macromedia.com/go/getflashplayer"
      />
    </object>
  </body>
</html>

This should display the values that are returned by the other side of the connection. From here, you should be able to handle Flash/JS communications transparently.

Troubleshooting

In case of problem, check the following :

  • the local security of your SWF should be enabled from the Flash security manager
  • OR you should access the sample from a local webserver

version #1160, modified 2008-05-03 14:16:02 by ponticelli