我有一些创建套接字的网络代码,我想使用 Node's standard HTTP API 在它们上执行 HTTP .
就这样开始
var http = require('http'),
net = require('net');
var port = 12345;
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end("Hello world");
}).listen(port);
var sock = net.createConnection({port: port});
如何将 sock
传递到 http.get()
或类似内容?
最佳答案
这可以通过自定义 http.Agent 来完成:
var agent = new http.Agent({ maxSockets: 1 });
agent.createSocket = function() {return sock};
http.get({
host: "localhost",
port: port,
path: "/",
agent: agent}, function(res) {
// ...
});
关于node.js - 如何通过已创建的套接字执行 HTTP?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25660913/