annotate Platforms/Wasm/WasmWebService.js @ 299:3897f9f28cfa am-callable-and-promise

backup work in progress: updated messaging framework with ICallable
author am@osimis.io
date Fri, 14 Sep 2018 16:44:01 +0200
parents 9afafb192180
children ed1a4302154f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
221
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
1 mergeInto(LibraryManager.library, {
257
9afafb192180 using PAM
am@osimis.io
parents: 236
diff changeset
2 WasmWebService_ScheduleGetRequest: function(callback, url, headersInJsonString, payload) {
221
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
3 // Directly use XMLHttpRequest (no jQuery) to retrieve the raw binary data
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
4 // http://www.henryalgus.com/reading-binary-files-using-jquery-ajax/
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
5 var xhr = new XMLHttpRequest();
257
9afafb192180 using PAM
am@osimis.io
parents: 236
diff changeset
6 var url_ = UTF8ToString(url);
9afafb192180 using PAM
am@osimis.io
parents: 236
diff changeset
7 var headersInJsonString_ = UTF8ToString(headersInJsonString);
9afafb192180 using PAM
am@osimis.io
parents: 236
diff changeset
8
9afafb192180 using PAM
am@osimis.io
parents: 236
diff changeset
9 xhr.open('GET', url_, true);
221
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
10 xhr.responseType = 'arraybuffer';
257
9afafb192180 using PAM
am@osimis.io
parents: 236
diff changeset
11 var headers = JSON.parse(headersInJsonString_);
9afafb192180 using PAM
am@osimis.io
parents: 236
diff changeset
12 for (var key in headers) {
9afafb192180 using PAM
am@osimis.io
parents: 236
diff changeset
13 xhr.setRequestHeader(key, headers[key]);
9afafb192180 using PAM
am@osimis.io
parents: 236
diff changeset
14 }
9afafb192180 using PAM
am@osimis.io
parents: 236
diff changeset
15 // console.log(xhr);
221
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
16 xhr.onreadystatechange = function() {
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
17 if (this.readyState == XMLHttpRequest.DONE) {
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
18 if (xhr.status === 200) {
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
19 // TODO - Is "new Uint8Array()" necessary? This copies the
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
20 // answer to the WebAssembly stack, hence necessitating
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
21 // increasing the TOTAL_STACK parameter of Emscripten
257
9afafb192180 using PAM
am@osimis.io
parents: 236
diff changeset
22 WasmWebService_NotifySuccess(callback, url_, new Uint8Array(this.response),
221
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
23 this.response.byteLength, payload);
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
24 } else {
257
9afafb192180 using PAM
am@osimis.io
parents: 236
diff changeset
25 WasmWebService_NotifyError(callback, url_, payload);
221
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
26 }
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
27 }
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
28 }
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
29
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
30 xhr.send();
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
31 },
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
32
299
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
33 WasmWebService_NewScheduleGetRequest: function(callableSuccess, callableFailure, url, headersInJsonString, payload) {
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
34 // Directly use XMLHttpRequest (no jQuery) to retrieve the raw binary data
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
35 // http://www.henryalgus.com/reading-binary-files-using-jquery-ajax/
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
36 var xhr = new XMLHttpRequest();
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
37 var url_ = UTF8ToString(url);
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
38 var headersInJsonString_ = UTF8ToString(headersInJsonString);
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
39
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
40 xhr.open('GET', url_, true);
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
41 xhr.responseType = 'arraybuffer';
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
42 var headers = JSON.parse(headersInJsonString_);
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
43 for (var key in headers) {
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
44 xhr.setRequestHeader(key, headers[key]);
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
45 }
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
46 //console.log(xhr);
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
47 xhr.onreadystatechange = function() {
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
48 if (this.readyState == XMLHttpRequest.DONE) {
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
49 if (xhr.status === 200) {
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
50 // TODO - Is "new Uint8Array()" necessary? This copies the
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
51 // answer to the WebAssembly stack, hence necessitating
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
52 // increasing the TOTAL_STACK parameter of Emscripten
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
53 console.log("WasmWebService success", this.response);
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
54 WasmWebService_NewNotifySuccess(callableSuccess, url_, new Uint8Array(this.response),
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
55 this.response.byteLength, payload);
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
56 console.log("WasmWebService success 2", this.response);
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
57 } else {
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
58 console.log("WasmWebService failed");
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
59 WasmWebService_NewNotifyError(callableFailure, url_, payload);
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
60 }
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
61 }
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
62 }
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
63
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
64 xhr.send();
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
65 },
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
66
257
9afafb192180 using PAM
am@osimis.io
parents: 236
diff changeset
67 WasmWebService_SchedulePostRequest: function(callback, url, headersInJsonString, body, bodySize, payload) {
221
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
68 var xhr = new XMLHttpRequest();
257
9afafb192180 using PAM
am@osimis.io
parents: 236
diff changeset
69 var url_ = UTF8ToString(url);
9afafb192180 using PAM
am@osimis.io
parents: 236
diff changeset
70 var headersInJsonString_ = UTF8ToString(headersInJsonString);
9afafb192180 using PAM
am@osimis.io
parents: 236
diff changeset
71 xhr.open('POST', url_, true);
221
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
72 xhr.responseType = 'arraybuffer';
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
73 xhr.setRequestHeader('Content-type', 'application/octet-stream');
257
9afafb192180 using PAM
am@osimis.io
parents: 236
diff changeset
74
9afafb192180 using PAM
am@osimis.io
parents: 236
diff changeset
75 var headers = JSON.parse(headersInJsonString_);
9afafb192180 using PAM
am@osimis.io
parents: 236
diff changeset
76 for (var key in headers) {
9afafb192180 using PAM
am@osimis.io
parents: 236
diff changeset
77 xhr.setRequestHeader(key, headers[key]);
9afafb192180 using PAM
am@osimis.io
parents: 236
diff changeset
78 }
221
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
79
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
80 xhr.onreadystatechange = function() {
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
81 if (this.readyState == XMLHttpRequest.DONE) {
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
82 if (xhr.status === 200) {
257
9afafb192180 using PAM
am@osimis.io
parents: 236
diff changeset
83 WasmWebService_NotifySuccess(callback, url_, new Uint8Array(this.response),
221
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
84 this.response.byteLength, payload);
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
85 } else {
257
9afafb192180 using PAM
am@osimis.io
parents: 236
diff changeset
86 WasmWebService_NotifyError(callback, url_, payload);
221
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
87 }
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
88 }
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
89 }
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
90
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
91 xhr.send(new Uint8ClampedArray(HEAPU8.buffer, body, bodySize));
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
92 }
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
93 });