我試圖使用fetch和ReadableStream。在這個例子中,ReadableStream應該無限地重複「一些數據...」。使用ReadableStream獲取
fetch('/', {
method: 'POST',
body: new ReadableStream({
pull: function(controller) {
console.log('pull called!');
controller.enqueue('Some data...');
}
})
});
這是行不通的。雖然pull
只執行一次,但沒有數據在請求主體中發送。
POST/HTTP/1.1
Host: example.com
Connection: keep-alive
Content-Length: 0
Origin: https://example.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36
Accept: */*
Referer: https://example.com/
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.8
我該如何製作一個ReadableStream(或任何一種可以寫動態數據的流)可用於獲取?
或者,如果這還不可能,請您註明這一點?謝謝。
注:這是一個更具體的分拆問題:Method for streaming data from browser to server via HTTP
https://github.com/whatwg/fetch/issues/439 – guest271314