【问题标题】:Streaming exmple using HTTP/2使用 HTTP/2 的流式传输示例
【发布时间】:2021-06-05 10:07:49
【问题描述】:

我希望编写一个数据记录器,它使用 HTTP/2 将数据从客户端流式传输到记录器,以及通过流从记录器中检索数据。 查看 HTTP/2 的 Javascript API 对我来说,如何保持流打开并交换双向流量,直到某个触发事件结束记录对我来说并不明显。 因此,我正在寻找有关如何执行双向交换以实现持久连接的示例(最好是 JavaScript)。

【问题讨论】:

    标签: http2


    【解决方案1】:

    以下代码 sn-ps 构成了我想要实现的基础。

    服务器:

    import http2 from 'http2'
    import fs from 'fs'
    
    // Private key and public certificate for access
    const options = {
        key: fs.readFileSync('private-key.pem'),
        cert: fs.readFileSync('public-cert.pem'),
    };
    
    // Creating and initializing server
    const server = http2.createServer(options);
    
    server.on('error', (error) => {
        console.log('Error: ' + error);
    });
    
    server.on('stream', (stream, requestHeaders) => {
        stream.respond({
            ':status': 200,
            'content-type': 'text/plain'
        });
    
        stream.on('data', (data) => {
            console.log('Received data: ' + data.toString());
            stream.write(data); // echo received data back
        });
    
        stream.on('close', () => {
            console.log('stream closed');
        });
    
        stream.on('end', () => {
            console.log('stream end');
        });
    
    });
    
    server.listen(8000);
    

    客户

    我最初在 POST 请求中指定了 Content_Length,但是当尝试向服务器发送多条消息时,这会产生错误。

    import http2 from 'http2'
    
    let x = 0;
    
    // Creating and initializing client
    const client = http2.connect('http://localhost:8000');
    console.log("Client connected");
    
    const msg1 = 'message 1';
    
    let req = client.request({
        ':method': 'POST',
        ':path': '/',
        'Content-Type': 'text/plain',
    });
    
    req.on('response', (responseHeaders, flags) => {
        console.log("status : " + responseHeaders[":status"]);
    });
    
    req.write(msg1);
    
    req.on('data', (data) => {
        console.log('Received: %s ', data.toString());
    
        req.write("aaa" + x.toString());
        x = x + 1;
        if (x > 10) {
            req.close();
        }
    });
    
    req.on('end', () => {
        client.close(() => {
            console.log("client closed");
        })
    });
    
    req.on('error', (error) => {
        console.log(error);
    })
    

    服务器上的输出是:

    Received data: message 1
    Received data: aaa0
    Received data: aaa1
    Received data: aaa2
    Received data: aaa3
    Received data: aaa4
    Received data: aaa5
    Received data: aaa6
    Received data: aaa7
    Received data: aaa8
    Received data: aaa9
    Received data: aaa10
    stream end
    stream closed
    

    客户端的输出是:

    Client connected
    status : 200
    Received: message 1
    Received: aaa0
    Received: aaa1
    Received: aaa2
    Received: aaa3
    Received: aaa4
    Received: aaa5
    Received: aaa6
    Received: aaa7
    Received: aaa8
    Received: aaa9
    client closed
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-20
      • 1970-01-01
      • 2020-06-15
      • 2011-11-22
      • 2012-01-02
      • 1970-01-01
      • 2019-01-07
      • 1970-01-01
      相关资源
      最近更新 更多