blob: 73b9b136ed07a83377f432b6881caad76868762a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
---
title: RTCPeerConnection.ondatachannel
slug: Web/API/RTCPeerConnection/ondatachannel
translation_of: Web/API/RTCPeerConnection/ondatachannel
---
<p>{{APIRef("WebRTC")}}{{SeeCompatTable}}</p>
<p><code><strong>RTCPeerConnection.ondatachannel </strong>属性</code>是一个{{event("Event_handlers", "event handler")}},当这个{{event("datachannel")}}事件在{{domxref("RTCPeerConnection")}}发生时,它指定的那个事件处理函数就会被调用。这个事件继承于 {{domxref("RTCDataChannelEvent")}},当远方伙伴调用{{domxref("RTCPeerConnection.createDataChannel", "createDataChannel()")}}时这个事件被加到这个连接(RTCPeerConnection)中。</p>
<p>在这个事件被收到的同时,这个{{domxref("RTCDataChannel")}} 实际上并没有打开,确保在open这个事件在<code>RTCDataChannel</code>触发以后才去使用它。</p>
<h2 id="语法">语法</h2>
<pre class="syntaxbox"><em>RTCPeerConnection</em>.ondatachannel = <em>function</em>;
</pre>
<h3 id="值">值</h3>
<p>将这个属性设置为接受一个参数的函数:这个参数是一个{{domxref("RTCDataChannelEvent")}},它的channel属性是一个已经创建了的{{domxref("RTCDataChannel")}}对象</p>
<h2 id="示例">示例</h2>
<pre class="brush: 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.');
};
};
</pre>
<h2 id="规范">规范</h2>
<table class="standard-table">
<thead>
<tr>
<th scope="col">Specification</th>
<th scope="col">Status</th>
<th scope="col">Comment</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{ SpecName('WebRTC 1.0', '#dom-rtcpeerconnection-ondatachannel', 'RTCPeerConnection.ondatachannel') }}</td>
<td>{{ Spec2('WebRTC 1.0') }}</td>
<td>Initial specification.</td>
</tr>
</tbody>
</table>
<h2 id="浏览器支持">浏览器支持</h2>
{{Compat("api.RTCPeerConnection.ondatachannel")}}
<h2 id="相关阅读">相关阅读</h2>
<ul>
<li>The {{event("datachannel")}} event and its type, {{domxref("RTCDataChannelEvent")}}.</li>
<li>{{domxref("RTCPeerConnection.createDataChannel()")}}</li>
<li><a href="/en-US/docs/Web/API/WebRTC_API/Simple_RTCDataChannel_sample">A simple RTCDataChannel sample</a></li>
</ul>
|