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. |
候选推荐 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
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 |
完整支持
不支持
RTCDataChannel
MediaDevices.getUserMedia()
Navigator.mediaDevices
RTCCertificate
RTCDTMFSender
RTCDTMFToneChangeEvent
RTCDataChannelEvent
RTCDtlsTransport
RTCErrorEvent
RTCIceCandidate
RTCIceTransport
RTCPeerConnection
RTCPeerConnectionIceErrorEvent
RTCPeerConnectionIceEvent
RTCRtpReceiver
RTCRtpSender
RTCRtpTransceiver
RTCSctpTransport
RTCSessionDescription
RTCStatsEvent
RTCStatsReport
RTCTrackEvent