close
	== HTML5_WebWorker_Tutorial ==
	** Update: 2012/7/17
	
	[ The Category of Web Worker ]
	>	Dedicated Worker:
			The worker is dedicated to the script(the boss script) loading the worker. 
			Other scripts cannot communicate with the worker.
	>	Shared Worker:
			The worker is shared with multiple scripts, not only the boss script.
			Different scripts can communicate with the worker
	
	[ The Scope ]
	>  In the Worker script, there is no "window" object.
		Instead, "self (this)" reference the global scope for the worker.
		But, notice that "self" in the Worker is not the same thing as "window" in the HTML page.
	
	[ The Security ]
	>	Workers has no access to
			*	The DOM (it's not thread-safe), the window, the document, the parent object
	>	Workers has access to
			*	The navigator  object, XMLHttpRequest, the application cache, the location object (read-only)
			*	Timing functions : setTimeout()/clearTimeout() and setInterval()/clearInterval()
			*	Importing external scripts : importScripts()
					-	importScripts('script1.js', 'script2.js');
			*	Spawning other web workers
					-	URIs within subworkers are resolved relative to their parent worker's location
					-	The same origin policy
					-	Example : http://www.whatwg.org/specs/web-apps/current-work/multipage/workers.html#delegation
	>	The same origin policy :
			Worker scripts must be external files with the same scheme as their calling page. 
			Thus, you cannot load a script from a data: URL or javascript: URL, 
			and an https: page cannot start worker scripts that begin with http: URLs.
	> Restriction with local access :
			Due to Google Chrome's security restrictions, workers will not run locally (e.g. from file://) in the latest versions of the browser. 
			Instead, they fail silently! 
			To run your app from the file:// scheme, run Chrome with the --allow-file-access-from-files flag set.
			NOTE: It is not recommended to run your primary browser with this flag set.
			It should only be used for testing purposes and not regular browsing.
			Other browsers do not impose the same restriction.
	
	== Dedicated Worker ==
	[ Create a Worker ]
	>	var worker = new Worker('example.js');
	
	[ Communicate between The Boss and The Worker ]
	>	Using worker.postMessage() in the boss scope / self.postMessage() in the worker scope to pass message.
		Using the event "onmessage" to act on the message when a message passing event happens.
	>	The boss :
			var worker = new Worker('example.js');
			worker.onmessage = function (e) {
				var msg = e.data;
				// Do sth with the message (data) passed in.
			}
			worker.postMessage(msg);
	>	The Wroker :
			self.onmessage = function (e) {
				var msg = e.data;
				// Do sth with the message (data) passed in.
				self.postMessage(processedData); // Report the processed data back to the boss.
			}
			
	[ Inline Workers ]
	>	By simulating the noraml case of importing script, we can build a WebWorker on the boss page :
			1)	New a BlobBuilder object
					*	var bb = new BlobBuilder(); // window.WebKitBlobBuilder in CH 12, window.MozBlobBuilder in FF 6
			2)	Append the code to that BlobBuilder object (making a script)
					*	bb.append(... ... ...);
			3)	Create a object URL to that BlobBuilder object by using window.URL.createObjectURL()
					*	var blobURL = window.URL.createObjectURL(bb.getBlob()); // In CH 10+, window.webkitURL.createObjectURL()
			4) Using the object URL on the WebWorker exactly like a path to the script
					*	var worker = new Worker(blobURL);
			5)	Terminate the worker :
					*	Notice :  killed immediately without an opportunity to complete its operations or clean up after itself.
					*	In the boss script, call worker.terminate();
					*	In the worker script, call self.close();
	>	Blob URLs are unique and last for the lifetime of your application (e.g. until the document is unloaded) if not released.
			*	Release the object URL by calling window.URL.revokeObjectURL()
					-	window.URL.revokeObjectURL(blobURL); // // In CH 10+, window.webkitURL.revokeObjectURL()
	>	In Chrome, there's a nice page to view all of the created blob URLs: chrome://blob-internals/.
	>	Import scripts within the Inline Worker:
		importScripts() requires the same origin policy, however a blob URL is prefixed with "blob:".
		The "blob:" prefix would cause the browser to think the blob URL is not from the same origin so
		a  cross origin error would occur.
		The solution to this cross origin error is to manually build a legal URL for importScripts().
		
	[ Error Handling ]
	>	The boss can handle the error returned by the worker by registering an "onerror" event.
	>	The event doesn't bubble and is cancelable; 
		to prevent the default action from taking place, the worker can call the error event's preventDefault() method.
	>	Error event object (err) :
		The 3 main useful properties in err :
			*	err.filename = the name of the worker script that caused the error.
			*	err.lineno = the line number where the error occurred.
			*	err.message = a meaningful description of the error.
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 Foxbrush 的頭像
    Foxbrush

    Foxbrush

    Foxbrush 發表在 痞客邦 留言(0) 人氣()