WebRTC
The RTCDataChannel.close() method closes the
RTCDataChannel. Either peer is permitted to call this method to initiate
closure of the channel.
Closure of the data channel is not instantaneous. Most of the process of closing the connection is handled asynchronously; you can detect when the channel has finished closing by watching for a RTCDataChannel.close_event event on the data channel.
The sequence of events which occurs in response to this method being called:
- RTCDataChannel.readyState is set to
closing. - A background task is established to handle the remainder of the steps below, and
close()returns to the caller. - The transport layer deals with any buffered messages; the protocol layer decides whether to send them or discard them.
- The underlying data transport is closed.
- The RTCDataChannel.readyState property is set to
closed. - If the transport was closed with an error,
the
RTCDataChannelis sent an RTCDataChannel.error_event event with its DOMException.name set toNetworkError. - A RTCDataChannel.close_event event is sent to the channel.
Syntax
close()
Parameters
None.
Return value
None (undefined).
Examples
const pc = new RTCPeerConnection();
const dc = pc.createDataChannel("my channel");
dc.onmessage = (event) => {
console.log(`received: ${event.data}`);
dc.close(); // We decided to close after the first received message
};
dc.onopen = () => {
console.log("datachannel open");
};
dc.onclose = () => {
console.log("datachannel close");
};
// Now negotiate the connection and so forth…
Specifications
Browser compatibility
See also
- WebRTC
- RTCDataChannel
- RTCDataChannel.readyState
- RTCDataChannel.close_event event
来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。
