Mercurial > hg > orthanc-stone
annotate Platforms/Wasm/WasmWebService.js @ 417:aee3d7941c9b
preparing to load images using DICOMweb
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 15 Nov 2018 17:28:15 +0100 |
parents | aad37d0b6407 |
children | d638d5721f1c |
rev | line source |
---|---|
221
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
1 mergeInto(LibraryManager.library, { |
309 | 2 WasmWebService_GetAsync: function(callableSuccess, callableFailure, url, headersInJsonString, payload, timeoutInSeconds) { |
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'; |
309 | 11 xhr.timeout = timeoutInSeconds * 1000; |
299
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
12 var headers = JSON.parse(headersInJsonString_); |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
13 for (var key in headers) { |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
14 xhr.setRequestHeader(key, headers[key]); |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
15 } |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
16 //console.log(xhr); |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
17 xhr.onreadystatechange = function() { |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
18 if (this.readyState == XMLHttpRequest.DONE) { |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
19 if (xhr.status === 200) { |
417
aee3d7941c9b
preparing to load images using DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
315
diff
changeset
|
20 var s = xhr.getAllResponseHeaders(); |
aee3d7941c9b
preparing to load images using DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
315
diff
changeset
|
21 var headers = _malloc(s.length + 1); |
aee3d7941c9b
preparing to load images using DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
315
diff
changeset
|
22 writeStringToMemory(s, headers); |
aee3d7941c9b
preparing to load images using DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
315
diff
changeset
|
23 |
299
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
24 // 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
|
25 // answer to the WebAssembly stack, hence necessitating |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
26 // increasing the TOTAL_STACK parameter of Emscripten |
303 | 27 WasmWebService_NotifySuccess(callableSuccess, url_, new Uint8Array(this.response), |
417
aee3d7941c9b
preparing to load images using DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
315
diff
changeset
|
28 this.response.byteLength, headers, payload); |
299
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
29 } else { |
303 | 30 WasmWebService_NotifyError(callableFailure, url_, payload); |
299
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 } |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
33 } |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
34 |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
35 xhr.send(); |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
36 }, |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
257
diff
changeset
|
37 |
309 | 38 WasmWebService_PostAsync: function(callableSuccess, callableFailure, url, headersInJsonString, body, bodySize, payload, timeoutInSeconds) { |
221
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
39 var xhr = new XMLHttpRequest(); |
257 | 40 var url_ = UTF8ToString(url); |
41 var headersInJsonString_ = UTF8ToString(headersInJsonString); | |
42 xhr.open('POST', url_, true); | |
309 | 43 xhr.timeout = timeoutInSeconds * 1000; |
221
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
44 xhr.responseType = 'arraybuffer'; |
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
45 xhr.setRequestHeader('Content-type', 'application/octet-stream'); |
257 | 46 |
47 var headers = JSON.parse(headersInJsonString_); | |
48 for (var key in headers) { | |
49 xhr.setRequestHeader(key, headers[key]); | |
50 } | |
221
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
51 |
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
52 xhr.onreadystatechange = function() { |
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
53 if (this.readyState == XMLHttpRequest.DONE) { |
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
54 if (xhr.status === 200) { |
417
aee3d7941c9b
preparing to load images using DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
315
diff
changeset
|
55 var s = xhr.getAllResponseHeaders(); |
aee3d7941c9b
preparing to load images using DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
315
diff
changeset
|
56 var headers = _malloc(s.length + 1); |
aee3d7941c9b
preparing to load images using DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
315
diff
changeset
|
57 writeStringToMemory(s, headers); |
aee3d7941c9b
preparing to load images using DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
315
diff
changeset
|
58 |
303 | 59 WasmWebService_NotifySuccess(callableSuccess, url_, new Uint8Array(this.response), |
417
aee3d7941c9b
preparing to load images using DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
315
diff
changeset
|
60 this.response.byteLength, headers, payload); |
221
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
61 } else { |
303 | 62 WasmWebService_NotifyError(callableFailure, url_, payload); |
221
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
63 } |
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
64 } |
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
65 } |
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
66 |
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
67 xhr.send(new Uint8ClampedArray(HEAPU8.buffer, body, bodySize)); |
315
aad37d0b6407
Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents:
309
diff
changeset
|
68 }, |
aad37d0b6407
Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents:
309
diff
changeset
|
69 |
aad37d0b6407
Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents:
309
diff
changeset
|
70 WasmWebService_DeleteAsync: function(callableSuccess, callableFailure, url, headersInJsonString, payload, timeoutInSeconds) { |
aad37d0b6407
Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents:
309
diff
changeset
|
71 var xhr = new XMLHttpRequest(); |
aad37d0b6407
Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents:
309
diff
changeset
|
72 var url_ = UTF8ToString(url); |
aad37d0b6407
Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents:
309
diff
changeset
|
73 var headersInJsonString_ = UTF8ToString(headersInJsonString); |
aad37d0b6407
Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents:
309
diff
changeset
|
74 xhr.open('DELETE', url_, true); |
aad37d0b6407
Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents:
309
diff
changeset
|
75 xhr.timeout = timeoutInSeconds * 1000; |
aad37d0b6407
Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents:
309
diff
changeset
|
76 xhr.responseType = 'arraybuffer'; |
aad37d0b6407
Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents:
309
diff
changeset
|
77 xhr.setRequestHeader('Content-type', 'application/octet-stream'); |
aad37d0b6407
Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents:
309
diff
changeset
|
78 |
aad37d0b6407
Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents:
309
diff
changeset
|
79 var headers = JSON.parse(headersInJsonString_); |
aad37d0b6407
Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents:
309
diff
changeset
|
80 for (var key in headers) { |
aad37d0b6407
Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents:
309
diff
changeset
|
81 xhr.setRequestHeader(key, headers[key]); |
aad37d0b6407
Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents:
309
diff
changeset
|
82 } |
aad37d0b6407
Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents:
309
diff
changeset
|
83 |
aad37d0b6407
Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents:
309
diff
changeset
|
84 xhr.onreadystatechange = function() { |
aad37d0b6407
Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents:
309
diff
changeset
|
85 if (this.readyState == XMLHttpRequest.DONE) { |
aad37d0b6407
Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents:
309
diff
changeset
|
86 if (xhr.status === 200) { |
417
aee3d7941c9b
preparing to load images using DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
315
diff
changeset
|
87 var s = xhr.getAllResponseHeaders(); |
aee3d7941c9b
preparing to load images using DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
315
diff
changeset
|
88 var headers = _malloc(s.length + 1); |
aee3d7941c9b
preparing to load images using DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
315
diff
changeset
|
89 writeStringToMemory(s, headers); |
aee3d7941c9b
preparing to load images using DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
315
diff
changeset
|
90 |
315
aad37d0b6407
Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents:
309
diff
changeset
|
91 WasmWebService_NotifySuccess(callableSuccess, url_, new Uint8Array(this.response), |
417
aee3d7941c9b
preparing to load images using DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
315
diff
changeset
|
92 this.response.byteLength, headers, payload); |
315
aad37d0b6407
Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents:
309
diff
changeset
|
93 } else { |
aad37d0b6407
Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents:
309
diff
changeset
|
94 WasmWebService_NotifyError(callableFailure, url_, payload); |
aad37d0b6407
Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents:
309
diff
changeset
|
95 } |
aad37d0b6407
Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents:
309
diff
changeset
|
96 } |
aad37d0b6407
Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents:
309
diff
changeset
|
97 } |
aad37d0b6407
Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents:
309
diff
changeset
|
98 |
aad37d0b6407
Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents:
309
diff
changeset
|
99 xhr.send(); |
221
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
100 } |
315
aad37d0b6407
Added LayerWidget::RemoveLayer + DELETE commands in WebService
am@osimis.io
parents:
309
diff
changeset
|
101 |
221
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
102 }); |