Friday, October 19, 2012

Web Workers Freak Out Over the Silliest Things

‹prev | My Chain | next›

I was pleasantly surprised to find that the r52 release of Three.js was a drop-in replacement for the r49 version that I have been using in Gaming JavaScript. Tonight I intend to see if the same can be said for Physijs, the physics engine that I used in several games.

So I load up one of the games in http://gamingjs.com/ice/ and convert it to the r52 release. For now, Three.js and all related JavaScript libraries remain in the http://gamingJS.com/52 URL-space. So the <script> tags look like:
<body></body>
<script src="http://gamingJS.com/52/Three.js"></script>
<script src="http://gamingJS.com/52/Detector.js"></script>
<script src="http://gamingJS.com/52/physi.js"></script>
<script src="http://gamingJS.com/52/ChromeFixes.js"></script>
<script>
// Physics
Physijs.scripts.ammo = 'http://gamingJS.com/52/ammo.js';
Physijs.scripts.worker = 'http://gamingJS.com/52/physijs_worker.js';
The main selling point of Mr Doob's code editor is that you can see a preview of the code after each change. As I am adding the /52 URL to each of those lines, I can see that everything continues to work just fine… until I change that very last line.

Ugh. Web workers. Why did it have to be web workers? (Very dangerous)

In the JavaScript console, I see the following error:
Uncaught Error: INVALID_STATE_ERR: DOM Exception 11
I suspect that I am not allowed because the editor (and preview) are located in a different URL path (http://gamingJS.com/ice) than the web worker (http://gamingJS.com/52). If I use the old worker that is in the path above the code editor (http://gamingJS.com/), then it works.

The other possibility is that the new Physijs does something funky that is not being allowed.

To test this out, I drop down to my local copy of the Gaming JavaScript site with the code-editor. I reproduce the same "DOM Exception 11" error by pointing at a worker in the /52 path. Now, I copy the latest physijs_worker script over the older version in the top-level directory of the site. I reload the page and… it works just fine.

Sigh.

So it does not appear to be the new Physijs worker. Which means that my earlier hypothesis is looking likely. Except that it should not be correct. Per the specification, errors should be thrown if the origin of the site and worker are different. The origin for both is http://gamingJS.com (or http://localhost:4000).

At something of a loss, I try it out in Firefox, which works:


In that capture, the ball in the preview has fallen onto the board and rolled nearly to the front. In other words physics is working. The rigid platform stopped the ball and the ball fell in gravity. There are no console errors about security. And I am using the /52/physijs_worker.js path.

So this is looking like a Chrome bug. Or a weird interaction between web workers and the preview frame created by the code-editor. Regardless of the cause, the result is that I cannot use version URL spaces for the scripts in Gaming JavaScript. I was not sure that I wanted to include that concept in the book anyway, but I dislike being prevented from having the option.

I will ruminate on this and see if I can't come up with a solution. If you are interested, the code in question (without the /52 URL space) is editable at gamingJS.com/ice.


Day #544

No comments:

Post a Comment