RTCPeerConnection.ondatachannel
>RTCPeerConnection.ondatachannel 属性是一个事件处理器,当 datachannel 事件在 RTCPeerConnection 发生时,它指定的那个事件处理函数就会被调用。这个事件继承于 RTCDataChannelEvent,当远方伙伴调用 createDataChannel() 时这个事件被加到这个连接(RTCPeerConnection)中。
在这个事件被收到的同时,这个 RTCDataChannel 实际上并没有打开,确保在 open 这个事件在RTCDataChannel触发以后才去使用它。
语法
RTCPeerConnection.ondatachannel = function;
值
将这个属性设置为接受一个参数的函数:这个参数是一个RTCDataChannelEvent,它的 channel 属性是一个已经创建了的RTCDataChannel对象
示例
js
pc.ondatachannel = function (ev) {
console.log("Data channel is created!");
ev.channel.onopen = function () {
console.log("Data channel is open and ready to be used.");
};
};
规范
| 规范 |
|---|
| WebRTC: Real-Time Communication in Browsers> # dom-rtcpeerconnection-ondatachannel> |