使用 Content-Type 读取 http 响应:multipart/x-mixed-replace, Javascript

问题描述

[解决] 可读流

块 (byte[]) 被直接写入响应 outputStream ,在客户端,异步函数中的这段代码在响应块作为 uInt8Array 到达时很好地读取它们

const response = await fetch(url);
const reader = response.body.getReader();

while (true) {
  const { value,done } = await reader.read();
  if (done) break;
  console.log('Received',value);
}

console.log('Response fully received');

https://web.dev/fetch-upload-streaming/ 处读取的代码

[旧]

当该 url 返回内容类型为 multipart/x-mixed-replace 的响应时,我想做与 <img src="stream/video.mjpeg"> 相同的实现。浏览器以某种方式从未完成/正在进行的响应中读取“chucks”。

为什么?我想使用 jmuxer (/example/h264.html) 流式传输 .h264 格式,这里使用了 WebSocket,但“感觉不对”,因为套接字用于双向通信(我认为这是真的)。​​

因此,<img> 成功消费并在服务器端(Java)中生成的响应如下所示:

for(int[] bytes: listWithFrames){
                    responSEOs.write((
                                "--BoundaryString\r\n" +
                                "Content-type: image/jpeg\r\n" +
                                "Content-Length: " +bytes.length +
                                "\r\n\r\n").getBytes());
                    responSEOs.write(bytes);
                    responSEOs.write("\r\n\r\n".getBytes());
                    responSEOs.flush();}

首先,我尝试使用 bytesXMLHttpRequest/Sending_and_Receiving_Binary_Data 一样在响应中写入 XMLHttpRequest,但是 oReq.response 在响应结束时被设置,而 oReq.responseText 不是仅在 oReq.responseType = "text";

时为 null

在 Base64 中编码每个 bytes 使其在 oReq.responseText 上可见,然后解码它也不能这样,因为它增加了很多大小并且 oReq.responseText 永远增长(可能避免通过执行多个请求和缓冲区来继续提供 jmuxer)

我真的需要 Node 吗? making-http-request-and-receiving-multipart-x-mixed-replace-response-in-node-js 我正在寻找某种 fetch,让我可以在收到的每个二进制文件 (arraybuffer/uInt8array) 部分到达时访问它们。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)