如何在已创建的套接字上执行HTTP?

时间:2014-09-04 08:38:43

标签: node.js

我有一些创建套接字的网络代码,我想使用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()或类似的?

1 个答案:

答案 0 :(得分:1)

可以使用自定义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) {
        // ...
    });
相关问题