I've worked with Worker objects before, and it's pretty straightforward, especially with the tutorial.
So naturally the next step would be to work with SharedWorkers. I've got it down, but now it seems the SharedWorker is not responding correctly. I send a postMessage to the SharedWorker, and in my code I have the SharedWorker posting back the same text, kind of like an echo effect.
Unfortunately the SharedWorker only returns the first letter of the message sent from the main program, and does not log to the console the "Background" string to let me know the SharedWorker is running. Also the "Setup complete" log message proves that the SharedWorker was created.
Code...
The main HTML file...
<html>
<head>
<title>Test</title>
</head>
<body>
<script>
var w = new SharedWorker('sharedworker.js');
w.port.start();
w.port.postMessage("start");
w.port.onmessage = function(e){
console.log(e.data);
};
console.log("Setup complete.");
</script>
</body>
</html>
The SharedWorker script...
onconnect = function(e){
var port = e.ports[0];
port.addEventListener('message', function(e){
port.postMessage(e.data[0]);
});
port.start();
console.log("Background.");
}
What am I doing wrong?