The WebRTC message event is sent to the onmessage event handler on an RTCDataChannel object when a message has been received from the remote peer.

冒泡 No
可取消 No
接口 MessageEvent
事件处理程序特性 onmessage

注意: message event uses as its event object type the MessageEvent interface defined by the HTML specification.

范例

For a given RTCDataChannel , dc , created for a peer connection using its createDataChannel() method, this code sets up a handler for incoming messages and acts on them by adding the data contained within the message to the current document as a new <p> (paragraph) element.

dc.addEventListener("message", ev => {
  let newParagraph = document.createElement("p");
  let textNode = document.createTextNode(event.data);
  newParagraph.appendChild(textNode);
  document.body.appendChild(newParagraph);
}, false);
					

Lines 2-4 create the new paragraph element and add the message data to it as a new text node. Line 6 appends the new paragraph to the end of the document's body.

You can also use an RTCDataChannel 对象的 onmessage event handler property to set the event handler:

dc.onmessage = ev => {
  let newParagraph = document.createElement("p");
  let textNode = document.createTextNode(event.data);
  newParagraph.appendChild(textNode);
  document.body.appendChild(newParagraph);
}
					

规范

规范 状态 注释
WebRTC 1.0: Real-time Communication Between Browsers
The definition of 'the <code>message</code> event' in that specification.
候选推荐

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
message event Chrome 56 Edge ≤79 Firefox Yes IE No Opera 43 Safari Yes WebView Android 56 Chrome Android 56 Firefox Android Yes Opera Android 43 Safari iOS Yes Samsung Internet Android 6.0

图例

完整支持

完整支持

不支持

不支持

另请参阅

元数据

  • 最后修改: