RTCDataChannel.close()
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 close
event on the data channel.
The sequence of events which occurs in response to this method being called:
-
RTCDataChannel.readyState
is set toclosing
. - 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 toclosed
. - If the transport was closed with an error, the
RTCDataChannel
is sent anerror
event with itsname
set toNetworkError
. - A
close
event is sent to the channel.
Syntax
RTCDataChannel.close();
Parameters
None.
Return value
undefined
.
Example
var pc = new RTCPeerConnection(); var dc = pc.createDataChannel("my channel"); dc.onmessage = function (event) { console.log("received: " + event.data); dc.close(); // We decided to close after the first received message }; dc.onopen = function () { console.log("datachannel open"); }; dc.onclose = function ( console.log("datachannel close"); }; // Now negotiate the connection and so forth...
Specifications
Specification |
---|
WebRTC 1.0: Real-Time Communication Between Browsers (WebRTC 1.0) # dom-rtcdatachannel-close |
Browser compatibility
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
close |
24 |
79 |
22 |
No |
15 |
11 |
≤37 |
25 |
22 |
14 |
11 |
1.5 |
See also
© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/RTCDataChannel/close