gpt4 book ai didi

node.js - 使用 node.js http2 模块的服务器发送事件 - 我应该如何将它与流/pushStream 一起使用?

转载 作者:行者123 更新时间:2023-12-03 12:15:27 26 4
gpt4 key购买 nike

我正在尝试实现每 500 毫秒发送大量数据(对象,而不是文件)的服务器。
经过一番阅读,服务器通过 http2 发送的事件似乎是最快的选择(由于 http2 是二进制协议(protocol),SSE 减少了流量开销)

玩了一下SSE在 http/1.1
我一直在尝试在 http2 上做同样的事情。我试过用 stream 这样做和 pushStream ,但没有成功。但是,使用与 http/1.1 相同的方式似乎可行。

我的问题是 - 为什么使用流的服务器 1(见下文)不起作用,而服务器 2 似乎工作正常?工作 Node 流时我错过了什么吗?

我正在使用 Node v10.9.0和 Chrome 68.0.3440.106
我已阅读以下问题和帖子,但仍然无法解决此问题:

  • Can I stream a response back to the browser using http2?
  • Node.js HTTP2 server Error: socket hang up
  • HTTP2 / SPDY Push-Stream Verification: How to Test?
  • HTTP/2 Server Push with Node.js
  • Using Server Sent Events with express

  • 服务器 1 - 带有流的 http2(不工作 - 客户端没有收到事件。chrome 将请求描述为未完成的请求):
    const fs = require('fs');
    const http2 = require('http2');

    const HTTPSoptions = {
    key: fs.readFileSync('./cert/selfsigned.key'),
    cert: fs.readFileSync('./cert/selfsigned.crt'),
    };
    const template = `
    <!DOCTYPE html>
    <html>
    <body>
    <script type="text/javascript">
    const source = new EventSource('/sse/');
    source.onmessage = function(e) {
    document.body.innerHTML += e.data + '<br>';
    };
    </script>
    </body>
    </html>
    `;


    const server = http2.createSecureServer(HTTPSoptions);

    server.on('stream', (stream, headers, flags) => {
    if (stream.url === 'sse/') {
    console.log(stream.respond);
    stream.respond({
    ':status': 200,
    'content-type': 'text/event-stream'
    });
    setInterval(() => stream ? res.write(`data: ${Math.random()}\n\n`) : '', 200);
    }
    });

    server.on('request', (req, res) => {
    if(req.url === '/') {
    res.end(template);
    }
    });



    server.listen(3001);

    服务器 2 - 带有流的 http2(工作正常):
    const fs = require('fs');
    const http2 = require('http2');

    const HTTPSoptions = {
    key: fs.readFileSync('./cert/selfsigned.key'),
    cert: fs.readFileSync('./cert/selfsigned.crt'),
    };
    const template = `
    <!DOCTYPE html>
    <html>
    <body>
    <script type="text/javascript">
    const source = new EventSource('/sse/');
    source.onmessage = function(e) {
    document.body.innerHTML += e.data + '<br>';
    };
    </script>
    </body>
    </html>
    `;


    const server = http2.createSecureServer(HTTPSoptions);

    server.on('request', (req, res) => {
    req.socket.setKeepAlive(true);

    if(req.url === '/sse/') {

    res.writeHead(200, {
    'Content-Type': 'text/event-stream', // <- Important headers
    'Cache-Control': 'no-cache'
    });
    res.write('\n');

    setInterval(() => res.write(`data: ${Math.random()}\n\n`), 200);
    } else {
    res.end(template);
    }
    });

    server.listen(3001);

    最佳答案

    要获取 http2 流的请求路径,您必须查看 :path header (docs):

    if (headers[':path'] === '/sse/') {

    您还尝试使用 res.write , 而 res应该是 stream .

    这是一个基于您的“服务器 1”实现的工作处理程序函数:

    server.on('stream', (stream, headers, flags) => {
    if (headers[':path'] === '/sse/') {
    stream.respond({
    ':status': 200,
    'content-type': 'text/event-stream'
    });
    setInterval(() => stream.write(`data: ${Math.random()}\n\n`), 200);
    }
    });

    关于node.js - 使用 node.js http2 模块的服务器发送事件 - 我应该如何将它与流/pushStream 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52086683/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com