javascript - Using transferable objects from a Web Worker -
i have code create web worker:
w = new worker("webwork.js"); w.onmessage = function(event) { alert(event.data); }
and webwork.js
code web worker:
self.onmessage = function(event) { //var ss=r; //causes error because of undefined var ss=""; for(var currprop in event) { ss+=("event."+currprop+"="+event[currprop]+"\n"); } postmessage(ss); }
now want transfer 128-megabyte arraybuffer
code:
var r = new arraybuffer(1048576*128); w.postmessage(0, [r]);
now have supposedly transferred variable r
, how access web worker itself. have tried event.r
, r
, self.r
, other things trying add second function argument array of arraybuffers
, nothing works.
how can access transferred variable(s) web worker?
postmesage(amessage, transferlist)
in transferlist
must specify transferable objects, contained in amessage
:
var objdata = { str: "string", ab: new arraybuffer(100), i8: new int8array(200) }; objworker.postmessage(objdata, [objdata.ab, objdata.i8.buffer]);
on other side:
self.onmessage = function(objevent) { var strtext = objevent.data.str; var objtypedarray = objevent.data.ab; var objtypedarrayview = objevent.data.i8; }
Comments
Post a Comment