Preface: This is a bad idea with untested code. If you deploy it on a production server bad things will happen.
I sniped the browser detection code from Quirksmode and it seems to work fairly well. The rest of the code is below:
The trickiest part of getting this to work was Adobe’s insane rules for allowing their Socket classes to connect to servers. In order for the client to successfully connect to the server you need to serve a XML policy file from port 843. Additionally, this file can’t be served by a HTTP server but must be a custom server that only spits back the file along with a null carriage return. A detailed description of this abortion is here http://www.adobe.com/devnet/flashplayer/articles/socket_policy_files.html
This really posses two problems. One, you need to be running some random “policy file server” for Flex sockets to be of any use. And two, since 843 is a privileged port, this server can’t be started by a regular user.
- Listen for connections from the Flex and accept them when they come in.
- When a message comes in – it can either be a request to create a new web worker or a postMessage() event containing some data for an existing worker.
- Otherwise if Flex passed a postMessage() message the server will forward that data to the running web worker.
- The other event that happens is that a web worker can send messages back to the Flex.
Anyway, I tested this on IE7+ and it seemed to work decently well. Per the warning on top I don’t want to leave this running on a live server anywhere.
If you want to get it to actually run, do the following:
- Download the zip of all the sources here.
- Start the JAR in WebWorkerServer/WebWorkerServer.jar with java -jar WebWorkerServer.jar 9999
- On the top of web/sfwwcompat.js change the IP address or the server to where your Java server is located (localhost if you want)
- Open web/wwsha1.html in IE or Chrome 2.0 and you should see stuff happen.
What’s in the box:
- WebWorkerConduit/ contains the Flex applicaiton.
- WebWorkerServer/ contains the Java server.
Credits: I borrowed the WebWorker SHA1 implementation from John Resig who adapted it from Ray C Morgan.
So here is another crazy idea. Instead of executing the WebWorker code on the server, would it be possible to dynamically make the WebWorker code re-entrant using setTimeout() on the client where loop structures exist?