📌  相关文章
📜  Node.js Http2ServerResponse.socket 方法(1)

📅  最后修改于: 2023-12-03 15:03:13.412000             🧑  作者: Mango

Node.js Http2ServerResponse.socket 方法

Http2ServerResponse.socket 方法是 Node.js 中 Http2 模块中的一个方法,用于获取与响应关联的 TCP 套接字。具体用法和返回值如下:

用法
response.socket
参数

返回值

<net.Socket> - 与响应关联的 TCP 套接字。

示例
const http2 = require('http2');
const fs = require('fs');

const server = http2.createSecureServer({
  key: fs.readFileSync('localhost-privkey.pem'),
  cert: fs.readFileSync('localhost-cert.pem')
});

server.on('stream', (stream, headers) => {
  stream.respond({
    'content-type': 'text/html',
    ':status': 200
  });
  stream.end('<html><body>Hello World!</body></html>');
  
  console.log(stream.session.socket); // 获取响应关联的 TCP 套接字
});

server.listen(8443);

运行以上示例代码,启动 Http2 服务器后,在客户端访问 https://localhost:8443/ 可以看到 'Hello World!' 字符串。同时在服务器端的控制台输出中,我们可以看到如下输出:

net.Socket {
  _connecting: false,
  _hadError: false,
  _handle: TCP {
    reading: false,
    onconnection: null,
    [Symbol(owner)]: [Circular]
  },
  _parent: null,
  _host: null,
  _readableState: ReadableState {
    objectMode: false,
    highWaterMark: 65536,
    buffer: BufferList { head: [Object], tail: [Object], length: 1 },
    length: 192,
    pipes: null,
    flowing: null,
    ended: true,
    endEmitted: false,
    reading: false,
    sync: true,
    needReadable: false,
    emittedReadable: false,
    readableListening: false,
    resumeScheduled: false,
    emitClose: true,
    autoDestroy: false,
    destroyed: false,
    defaultEncoding: 'utf8',
    awaitDrain: 0,
    readingMore: false,
    decoder: null,
    encoding: null
  },
  readable: false,
  _events: [Object: null prototype] {
    end: [Function (anonymous)]
  },
  _eventsCount: 1,
  _maxListeners: undefined,
  _writableState: WritableState {
    objectMode: false,
    highWaterMark: 16384,
    finalCalled: true,
    needDrain: false,
    ending: true,
    ended: true,
    finished: true,
    destroyed: false,
    decodeStrings: true,
    defaultEncoding: 'utf8',
    length: 0,
    writing: false,
    corked: 0,
    sync: false,
    bufferProcessing: false,
    onwrite: [Function (anonymous)],
    writecb: null,
    writelen: 0,
    afterWriteTickInfo: null,
    bufferedRequest: null,
    lastBufferedRequest: null,
    pendingcb: 0,
    prefinished: true,
    errorEmitted: false,
    emitClose: true,
    autoDestroy: false,
    bufferedRequestCount: 0,
    corkedRequestsFree: {
      next: null,
      entry: null,
      finish: [Function (anonymous)]
    }
  },
  writable: false,
  allowHalfOpen: false,
  _sockname: null,
  _peername: null,
  _pendingData: null,
  _pendingEncoding: '',
  server: null,
  _server: null,
  ssl: null,
  _requestCert: false,
  _rejectUnauthorized: false,
  parser: null,
  _httpMessage: null,
  _httpMessageRes: null,
  [Symbol(asyncId)]: 25,
  [Symbol(kHandle)]: TCP {
    reading: false,
    onconnection: null,
    [Symbol(owner)]: [Circular]
  },
  [Symbol(lastWriteQueueSize)]: 0,
  [Symbol(timeout)]: null,
  [Symbol(kBuffer)]: null,
  [Symbol(kBufferCb)]: null,
  [Symbol(position)]: 0
}

从输出中我们可以看到,response.socket 方法返回的是一个 net.Socket 对象,该对象包含了套接字的详细信息,可以用于实现 TCP 连接相关的功能。

注意: response.socket 方法返回的对象是 net.Socket 类型的对象,而不是 tls.TLSSocket 类型的对象,因为 Http2 模块使用的是 TCP 套接字,而非 HTTPS。如果需要获取客户端请求的详细信息,可以使用 Node.js 的 IncomingMessage.connection 属性,该属性返回的是一个 tls.TLSSocket 类型的对象。