AJAX - The Complete Reference

(avery) #1

524 Part III: Advanced Topics


a binary bridge, making the communication to a socket server and pipe information back
and forth to the JavaScript in the page. We note the browser isn’t the only one needing
assistance, as the socket server will act as a helper to the Web server as well.

As an example of the binary bridge approach, we again use a Flash object helper. Given
the following ActionScript code in our file (ajaxtcrflash.as), we see the exposure of a socket
method externally.

import flash.external.ExternalInterface;
class AjaxTCRFlash{

static function socket(url, port, callback)
{
var socketObj = new XMLSocket();
socketObj.connect(url, port);
socketObj.onData = function(input:String) {
ExternalInterface.call(callback, input.toString());
};
}
static function main() {
ExternalInterface.addCallback("socket", null, socket);
}
}

Similar to the cross-domain example earlier in the chapter, we compile this code into a
SWF file and take the created SWF file and insert it into the page. We do have to address the
various browser differences for inserting and referencing the SWF file, but once it is put in
the page, we simply call its externally exposed socket() method and signal what the
callback is that we want it to populate the page with.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
Free download pdf