public MandelbrotAsync() { var w = new Worker( scope => { Console.WriteLine("waiting for scope data"); int shift = 0; var once = false; scope.onmessage += ze => { #region waiting for scope data if (!once) { once = true; return; } #endregion var frame = (byte[])ze.data; var zDefaultWidth = MandelbrotProvider.DefaultWidth; var zDefaultHeight = MandelbrotProvider.DefaultHeight; var e = new Stopwatch(); e.Start(); var buffer = MandelbrotProvider.DrawMandelbrotSet(shift); var k = 0; for (int i = 0; i < zDefaultWidth; i++) for (int j = 0; j < zDefaultHeight; j++) { var i4 = i * 4; var j4 = j * 4; frame[(i4 + j4 * zDefaultWidth + 2)] = (byte)((buffer[k] >> (0 * 8)) & 0xff); frame[(i4 + j4 * zDefaultWidth + 1)] = (byte)((buffer[k] >> (1 * 8)) & 0xff); frame[(i4 + j4 * zDefaultWidth + 0)] = (byte)((buffer[k] >> (2 * 8)) & 0xff); frame[(i4 + j4 * zDefaultWidth + 3)] = 0xff; k++; } ze.ports.WithEach(port => port.postMessage(frame)); //Console.WriteLine("worker: " + new { shift, e.ElapsedMilliseconds }); shift++; }; } ); w.postMessage(new { }); var memory = new Queue<byte[]>(); for (int i = 0; i < 3; i++) memory.Enqueue(new byte[MandelbrotProvider.DefaultWidth * MandelbrotProvider.DefaultHeight * 4]); Native.window.onframe += delegate { // need a few next frames if (memory.Count > 0) { var x = memory.Dequeue(); Action<MessageEvent> yield = e => { var xe = new Stopwatch(); xe.Start(); if (frame != null) memory.Enqueue(frame); frame = (byte[])e.data; if (onframe != null) onframe(); //Console.WriteLine("yield: " + new { xe.ElapsedMilliseconds }); }; w.postMessage(x, yield); } }; }
/// <summary> /// This is a javascript application. /// </summary> /// <param name="page">HTML document rendered by the web server which can now be enhanced.</param> public Application(IApp page) { new Abstractatech.ConsoleFormPackage.Library.ConsoleForm { }.InitializeConsoleFormWriter().Show(); page.Invoke.onclick += delegate { var now = DateTime.Now; var n = new XElement("client", new XAttribute("value", "" + now) ); service.Invoke( n, xml => { Console.WriteLine(xml.ToString()); //new IHTMLPre { innerText = xml.ToString() }.AttachTo(page.right); } ); }; page.InvokeSpecal.onclick += delegate { var now = DateTime.Now; var n = new XElement("client", new XAttribute("value", "" + now) ); service.InvokeSpecial( n, xml => { Console.WriteLine(xml.ToString()); //new IHTMLPre { innerText = xml.ToString() }.AttachTo(page.left); } ); }; page.InvokeSpecalInsideWorker.onclick += delegate { Console.WriteLine("InvokeSpecalInsideWorker"); var ww = new Worker( worker => { // running in worker context. cannot talk to outer scope yet. worker.RedirectConsoleOutput(); Console.WriteLine("at worker"); var now = DateTime.Now; var n = "<client value='now' />"; var xservice = new ApplicationWebService(); Console.WriteLine("before InvokeSpecialString"); xservice.InvokeSpecialString( n, xml => { Console.WriteLine("at InvokeSpecialString"); Console.WriteLine(xml); //new IHTMLPre { innerText = xml.ToString() }.AttachTo(page.left); } ); } ); ww.onmessage += e => { Console.Write("" + e.data); }; }; page.Clear.onclick += delegate { // Error 1 The call is ambiguous between the following methods or properties: // 'ScriptCoreLib.JavaScript.Extensions.INodeExtensions.Clear(ScriptCoreLib.JavaScript.DOM.INode)' and // 'ScriptCoreLib.JavaScript.Extensions.INodeExtensions.Clear(ScriptCoreLib.JavaScript.DOM.INode)' X:\jsc.svn\examples\javascript\EventSourceForWebServiceYield\EventSourceForWebServiceYield\Application.cs 71 21 EventSourceForWebServiceYield //page.left.Clear(); //page.right.Clear(); }; }
/// <summary> /// This is a javascript application. /// </summary> /// <param name="page">HTML document rendered by the web server which can now be enhanced.</param> public Application(IApp page) { new Abstractatech.ConsoleFormPackage.Library.ConsoleForm { }.InitializeConsoleFormWriter().Show(); var ww = new Worker( worker => { // running in worker context. cannot talk to outer scope yet. //worker.RedirectConsoleOutput(); // hello from the background worker { self = [object WorkerGlobalScope] } Console.WriteLine("hello from the background worker " + new { Native.self }); // https://code.google.com/p/chromium/issues/detail?id=31666 // http://stackoverflow.com/questions/9259251/unable-to-create-web-worker-from-inside-webworker-in-chrome // Uncaught ReferenceError: Worker is not defined // works in FF // does NOT work in Chrome // works in IE // ww: www: goo { self = [object DedicatedWorkerGlobalScope] } var www = new Worker( wworker => { // running in worker context. cannot talk to outer scope yet. //wworker.RedirectConsoleOutput(); // hello from the background worker { self = [object WorkerGlobalScope] } Console.WriteLine("goo " + new { Native.self }); } ); www.onmessage += e => { Console.Write("www: " + e.data); }; } ); ww.onmessage += e => { Console.Write("ww: " + e.data); }; for (int xi = 0; xi < 4; xi++) new IHTMLButton { innerText = "cpu #" + xi }.AttachToDocument().WhenClicked( btn => { btn.disabled = true; var www = new Worker( wworker => { // running in worker context. cannot talk to outer scope yet. //wworker.RedirectConsoleOutput(); // hello from the background worker { self = [object WorkerGlobalScope] } var x = 0.0; Console.WriteLine("Start"); var s = new Stopwatch(); s.Start(); for (int j = 0; j < 32; j++) { for (int i = 0; i < 32000000; i++) { x = Math.Sin(i); } Console.WriteLine(new { j, s.Elapsed }.ToString()); } Console.WriteLine("Stop"); } ); www.onmessage += e => { Console.Write("www: " + e.data); }; } ); }
// jsc, every visit is a new process, see procfs /// <summary> /// This is a javascript application. /// </summary> /// <param name="page">HTML document rendered by the web server which can now be enhanced.</param> public Application(IApp page) { Native.worker.With( // #0 worker => { // ww3.onmessage: { data = xxx fake0 { href = http://192.168.1.100:4216/view-source#goo, var href = Native.worker.location.href; var index = href.SkipUntilOrEmpty("#"); // fake inline worker.postMessage("xxx fake0 " + new { index, worker.location.href }); } ); Native.worker.With( // #1 worker => { // fake inline worker.postMessage("xxx fake1 "); } ); Native.window.With( window => { // what about multiple? // what about dynamic handler selector? new IHTMLButton { innerText = "w2" }.AttachToDocument().WhenClicked( btn => { // whats next, ApplicationWorker with methods or WorkerAction<> delegate // what if jsc would detect any delegate Action<Action> that only communicates // cia delegates and turn them into background workers? var ww = new Worker( // #2 worker => { // running in worker context. cannot talk to outer scope yet. Console.WriteLine("xxx via Console"); worker.postMessage("xxx"); } ); // ww.onmessage: { data = xxx fake } ww.onmessage += e => { Console.WriteLine("ww.onmessage: " + new { e.data }); }; } ); Action<string, Action<DedicatedWorkerGlobalScope>, Action<MessageEvent>> work = (innerText, handler, onmessage) => { new IHTMLButton { innerText = innerText }.AttachToDocument().WhenClicked( btn => { var ww = new Worker(handler); // ww.onmessage: { data = xxx fake } ww.onmessage += onmessage; } ); }; Action<MessageEvent> work7onmessage = e => { Console.WriteLine("work 7 onmessage " + new { e.data }); }; work("work 7", arg2: worker => { worker.postMessage("work 7"); }, arg3: work7onmessage ); Action<MessageEvent> work8onmessage = e => { Console.WriteLine("work 8 onmessage " + new { e.data }); }; work("work 8", arg2: worker => { worker.postMessage("work 8"); }, arg3: work8onmessage ); new IHTMLButton { innerText = "w3" }.AttachToDocument().WhenClicked( btn => { var ww = new Worker( // #2 worker => { // running in worker context. cannot talk to outer scope yet. worker.postMessage("xxx3"); } ); // ww.onmessage: { data = xxx fake } ww.onmessage += e => { Console.WriteLine("ww3.onmessage: " + new { e.data }); }; } ); new IHTMLButton { innerText = "w goo" }.AttachToDocument().WhenClicked( btn => { var ww = new Worker("view-source#goo"); // ww.onmessage: { data = xxx fake } ww.onmessage += e => { Console.WriteLine("ww3.onmessage: " + new { e.data }); }; } ); } ); }
/// <summary> /// This is a javascript application. /// </summary> /// <param name="page">HTML document rendered by the web server which can now be enhanced.</param> public Application(IApp page) { // http://qscribble.blogspot.com/2012/07/asyncawait-vs-stack-switching.html // https://sites.google.com/a/jsc-solutions.net/backlog/knowledge-base/2013/201308/20130825 // X:\jsc.svn\core\ScriptCoreLib\JavaScript\BCLImplementation\System\Delegate.cs yield_locals.loc0 = "foo"; Action yield = delegate { Console.WriteLine("more work " + new { yield_locals.loc0, Thread.CurrentThread.ManagedThreadId }); // this function can run in both DOM and WebWorker. // if we were to talk to DOM, would we need to detect and autoswitch the context? //page.Content.innerText = "goo"; }; // { Target = [object Window], Method = { MethodToken = AQAABrpGJTe973zWF5WUSg } } Console.WriteLine( new { yield.Target, yield.Method } ); //yield.Method.MetadataToken var Method = yield.Method; var MethodToken = (string)(object)((__MethodInfo)(object)Method).MethodToken; Console.WriteLine( new { MethodToken } ); // { MethodToken = AQAABrpGJTe973zWF5WUSg } // should be static and parameterless call // more work { loc0 = foo } Method.Invoke(null, new object[0]); var counter = 0; new IHTMLButton { innerText = "spawn new thread" }.AttachToDocument().WhenClicked( delegate { counter++; yield_locals.loc0 = "loc0 #" + new { counter }; var w = new Worker( worker => { Console.WriteLine("will wait for scope ..."); worker.onmessage += e => { // { data = ldstr:AQAABrpGJTe973zWF5WUSg } Console.WriteLine(new { e.data }); dynamic data = e.data; //var data___string = (object)data.__string; //var datax = Expando.Of(data___string); //dynamic target = __string; //datax.GetMembers().WithEach( // nn => // { // Console.WriteLine("worker reads " + nn.Name); // target[nn.Name] = nn.Value; // } //); string message = data.message; var scope_MethodToken = message.SkipUntilOrEmpty("ldstr:"); if (!string.IsNullOrEmpty(scope_MethodToken)) { Console.WriteLine(new { scope_MethodToken }); //yield_locals.loc0 = "bar"; // more work { loc0 = bar } var scope_yield_add = (MulticastDelegate)IFunction.Of(scope_MethodToken); // Uncaught TypeError: Object #<lxyFdjnUJTaDStMnfRqY_aQ> has no method 'bBoABiRtYD2yr4CzwPIbLw' // will this fail? jsc might need to do a reconstructive cast here Action scope_yield = delegate { scope_yield_add.Method.Invoke(null, new object[0]); }; // Uncaught TypeError: Cannot read property 'undefined' of null //scope_yield += (Action)scope_yield_add; //scope_yield_add.Method.Invoke(null, new object[0]); scope_yield(); } //f.apply(null); // running in worker context. cannot talk to outer scope yet. // fist thing to support should be to allow incoming local strings Console.WriteLine("working ..."); var s = new Stopwatch(); s.Start(); // spin the cpu // how long do we have to, in order for task manager to notice? // this should keep one cpu utilized atleast at 70% for (int i = 0; i < 5; i++) { var xs = new Stopwatch(); xs.Start(); while (xs.ElapsedMilliseconds < 1000) ; Console.WriteLine("."); } Console.WriteLine("working ... done " + new { s.Elapsed }); // how can we yield back to DOM? // SynchronizationContext.Current. // special magic to break out of webworker context? // awaitable? worker.yield( delegate { // which context are we now in? // DOM? nested worker? } ); }; } ); // staticfields.string.data1 // can we send a copy of all static fields? // send scope/context data // we need domain memory kind of thing // memory that can be swapped dynamic xdata = new object(); xdata.message = "ldstr:" + MethodToken; //dynamic xdata___string = new object(); //Expando.Of(__string).GetMembers().With( // xx => // { // xx.WithEach( // nn => // { // if (nn.Value != null) // { // Console.WriteLine("will copy " + nn.Name); // xdata___string[nn.Name] = nn.Value; // } // } // ); // } // ); ////xdata___string.fake = "fake"; //xdata.__string = xdata___string; w.postMessage((object)xdata); } ); Expando.Of(__string).GetMembers().With( xx => { Console.WriteLine(new { xx.Length }); var bytes = 0; xx.WithEach( nn => { if (nn.Value != null) { bytes += nn.Value.Length; Console.WriteLine(nn.Name + " = " + nn.Value); } else { Console.WriteLine(nn.Name + " = null"); } } ); Console.WriteLine(new { bytes }); } ); }
// dont we have our own encoder now?? // X:\jsc.svn\core\ScriptCoreLib\Shared\BCLImplementation\System\Tuple.cs //no implementation for System.Tuple b6efdcc2-6386-375e-84aa-6732b6518b3f //script: error JSC1000: No implementation found for this native method, please implement [static System.Tuple.Create(System.IProgress`1[[System.Int32, mscorlib, Version=4.0.0.0, Culture=ne //script: warning JSC1000: Did you reference ScriptCoreLib via IAssemblyReferenceToken? //script: error JSC1000: error at GIFEncoderWorker..ctor, // assembly: U:\jsgif.Application.exe // type: GIFEncoderWorker, jsgif.Application, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null // offset: 0x0049 // method:Void .ctor(Int32, Int32, Int32, Int32, System.Collections.Generic.IEnumerable`1[System.Byte[]], System.Action`1[System.Int32]) //*** Compiler cannot continue... press enter to quit. // based on http://antimatter15.com/wp/2010/07/javascript-to-animated-gif/ //cript: error JSC1000: No implementation found for this native method, please implement [static System.Tuple.Create(System.IProgress`1[[System.Int32, //cript: warning JSC1000: Did you reference ScriptCoreLib via IAssemblyReferenceToken? //cript: error JSC1000: error at GIFEncoderWorker..ctor, //assembly: V:\jsgif.Application.exe //type: GIFEncoderWorker, jsgif.Application, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null //offset: 0x0049 // method:Void .ctor(Int32, Int32, Int32, Int32, System.Collections.Generic.IEnumerable`1[System.Byte[]], System.Action`1[System.Int32]) //** Compiler cannot continue... press enter to quit. /// <summary> /// This is a javascript application. /// </summary> /// <param name="page">HTML document rendered by the web server which can now be enhanced.</param> public Application(IDefault page) { // https://bugzilla.mozilla.org/show_bug.cgi?id=709490 // http://stackoverflow.com/questions/7844886/using-webgl-from-inside-a-web-worker-is-it-possible-how const int x = 640; const int y = 480; // do we need automatic decomposer? var context = new CanvasRenderingContext2D(x, y); // needs to be in dom? no var canvas = context.canvas; context.fillStyle = "rgb(255,255,255)"; context.fillRect(0, 0, canvas.width, canvas.height); // rebuild redux? var my_gradient = context.createLinearGradient(0, 0, 16, 0); my_gradient.addColorStop(0, "blue"); my_gradient.addColorStop(1, "white"); //context.fillStyle = my_gradient; //"rgb(255,255,255)"; context.fillStyle = "rgb(255,255,255)"; context.fillRect(0, 0, 16, canvas.height); //GIF can't do transparent so do white Action yield = delegate { }; var r = new Random(); Func<byte> random = r.NextByte; //encoder.addFrame(context); // 4 : 3sec // 8 : 0.00:00:06 // 16 : 0.00:00:18 var frames = new List<byte[]>(); // whats the difference? should jsc switch to tyhe typed array yet? //var frames = new List<byte[]>(); for (int i = 0; i < 16; i++) { context.fillStyle = "rgb(" + random() + "," + random() + "," + random() + ")"; context.fillRect( random() / 4, random() / 4, 32 + random(), 32 + random() ); var data = context.getImageData().data; //Uint8Array // { data = [object Uint8ClampedArray] } Console.WriteLine(new { data }); frames.Add(data); } yield += delegate { }; new IHTMLButton { innerText = "encode!" }.AttachToDocument().WhenClicked( delegate { var e = new Stopwatch(); e.Start(); Console.WriteLine("new encoder"); var encoder = new GIFEncoder(); encoder.setSize(x, y); encoder.setRepeat(0); //auto-loop encoder.setDelay(1000 / 15); encoder.start(); foreach (var data in frames) { Console.WriteLine("addFrame"); encoder.addFrame((Uint8ClampedArray)(object)data, true); } Console.WriteLine("finish"); encoder.finish(); var bytes = Encoding.ASCII.GetBytes(encoder.stream().getData()); var src = "data:image/gif;base64," + Convert.ToBase64String(bytes); Console.WriteLine(e.Elapsed); new IHTMLImage { src = src }.AttachToDocument(); } ); new IHTMLButton { innerText = "encode via worker" }.AttachToDocument().WhenClicked( delegate { var e = new Stopwatch(); e.Start(); var w = new Worker( scope => { Console.WriteLine("new encoder"); var encoder = new GIFEncoder(); encoder.setSize(x, y); encoder.setRepeat(0); //auto-loop encoder.setDelay(1000 / 15); encoder.start(); scope.onmessage += ee => { dynamic xdata = ee.data; string message = xdata.message; //Console.WriteLine(new { message }); if (message == "addFrame") { Console.WriteLine("addFrame"); // http://stackoverflow.com/questions/8776751/web-worker-dealing-with-imagedata-working-with-firefox-but-not-chrome //byte[] data = xdata.data; Uint8ClampedArray data = xdata.data; //data[ // { data = [object Uint8ClampedArray] } //Console.WriteLine(new { data }); encoder.addFrame(data, true); } if (message == "finish") { Console.WriteLine("finish"); encoder.finish(); var bytes = Encoding.ASCII.GetBytes(encoder.stream().getData()); var src = "data:image/gif;base64," + Convert.ToBase64String(bytes); ee.ports.WithEach(port => port.postMessage(src)); } }; } ); foreach (var data in frames) { dynamic xdata = new object(); xdata.message = "addFrame"; xdata.data = data; // Error 4 Cannot convert lambda expression to type 'ScriptCoreLib.JavaScript.DOM.MessagePort[]' because it is not a delegate type X:\jsc.svn\examples\javascript\synergy\jsgif\jsgif\Application.cs 171 38 jsgif w.postMessage( (object)xdata, (MessageEvent ee) => { // ? Console.WriteLine("addFrame done"); } ); } w.postMessage( new { message = "finish" }, (MessageEvent ee) => { Console.WriteLine("finish done"); var src = (string)ee.data; Console.WriteLine(e.Elapsed); new IHTMLImage { src = src }.AttachToDocument(); w.terminate(); } ); //var bytes = Encoding.ASCII.GetBytes(encoder.stream().getData()); //var src = "data:image/gif;base64," + Convert.ToBase64String(bytes); //new IHTMLImage { src = src }.AttachToDocument(); } ); new IHTMLButton { innerText = "encode via GIFEncoderAsync. works!" }.AttachToDocument().WhenClicked( btn => { btn.disabled = true; var e = new Stopwatch(); e.Start(); var w = new GIFEncoderAsync( width: x, height: y, delay: 1000 / 10 ); //http://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_meter var innerText = btn.innerText; btn.Clear(); dynamic meter = new IHTMLElement("meter").AttachTo(btn); meter.max = frames.Count; var value = 0; foreach (var data in frames) { w.addFrame((Uint8ClampedArray)(object)data, delegate { Console.WriteLine("addFrame done"); value++; meter.value = value; } ); } w.finish( src => { Console.WriteLine("finish done"); Console.WriteLine(e.Elapsed); new IHTMLImage { src = src }.AttachToDocument(); btn.innerText = innerText; btn.disabled = false; btn.title = new { e.Elapsed }.ToString(); } ); } ); new IHTMLButton { innerText = "encode via GIFEncoderWorker. crashes the system? why?" }.AttachToDocument().WhenClicked( async btn => { Console.WriteLine("encoding!"); btn.disabled = true; var e = new Stopwatch(); e.Start(); var src = await new GIFEncoderWorker( x, y, delay: 1000 / 10, frames: frames, AtFrame: index => { btn.innerText = new { index, frames.Count }.ToString(); } ); Console.WriteLine("done!"); Console.WriteLine(e.Elapsed); new IHTMLImage { src = src }.AttachToDocument(); btn.disabled = false; btn.title = new { e.Elapsed }.ToString(); } ); }
/// <summary> /// This is a javascript application. /// </summary> /// <param name="page">HTML document rendered by the web server which can now be enhanced.</param> public Application(IApp page) { // { self = [object Window] } //var self = new IFunction("return this;").apply(null); // { self = [object Window], window = [object Window], screen = [object Screen], document = [object HTMLDocument], page = [object Object] } Console.WriteLine(new { Native.self, Native.window, Native.screen, Native.document, page }); Native.worker.With( worker => { worker.onmessage += e => { // onmessage: { data = mirror: { data = hey you, ports = 1 } } worker.postMessage( "mirror: " + new { e.data, ports = e.ports.Length } ); e.ports.WithEach( port => { port.postMessage( "port mirror: " + new { e.data, ports = e.ports.Length } ); } ); }; worker.postMessage( "hello from worker? " + new { Native.self, Native.window, Native.screen, Native.document, page } ); } ); Native.document.With( document => { // what if we are in web worker? // Uncaught ReferenceError: document is not defined new IHTMLButton { innerText = "worker" }.AttachToDocument().WhenClicked( delegate { Console.WriteLine("before Worker"); //var tt = new Thread(); // Error 1 'ScriptCoreLib.JavaScript.DOM.Worker.Worker(System.Action<ScriptCoreLib.JavaScript.DOM.DedicatedWorkerGlobalScope>)' is obsolete: 'not implemented yet' x:\jsc.svn\examples\javascript\OmniWebWorkerExperiment\OmniWebWorkerExperiment\Application.cs 87 38 OmniWebWorkerExperiment var ww = new Worker( (DedicatedWorkerGlobalScope worker) => { // running in worker context. cannot talk to outer scope yet. worker.postMessage("xxx"); } ); ww.onmessage += e => { Console.WriteLine("ww.onmessage: " + new { e.data }); }; var w = new Worker(); w.onmessage += e => { Console.WriteLine("onmessage: " + new { e.data }); // onmessage: { data = hello from worker? 1 } }; var c = new MessageChannel(); c.port1.onmessage += e => { Console.WriteLine("port1.onmessage: " + new { e.data }); // onmessage: { data = hello from worker? 1 } }; c.port1.start(); c.port2.start(); // Transferable should be an interface? w.postMessage("hey you", new[] { c.port2 }); //w.postMessage("hey you", new[] { (Transferable)(object)c.port2 }); Console.WriteLine("after Worker"); //onmessage: { data = hello from worker? { self = [object global], window = , screen = , document = , page = } } //onmessage: { data = mirror: { data = hey you } } } ); } ); }
public void InitializeContent() { //FormStyler.AtFormCreated = FormStylerLikeAero.LikeAero; //FormStyler.AtFormCreated = FormStylerLikeFloat.LikeFloat; //FormStyler.AtFormCreated = FormStyler.LikeWindows3; FormStyler.AtFormCreated = FormStyler.LikeVisualStudioMetro; #region new Form var xf = new Form(); content.BackColor = System.Drawing.Color.Transparent; xf.Controls.Add(content); xf.Show(); content.button2.Click += delegate { #region Worker // we now should have simple scope sharing for Task.Run var www = new Worker( wworker => { // running in worker context. cannot talk to outer scope yet. //wworker.RedirectConsoleOutput(); // hello from the background worker { self = [object WorkerGlobalScope] } var x = 0.0; Console.WriteLine("Start"); var s = new Stopwatch(); s.Start(); for (int j = 0; j < 32; j++) { for (int i = 0; i < 32000000; i++) { x = Math.Sin(i); } Console.WriteLine(new { j, s.Elapsed }.ToString()); } Console.WriteLine("Stop"); } ); www.onmessage += e => { Console.Write("www: " + e.data); }; #endregion }; //new Abstractatech.ConsoleFormPackage.Library.ConsoleForm { }.InitializeConsoleFormWriter().Show(); #endregion Console.WriteLine("hello console!"); // The webpage at http://192.168.1.100:6669/ might be temporarily down or it may have moved permanently to a new web address. //Error code: ERR_UNSAFE_PORT }
/// <summary> /// This is a javascript application. /// </summary> /// <param name="page">HTML document rendered by the web server which can now be enhanced.</param> public Application(IApp page) { //new ConsoleForm { HandleFormClosing = false }.InitializeConsoleFormWriter().PopupInsteadOfClosing().Show(); Native.window.onmessage += e => { Console.WriteLine("Window onmessage: " + new { e.data }); }; //dynamic w = new IFunction("return new Worker('/w');").apply(null); try { // E/Web Console(30665): Uncaught ReferenceError: Worker is not defined at http://192.168.1.101:6612/view-source:48124 // does not exist on android webview! Action wwdone = delegate { }; // should automatic code analysis select part of this code // and make it background? InlineWorker ww = (data, yield) => { // thinking yield("long thinking complete! took a lot of cpu!", delegate { // ah more work // async? wwdone(); } ); }; ww("from app to worker ", (result, yield) => { // i think worker has more to do yield(); // call wwdone if done } ); var w = new Worker("/view-source/w"); // onmessage: { data = hello from worker? { self = [object global], constructor = function DedicatedWorkerContext() { [native code] }, prototype = , href = http://192.168.1.100:3054/w } } // view-source:26922 //onmessage: { data = mirror: { data = from app to worker } } w.onmessage += //IFunction.OfDelegate( new Action<MessageEvent>( e => { Console.WriteLine("onmessage: " + new { e.data }); // onmessage: { data = hello from worker? 1 } } ); //); w.postMessage("from app to worker "); page.SendAMessageToWebWorker.onclick += delegate { w.postMessage("from click to worker "); }; } catch (Exception ex) { // do we have stacktace in js? // script: error JSC1000: No implementation found for this native method, please implement [System.Exception.get_StackTrace()] //Console.WriteLine("error: " + new { ex.Message, ex.StackTrace }); Console.WriteLine("error: " + new { ex.Message }); } }
public PlasmaAsync(int _DefaultWidth, int _DefaultHeight) { var xscope = new { DefaultWidth = _DefaultWidth, DefaultHeight = _DefaultHeight }; var w = new Worker( scope => { Console.WriteLine("waiting for scope data"); int shift = 0; int zDefaultWidth = 0; int zDefaultHeight = 0; var once = false; Action<object> init = data => { int zzDefaultWidth = (data as dynamic).DefaultWidth; int zzDefaultHeight = (data as dynamic).DefaultHeight; zDefaultWidth = zzDefaultWidth; zDefaultHeight = zzDefaultHeight; }; scope.onmessage += ze => { #region waiting for scope data if (!once) { once = true; init(ze.data); Plasma.generatePlasma(zDefaultWidth, zDefaultHeight); return; } #endregion var frame = (byte[])ze.data; Console.WriteLine("got frame: " + new { frame.Length }); var e = new Stopwatch(); e.Start(); var buffer = Plasma.shiftPlasma(shift); var k = 0; for (int i = 0; i < zDefaultWidth; i++) for (int j = 0; j < zDefaultHeight; j++) { var i4 = i * 4; var j4 = j * 4; frame[(i4 + j4 * zDefaultWidth + 2)] = (byte)((buffer[k] >> (0 * 8)) & 0xff); frame[(i4 + j4 * zDefaultWidth + 1)] = (byte)((buffer[k] >> (1 * 8)) & 0xff); frame[(i4 + j4 * zDefaultWidth + 0)] = (byte)((buffer[k] >> (2 * 8)) & 0xff); frame[(i4 + j4 * zDefaultWidth + 3)] = 0xff; k++; } ze.ports.WithEach(port => port.postMessage(frame)); //Console.WriteLine("worker: " + new { shift, e.ElapsedMilliseconds }); shift++; }; } ); w.postMessage(xscope); var memory = new Queue<byte[]>(); for (int i = 0; i < 3; i++) memory.Enqueue(new byte[_DefaultWidth * _DefaultHeight * 4]); Native.window.onframe += delegate { // need a few next frames if (memory.Count > 0) { var x = memory.Dequeue(); Action<MessageEvent> yield = e => { var xe = new Stopwatch(); xe.Start(); if (frame != null) memory.Enqueue(frame); frame = (byte[])e.data; Console.WriteLine("got new frame: " + new { frame.Length }); if (onframe != null) onframe(); //Console.WriteLine("yield: " + new { xe.ElapsedMilliseconds }); }; Console.WriteLine("send frame: " + new { x.Length, x }); w.postMessage(x, yield); } }; Dispose = () => { w.terminate(); memory.Clear(); }; }