RTCDataChannel.onmessage - Web APIs 编辑
Experimental
This is an experimental technology
Check the Browser compatibility table carefully before using this in production.
The RTCDataChannel.onmessage
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 Browsers The definition of 'RTCDataChannel.onmessage' in that specification. | Candidate Recommendation | Initial specification. |
Browser compatibility
BCD tables only load in the browser
See also
- WebRTC
RTCPeerConnection
RTCDataChannel
- The
message
event and its type,MessageEvent
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论