session.sendDatagram(datagram[, encoding])
datagram<string> | <ArrayBufferView> | <Promise>encoding<string> 如果datagram是字符串,要使用的编码。默认:'utf8'。- 返回:<Promise> 用于 <bigint> 数据报 ID。
向远程对等方发送不可靠的数据报,并返回数据报 ID 的承诺。
🌐 Sends an unreliable datagram to the remote peer, returning a promise for the datagram ID.
如果 datagram 是字符串,它将使用指定的 encoding 进行编码。
🌐 If datagram is a string, it will be encoded using the specified encoding.
如果 datagram 是一个 ArrayBufferView,字节将被复制到一个内部缓冲区;调用者的源缓冲区保持不变,并且可以在调用返回后立即重复使用或修改。希望确保其源在调用后不会被修改的调用者(例如,当将缓冲区交给另一个异步消费者时)可以在传递缓冲区之前自己调用 ArrayBuffer.prototype.transfer()。
🌐 If datagram is an ArrayBufferView, the bytes are copied into an
internal buffer; the caller's source buffer is unchanged and may be reused
or mutated immediately after the call returns. Callers that want to ensure
their source cannot be mutated after the call (for example, when handing
the buffer off to another async consumer) can call
ArrayBuffer.prototype.transfer() themselves before passing the buffer.
如果 datagram 是 Promise,则在发送之前会等待。如果在等待时会话关闭,则会静默返回 0n(数据报本质上是不可靠的)。
🌐 If datagram is a Promise, it will be awaited before sending. If the
session closes while awaiting, 0n is returned silently (datagrams are
inherently unreliable).
如果数据报负载长度为零(编码后的空字符串、分离的缓冲区或零长度视图),将返回 0n,并且不会发送任何数据报。
🌐 If the datagram payload is zero-length (empty string after encoding, detached
buffer, or zero-length view), 0n is returned and no datagram is sent.
对于 HTTP/3 会话,对等方必须通过 application: { enableDatagrams: true } 广告 SETTINGS_H3_DATAGRAM=1 才能发送数据报。如果对等方的设置是 0,sendDatagram() 返回 0n(根据 RFC 9297 第 3 条,一端点不得发送 HTTP 数据报,除非对等方表示支持)。
🌐 For HTTP/3 sessions, the peer must advertise SETTINGS_H3_DATAGRAM=1
(via application: { enableDatagrams: true }) for datagrams to be sent.
If the peer's setting is 0, sendDatagram() returns 0n (per RFC 9297
§3, an endpoint MUST NOT send HTTP Datagrams unless the peer indicated
support).
数据报不能被分片——每个数据报必须适合一个 QUIC 数据包。最大数据报大小由对等方的 maxDatagramFrameSize 传输参数决定(对等方在握手期间会通告该参数)。如果对等方将其设置为 0,则不支持数据报,并且会返回 0n。如果数据报超过对等方的限制,它将被静默丢弃,并返回 0n。本端的 maxDatagramFrameSize 传输参数(默认值:1200 字节)控制本端向对等方通告自身最大值的能力。
🌐 Datagrams cannot be fragmented — each must fit within a single QUIC packet.
The maximum datagram size is determined by the peer's
maxDatagramFrameSize transport parameter (which the peer advertises during
the handshake). If the peer sets this to 0, datagrams are not supported
and 0n will be returned. If the datagram exceeds the peer's limit, it
will be silently dropped and 0n returned. The local
maxDatagramFrameSize transport parameter (default: 1200 bytes) controls
what this endpoint advertises to the peer as its own maximum.