blob: eae01846771a8062f415a1bfb064a8506e3813db (
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
---
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("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>
<p>{{ CompatibilityTable}}</p>
<div id="compat-desktop">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Chrome</th>
<th>Firefox (Gecko)</th>
<th>Internet Explorer</th>
<th>Opera</th>
<th>Safari</th>
</tr>
<tr>
<td>Basic support</td>
<td>{{ CompatVersionUnknown}} [1]</td>
<td>{{ CompatGeckoDesktop(18) }} [2]</td>
<td>{{ CompatNo}}</td>
<td>{{ CompatVersionUnknown}}</td>
<td>{{ CompatUnknown}}</td>
</tr>
</tbody>
</table>
</div>
<div id="compat-mobile">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Android</th>
<th>Chrome for Android</th>
<th>Firefox Mobile (Gecko)</th>
<th>IE Mobile</th>
<th>Opera Mobile</th>
<th>Safari Mobile</th>
</tr>
<tr>
<td>Basic support</td>
<td>{{ CompatUnknown}}</td>
<td>{{ CompatUnknown}}</td>
<td>{{ CompatGeckoMobile(22) }} [2]</td>
<td>{{ CompatNo}}</td>
<td>{{ CompatUnknown}}</td>
<td>{{ CompatUnknown}}</td>
</tr>
</tbody>
</table>
</div>
<p>[1] Though this property is not prefixed, the interface it belongs to is.</p>
<p>[2] This property's name isn't prefixed, but the interface it's in, {{domxref("RTCPeerConnection")}}, was prefixed as <code>MozRTCPeerConnection</code> until Firefox 44.</p>
<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>
|