Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

RTCPeerConnection: datachannel event

BaselineWidely available

Adatachannel event is sent to anRTCPeerConnection instance when anRTCDataChannel has been added to the connection, as a result of the remote peer callingRTCPeerConnection.createDataChannel().

Note:This event isnot dispatched when the local end of the connection creates the channel.

This event is not cancelable and does not bubble.

Syntax

Use the event name in methods likeaddEventListener(), or set an event handler property.

js
addEventListener("datachannel", (event) => { })ondatachannel = (event) => { }

Event type

Event properties

Also inherits properties fromEvent.

channelRead only

Returns theRTCDataChannel associated with the event.

Examples

This example sets up a function that handlesdatachannel events by gathering the information needed to communicate with the newly addedRTCDataChannel and by adding event handlers for the events that occur on that channel.

js
pc.addEventListener(  "datachannel",  (ev) => {    receiveChannel = ev.channel;    receiveChannel.onmessage = myHandleMessage;    receiveChannel.onopen = myHandleOpen;    receiveChannel.onclose = myHandleClose;  },  false,);

receiveChannel is set to the value of the event'schannel property, which specifies theRTCDataChannel object representing the data channel linking the remote peer to the local one.

This same code can also instead use theRTCPeerConnection interface'sondatachannel event handler property, like this:

js
pc.ondatachannel = (ev) => {  receiveChannel = ev.channel;  receiveChannel.onmessage = myHandleMessage;  receiveChannel.onopen = myHandleOpen;  receiveChannel.onclose = myHandleClose;};

Specifications

Specification
WebRTC: Real-Time Communication in Browsers
# dom-rtcpeerconnection-ondatachannel

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp