Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

RTCDataChannel.onmessage

我们的志愿者还没有将这篇文章翻译为 中文 (简体)加入我们帮助完成翻译!

This is an experimental technology
Because this technology's specification has not stabilized, check the compatibility table for usage in various browsers. Also note that the syntax and behavior of an experimental technology is subject to change in future versions of browsers as the specification changes.

The RTCDataChannel.ommessage property stores an EventHandler which specifies a function to be called when the message event is fired on the channel. This event is represented by the MessageEvent interface. This event is sent to the channel when a message is received from the other peer.

Syntax

RTCDataChannel.onmessage = function;

Value

A function which the browser will call to handle the message event. The function receives as its sole input parameter a MessageEvent object describing the event.

Example

This code snippet creates a peer connection, adds a data channel to it, and starts creating new <p> (paragraph) elements each time a message arrives, with the message's contents displayed inside it. The new elements are then attached to the end of the document.

let pc = new RTCPeerConnection();
let dc = pc.createDataChannel();

dc.onmessage = function(event) {
  var el = document.createElement("p");
  var txtNode = document.createTextNode(event.data);
 
  el.appendChild(txtNode);
  receiveBox.appendChild(el);
}

Specifications

Specification Status Comment
WebRTC 1.0: Real-time Communication Between Browser
The definition of 'RTCDataChannel.onmessage' in that specification.
Working Draft Initial specification.

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support (Yes) 22 (22) [1] No support (Yes) ?
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile Chrome for Android
Basic support No support 28 [2] 22.0 (22) [1] No support No support No support (Yes)

[1] The interface is called DataChannel and not RTCDataChannel in Firefox. However, a binding has been in place since Firefox 24 so that either name will work.

[2] WebRTC, including RTCDataChannel, was introduced in Chrome 28 for Android behind a flag, and enabled by default starting in Chrome 29 for Android.

See also

文档标签和贡献者

 此页面的贡献者: Sheppy, jpmedley, teoli
 最后编辑者: Sheppy,