Skip to content

Instantly share code, notes, and snippets.

@LuoZijun
Last active February 13, 2017 16:40
Show Gist options
  • Save LuoZijun/9efafd7a69ffb00b0ad8db373073315e to your computer and use it in GitHub Desktop.
Save LuoZijun/9efafd7a69ffb00b0ad8db373073315e to your computer and use it in GitHub Desktop.
Web Worker

Test Zlib.js with Web Worker

Date: 2017/02/14
git clone "https://gist.github.com/LuoZijun/9efafd7a69ffb00b0ad8db373073315e"
cd ./9efafd7a69ffb00b0ad8db373073315e

wget "https://github.com/imaya/zlib.js/raw/master/bin/zlib.min.js"
python -m SimpleHTTPServer 8000
open "http://127.0.0.1:8000"
<!DOCTYPE html>
<html lang="zh_CN">
<head>
<meta name="renderer" content="webkit|ie-comp|ie-stand" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta charset="utf-8" />
<meta name="keywords" content="" />
<meta name="description" content="" />
<title>Zlib Test With WebWorker</title>
</head>
<body>
<h1>Zlib Test With WebWorker</h1>
</body>
<script type="text/javascript" src="./zlib.min.js"></script>
<script type="text/javascript">
var signaller_url = "./signaller.js";
var zlib_url = "./zlib.min.js";
function Thread (js_path){
this.handlers = [];
this.thread = new Worker(js_path);
this.thread.onmessage = this.onmessage.bind(this);
}
Thread.prototype.add_task = function (js_code, handler){
var request_id = this.handlers.length;
this.handlers.push(handler);
this.thread.postMessage({
"id" : request_id,
"code": js_code
});
};
Thread.prototype.onmessage = function (evt){
var request_id = evt.data.id;
if ( typeof this.handlers[request_id] === 'function' ) {
this.handlers[request_id](evt.data);
delete this.handlers[request_id];
}
};
var thread = new Thread(signaller_url);
function compress(data, callback){
// var deflate = new Zlib.Deflate(plain);
// var compressed = deflate.compress();
var javascript_code = 'importScripts("#ZLIB_URL#");'
+ 'var deflate = new Zlib.Deflate("#DATA#");'
+ 'deflate.compress();';
javascript_code = javascript_code.replace("#DATA#", data)
.replace("#ZLIB_URL#", zlib_url);
thread.add_task(javascript_code, function (evt){
callback(evt.result);
});
}
function decompress(data, callback){
var javascript_code = 'importScripts("#ZLIB_URL#");'
+ 'var data = "#DATA#";'
+ 'if ( data instanceof Uint8Array == false ) {'
+ ' data = data.split(",").map(function (n){return parseInt(n)});'
+ ' data = new Uint8Array(data);'
+ '}'
+ 'console.info("Decompress ...");'
+ 'console.info(data);'
+ 'var inflate = new Zlib.Inflate(data);'
+ 'inflate.decompress();';
// var inflate = new Zlib.Inflate(compressed);
// var plain = inflate.decompress();
javascript_code = javascript_code.replace("#DATA#", data)
.replace("#ZLIB_URL#", zlib_url);
thread.add_task(javascript_code, function (evt){
callback(evt.result);
});
}
function test_raw(){
var plain = "你好,世界!";
var deflate = new Zlib.Deflate(plain);
var compressed = deflate.compress();
console.log("plain: ", plain, "compressed: ", compressed);
var inflate = new Zlib.Inflate(compressed);
var plain_out = inflate.decompress();
console.log("compressed: ", compressed, "plain: ", plain_out);
}
function test_worker(){
compress("你好,世界!", function (compressed_data){
console.log("Compressed Data: ", compressed_data);
decompress(compressed_data, function (plain){
console.log("plain text: ", plain);
});
});
}
function main(){
test_raw();
test_worker();
}
main();
</script>
</html>
function ord(s){
return String.charCodeAt(s);
}
function chr(u8){
return String.fromCharCode(u8);
}
// worker code
onmessage = function(task) {
var request_id = task.data.id;
var code = task.data.code;
var result = eval(code);
if ( result instanceof Uint8Array ) {
result = result.toString();
}
postMessage({
"id" : request_id,
"result": result
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment