annotate Platforms/Wasm/WasmWebService.js @ 303:ed1a4302154f am-callable-and-promise

new messages in wasm too
author am@osimis.io
date Tue, 18 Sep 2018 18:17:26 +0200
parents 3897f9f28cfa
children 14ef1227120f
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, {
303
ed1a4302154f new messages in wasm too
am@osimis.io
parents: 299
diff changeset
2 WasmWebService_GetAsync: function(callableSuccess, callableFailure, url, headersInJsonString, payload) {
299
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
3 // 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
4 // 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
5 var xhr = new XMLHttpRequest();
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
6 var url_ = UTF8ToString(url);
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
7 var headersInJsonString_ = UTF8ToString(headersInJsonString);
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
8
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
9 xhr.open('GET', url_, true);
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
10 xhr.responseType = 'arraybuffer';
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
11 var headers = JSON.parse(headersInJsonString_);
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
12 for (var key in headers) {
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
13 xhr.setRequestHeader(key, headers[key]);
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
14 }
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
15 //console.log(xhr);
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
16 xhr.onreadystatechange = function() {
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
17 if (this.readyState == XMLHttpRequest.DONE) {
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
18 if (xhr.status === 200) {
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
19 // 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
20 // answer to the WebAssembly stack, hence necessitating
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
21 // increasing the TOTAL_STACK parameter of Emscripten
303
ed1a4302154f new messages in wasm too
am@osimis.io
parents: 299
diff changeset
22 WasmWebService_NotifySuccess(callableSuccess, url_, new Uint8Array(this.response),
299
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
23 this.response.byteLength, payload);
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
24 } else {
303
ed1a4302154f new messages in wasm too
am@osimis.io
parents: 299
diff changeset
25 WasmWebService_NotifyError(callableFailure, url_, payload);
299
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
26 }
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
27 }
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
28 }
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
29
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
30 xhr.send();
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
31 },
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
32
303
ed1a4302154f new messages in wasm too
am@osimis.io
parents: 299
diff changeset
33 WasmWebService_PostAsync: function(callableSuccess, callableFailure, url, headersInJsonString, body, bodySize, payload) {
221
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
34 var xhr = new XMLHttpRequest();
257
9afafb192180 using PAM
am@osimis.io
parents: 236
diff changeset
35 var url_ = UTF8ToString(url);
9afafb192180 using PAM
am@osimis.io
parents: 236
diff changeset
36 var headersInJsonString_ = UTF8ToString(headersInJsonString);
9afafb192180 using PAM
am@osimis.io
parents: 236
diff changeset
37 xhr.open('POST', url_, true);
221
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
38 xhr.responseType = 'arraybuffer';
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
39 xhr.setRequestHeader('Content-type', 'application/octet-stream');
257
9afafb192180 using PAM
am@osimis.io
parents: 236
diff changeset
40
9afafb192180 using PAM
am@osimis.io
parents: 236
diff changeset
41 var headers = JSON.parse(headersInJsonString_);
9afafb192180 using PAM
am@osimis.io
parents: 236
diff changeset
42 for (var key in headers) {
9afafb192180 using PAM
am@osimis.io
parents: 236
diff changeset
43 xhr.setRequestHeader(key, headers[key]);
9afafb192180 using PAM
am@osimis.io
parents: 236
diff changeset
44 }
221
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
45
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
46 xhr.onreadystatechange = function() {
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
47 if (this.readyState == XMLHttpRequest.DONE) {
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
48 if (xhr.status === 200) {
303
ed1a4302154f new messages in wasm too
am@osimis.io
parents: 299
diff changeset
49 WasmWebService_NotifySuccess(callableSuccess, url_, new Uint8Array(this.response),
221
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
50 this.response.byteLength, payload);
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
51 } else {
303
ed1a4302154f new messages in wasm too
am@osimis.io
parents: 299
diff changeset
52 WasmWebService_NotifyError(callableFailure, url_, payload);
221
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
53 }
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
54 }
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
55 }
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
56
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
57 xhr.send(new Uint8ClampedArray(HEAPU8.buffer, body, bodySize));
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
58 }
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
59 });