使用 SPDY/http2 在 Express 应用程序中挂断套接字
Socket hang up in express app using SPDY/http2
我有一个正在尝试实现的 express 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 正在处理指定的资产文件。它的性能提升真的非常棒。
但是,当请求被客户端中止时,问题就出现了。在浏览器中快速按 refresh/stop 即可轻松复制。在这种情况下,我收到以下导致我的节点应用程序崩溃的错误:
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)
有没有人知道我可能做错了什么,我应该如何处理中止的请求,或者提供 http2/spdy PUSH_PROMISE Express 支持的另一个模块?
在与 npm-spdy
的开发者交谈后,这似乎目前已损坏。
我有一个正在尝试实现的 express 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 正在处理指定的资产文件。它的性能提升真的非常棒。
但是,当请求被客户端中止时,问题就出现了。在浏览器中快速按 refresh/stop 即可轻松复制。在这种情况下,我收到以下导致我的节点应用程序崩溃的错误:
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)
有没有人知道我可能做错了什么,我应该如何处理中止的请求,或者提供 http2/spdy PUSH_PROMISE Express 支持的另一个模块?
在与 npm-spdy
的开发者交谈后,这似乎目前已损坏。