Created
August 16, 2011 15:22
-
-
Save jay3sh/1149347 to your computer and use it in GitHub Desktop.
WebWorker postMessage for debug
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// In the Main thread | |
// | |
var worker = new Worker('/path/of/webworker/code.js') | |
worker.onmessage = function (e) { | |
var result = JSON.parse(e.data); | |
if(result.type == 'debug') { | |
console.log(result.msg); | |
} else if(result.type == 'response') { | |
// ... use result.answer ... | |
} | |
} | |
// | |
// In the WebWorker | |
// | |
function debug(msg) { | |
postMessage(JSON.stringify({type:'debug',msg:msg})); | |
} | |
onmessage = function (e) { | |
var inputData = e.data; | |
// work on input data | |
debug('Working OK'); | |
// work some more | |
// ... | |
postMessage(JSON.stringify({type:'response', answer:42})); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment