|
7 | 7 | <link href="./lib/weui.min.css" rel="stylesheet"/> |
8 | 8 | <link href="./lib/demo.css" rel="stylesheet"/> |
9 | 9 |
|
10 | | - <script src="../dist/vconsole.min.js"></script> |
11 | 10 | <script src="https://unpkg.com/axios/dist/axios.min.js"></script> |
| 11 | + <script src="../dist/vconsole.min.js"></script> |
12 | 12 | </head> |
13 | 13 | <body ontouchstart> |
14 | 14 | <div class="page"> |
|
19 | 19 | <a onclick="postAjax()" href="javascript:;" class="weui_btn weui_btn_default">POST XHR (Data: Object)</a> |
20 | 20 | <a onclick="postAjax('json')" href="javascript:;" class="weui_btn weui_btn_default">POST XHR (Data: JSON String)</a> |
21 | 21 | <a onclick="optionsXHR()" href="javascript:;" class="weui_btn weui_btn_default">OPTIONS XHR</a> |
| 22 | + <a onclick="xhrStream()" href="javascript:;" class="weui_btn weui_btn_default">XHR Stream</a> |
22 | 23 | </div> |
23 | 24 |
|
24 | 25 | <div class="section"> |
|
33 | 34 | <a onclick="postFetchByRequest()" href="javascript:;" class="weui_btn weui_btn_default">POST Fetch Using XHR</a> |
34 | 35 | <a onclick="optionsFetch()" href="javascript:;" class="weui_btn weui_btn_default">OPTIONS Fetch</a> |
35 | 36 | <a onclick="fetchIOSLowBug()" href="javascript:;" class="weui_btn weui_btn_default">Fetch iOS LowBug</a> |
| 37 | + <a onclick="fetchStream()" href="javascript:;" class="weui_btn weui_btn_default">Fetch Steam</a> |
36 | 38 | </div> |
37 | 39 |
|
38 | 40 | <div class="section"> |
|
131 | 133 | } |
132 | 134 |
|
133 | 135 | function getFetch() { |
| 136 | + vConsole.show(); |
134 | 137 | window.fetch('./data/success.json?method=fetchGet&id=' + Math.random(), { |
135 | 138 | method: 'GET', |
136 | 139 | headers: { |
137 | 140 | 'custom-header': 'foobar', |
138 | 141 | 'content-type': 'application/json' |
139 | 142 | }, |
140 | 143 | }).then((data) => { |
141 | | - return data.json(); |
| 144 | + console.log('getFetch() response:', data); |
| 145 | + setTimeout(() => { |
| 146 | + data.json().then((res) => { |
| 147 | + console.log(res); |
| 148 | + }); |
| 149 | + }, 3000); |
| 150 | + // return data; |
| 151 | + // return data.json(); |
142 | 152 | }).then((data) => { |
143 | | - console.log('get Fetch Response:', data); |
| 153 | + // console.log('getFetch() json:', data); |
144 | 154 | }); |
145 | 155 | } |
146 | 156 |
|
|
161 | 171 | }); |
162 | 172 | } |
163 | 173 |
|
164 | | - |
165 | 174 | function getFetchSimple() { |
166 | 175 | window.fetch('./data/large.json?type=fetchGet&id=' + Math.random()).then((data) => { |
167 | 176 | return data.json(); |
|
256 | 265 | }); |
257 | 266 | } |
258 | 267 |
|
| 268 | +function fetchStream() { |
| 269 | + vConsole.show(); |
| 270 | + window.fetch('./data/stream.flv?id=' + Math.random()).then((response) => { |
| 271 | + console.log('then response', 'bodyUsed:', response.bodyUsed, 'locked:', response.body.locked); |
| 272 | + // console.log(response.text()) |
| 273 | + // return; |
| 274 | + const reader = response.body.getReader(); |
| 275 | + console.log('then response', 'bodyUsed:', response.bodyUsed, 'locked:', response.body.locked); |
| 276 | + let bytesReceived = 0; |
| 277 | + |
| 278 | + return reader.read().then(function process(result) { |
| 279 | + console.log('reader.read', 'bodyUsed:', response.bodyUsed, 'locked:', response.body.locked); |
| 280 | + if (result.done) { |
| 281 | + console.log('Failed to find match'); |
| 282 | + return; |
| 283 | + } |
| 284 | + |
| 285 | + bytesReceived += result.value.length; |
| 286 | + console.log(`Received ${bytesReceived} bytes.`); |
| 287 | + |
| 288 | + if (bytesReceived > 3000000) { |
| 289 | + reader.cancel(); |
| 290 | + console.log('Cancel.', response.status); |
| 291 | + return; |
| 292 | + } |
| 293 | + |
| 294 | + return reader.read().then(process); |
| 295 | + }); |
| 296 | + }); |
| 297 | +} |
| 298 | + |
| 299 | +function xhrStream() { |
| 300 | + vConsole.show(); |
| 301 | + const url = './data/stream.flv?id=' + Math.random(); |
| 302 | + const xhr = new XMLHttpRequest(); |
| 303 | + xhr.timeout = 11000; |
| 304 | + console.log('xhr type:', typeof xhr, xhr instanceof XMLHttpRequest); |
| 305 | + xhr.open('GET', url); |
| 306 | + xhr.send(); |
| 307 | + xhr.onreadystatechange = () => { |
| 308 | + console.log('XHR onreadystatechange:', 'readyState:', xhr.readyState, 'responseType:', xhr.responseType); |
| 309 | + }; |
| 310 | + xhr.onprogress = (e) => { |
| 311 | + // console.log('XHR onprogress:', 'readyState:', xhr.readyState, 'status:', xhr.status, 'loaded:', e.loaded, 'timeStamp:', e.timeStamp); |
| 312 | + if (e.loaded > 3000000) { |
| 313 | + xhr.abort(); |
| 314 | + } |
| 315 | + }; |
| 316 | + xhr.onloadstart = (e) => { |
| 317 | + // console.log('XHR onloadstart:', e); |
| 318 | + }; |
| 319 | + xhr.onloadend = (e) => { |
| 320 | + // console.log('XHR onloadend:', 'readyState:', xhr.readyState, xhr.status, e); |
| 321 | + }; |
| 322 | + xhr.onload = (e) => { |
| 323 | + console.log('XHR onload:', 'readyState:', xhr.readyState, xhr.status, xhr.responseType); |
| 324 | + }; |
| 325 | + xhr.onerror = (e) => { |
| 326 | + console.log('XHR onerror:', e); |
| 327 | + }; |
| 328 | + xhr.onabort = (e) => { |
| 329 | + console.log('XHR onabort:', xhr.readyState, xhr.status, e); |
| 330 | + }; |
| 331 | + xhr.ontimeout = (e) => { |
| 332 | + console.log('XHR ontimeout:', e); |
| 333 | + } |
| 334 | +} |
| 335 | + |
259 | 336 | function postImage() { |
260 | 337 | console.info('postImage() Start, response should be logged after End'); |
261 | 338 | const xhr = new XMLHttpRequest(); |
|
269 | 346 | } |
270 | 347 |
|
271 | 348 | function sendBeacon() { |
| 349 | + vConsole.show(); |
272 | 350 | console.info('sendBeacon() Start, response should be logged after End'); |
273 | 351 | window.navigator.sendBeacon('./data/success.json?method=beacon', JSON.stringify({ |
274 | 352 | foo: 'bar', |
|
297 | 375 | console.info('axiosRequest() End'); |
298 | 376 | } |
299 | 377 |
|
300 | | -function sendBeacon() { |
301 | | - console.info('sendBeacon() Start, response should be logged after End'); |
302 | | - window.navigator.sendBeacon('./data/success.json?method=beacon', JSON.stringify({ |
303 | | - foo: 'bar', |
304 | | - id: Math.random(), |
305 | | - type: 'sendBeacon' |
306 | | - })); |
307 | | - console.info('sendBeacon() End'); |
308 | | -} |
309 | | - |
310 | 378 | function axiosRequest(method) { |
311 | 379 | console.info('axiosRequest() Start'); |
312 | 380 | axios({ |
|
0 commit comments