quic.listen(onsession,[options])


  • onsession quic.OnSessionCallback
  • options quic.SessionOptions
  • 返回:<Promise> 对 quic.QuicEndpoint 的承诺

将端点配置为作为服务器监听。当远程端启动新会话时,将调用所提供的 onsession 回调,并传入创建的会话。

🌐 Configures the endpoint to listen as a server. When a new session is initiated by a remote peer, the given onsession callback will be invoked with the created session.

import { listen } from 'node:quic';

const endpoint = await listen((session) => {
  // ... handle the session
});

// Closing the endpoint allows any sessions open when close is called
// to complete naturally while preventing new sessions from being
// initiated. Once all existing sessions have finished, the endpoint
// will be destroyed. The call returns a promise that is resolved once
// the endpoint is destroyed.
await endpoint.close(); 

默认情况下,每次调用 listen(...) 都会创建一个新的本地 QuicEndpoint 实例,并绑定到一个随机的本地 IP 端口。要指定使用的确切本地地址,或在单个本地端口上复用多个 QUIC 会话,请传递 endpoint 选项,其参数可以是 QuicEndpointEndpointOptions

🌐 By default, every call to listen(...) will create a new local QuicEndpoint instance bound to a new random local IP port. To specify the exact local address to use, or to multiplex multiple QUIC sessions over a single local port, pass the endpoint option with either a QuicEndpoint or EndpointOptions as the argument.

最多,任何单个 QuicEndpoint 只能被配置为以服务器身份监听一次。

🌐 At most, any single QuicEndpoint can only be configured to listen as a server once.