套接字使用SPDY / http2挂在快递应用程序中

时间:2016-01-19 03:13:37

标签: node.js ssl express http2 spdy

我有一个快递4应用程序,我试图实现http2 / SPDY。

www(创建服务器的地方):

var spdy         = require('spdy');

var options = {
    /* certs */
}

var server = spdy.createServer(options,app).listen(8081);

index.js:

var asset        = fs.readFileSync("public/js/asset.js");

router.get('/test', function(req,res) {

    if (req.isSpdy) {
        var headers = {'content-type': 'application/javascript'};
        res.push('/js/asset.js', headers, function(err,stream) {
            if (err) {
                console.log(err);
                return;
            }
            stream.end(asset);
        });
    }
    res.render('test', {}); 
});

现在这一切在正常使用场景中完美无缺。使用SPDY indicator plugin我可以看到我的网站是通过HTTP / 2提供的。我也能看到PUSH_PROMISES适用于指定的资产文件。它的性能提升非常棒。

但是,当客户端中止请求时会出现问题。通过在浏览器中快速按下刷新/停止,可以轻松复制。在这种情况下,我收到以下错误,导致我的节点应用程序崩溃:

events.js:141
      throw er; // Unhandled 'error' event
      ^

Error: socket hang up
    at TLSSocket.onclose (/Users/colevarner/fullpower/node/motionxlive/node_modules/spdy/node_modules/spdy-transport/lib/spdy-transport/connection.js:186:15)
    at TLSSocket.g (events.js:260:16)
    at emitOne (events.js:82:20)
    at TLSSocket.emit (events.js:169:7)
    at Immediate._onImmediate (net.js:469:12)
    at processImmediate [as _immediateCallback] (timers.js:374:17)

如果我注释掉res.push()次调用,则代码按预期工作,没有错误。

我给了npm spdy-push包装器模块(它说它处理连接关闭)一次尝试,但它根本不起作用。如文档所示设置时,会导致以下错误:

/Users/colevarner/fullpower/node/motionxlive/node_modules/spdy/node_modules/spdy-transport/lib/spdy-transport/stream.js:662
  return callback(err, push);
         ^

TypeError: callback is not a function
    at /Users/colevarner/fullpower/node/motionxlive/node_modules/spdy/node_modules/spdy-transport/lib/spdy-transport/stream.js:662:14
    at /Users/colevarner/fullpower/node/motionxlive/node_modules/spdy/node_modules/spdy-transport/lib/spdy-transport/connection.js:833:5
    at Object.callback (/Users/colevarner/fullpower/node/motionxlive/node_modules/spdy/node_modules/spdy-transport/lib/spdy-transport/stream.js:463:5)
    at Framer.tickSync (/Users/colevarner/fullpower/node/motionxlive/node_modules/spdy/node_modules/spdy-transport/lib/spdy-transport/protocol/base/scheduler.js:130:12)
    at Framer.tick (/Users/colevarner/fullpower/node/motionxlive/node_modules/spdy/node_modules/spdy-transport/lib/spdy-transport/protocol/base/scheduler.js:108:13)
    at /Users/colevarner/fullpower/node/motionxlive/node_modules/spdy/node_modules/spdy-transport/lib/spdy-transport/protocol/base/scheduler.js:102:10
    at doNTCallback0 (node.js:407:9)
    at process._tickCallback (node.js:336:13)

有没有人对我可能做错了什么,我应该如何处理已中止的请求,或者为Express提供http2 / spdy PUSH_PROMISE支持的其他模块有任何想法?

1 个答案:

答案 0 :(得分:0)

在与npm-spdy的开发人员交谈之后,它似乎已经破了。