RTCPeerConnection.createDataChannel()
åºçº¿
广æ³å¯ç¨
èª 2020å¹´1æ èµ·ï¼æ¤ç¹æ§å·²å¨ä¸»æµæµè§å¨ä¸å¾å°æ¯æï¼å¯å¨å¤§å¤æ°è®¾å¤åæµè§å¨çæ¬ä¸æ£å¸¸ä½¿ç¨ã
å®éªæ§: è¿æ¯ä¸é¡¹å®éªæ§ææ¯
å¨å°å
¶ç¨äºç产ä¹åï¼è¯·ä»ç»æ£æ¥æµè§å¨å
¼å®¹æ§è¡¨æ ¼ã
RTCPeerConnection ç createDataChannel() æ¹æ³å建ä¸ä¸ªå¯ä»¥åéä»»ææ°æ®çæ°æ®éé (data channel)ã常ç¨äºåå°ä¼ è¾å
容ï¼ä¾å¦ï¼å¾åï¼æä»¶ä¼ è¾ï¼è天æåï¼æ¸¸ææ°æ®æ´æ°å
ï¼ççã
åºäºæä¸ªè¿æ¥å建第ä¸ä¸ª data channel æ¶ï¼ä¼éè¿åéä¸ä¸ª negotiationneeded äºä»¶æ¥å¼å§éæ°è°å¤ï¼renegotiationï¼ã
è¯æ³
createDataChannel(label)
createDataChannel(label, options)
åæ°
label-
ä¸ä¸ªä¾¿äºçè§£çééåã该å符串ä¸è½é¿äº 65,535 åè.
optionså¯é-
æä¾ data channel 设置çä¸ä¸ª
RTCDataChannelInitdictionary
RTCDataChannelInit dictionary
RTCDataChannelInit åå
¸æä¾ä»¥ä¸å段ï¼ç¨ä»¥æé å¯éç options åæ°æ¥è®¾ç½® data channel ä»¥æ»¡è¶³ä½ çéæ±ï¼
orderedå¯é-
表示éè¿
RTCDataChannelçä¿¡æ¯çå°è¾¾é¡ºåºéè¦ååé顺åºä¸è´ (true), æè å°è¾¾é¡ºåºä¸éè¦ååé顺åºä¸è´ (false). é»è®¤ï¼true. maxPacketLifeTimeå¯é-
The maximum number of milliseconds that attempts to transfer a message may take in unreliable mode. While this value is a 16-bit unsigned number, each user agent may clamp it to whatever maximum it deems appropriate. Default:
null. maxRetransmitså¯é-
The maximum number of times the user agent should attempt to retransmit a message which fails the first time in unreliable mode. While this value is a16-bit unsigned number, each user agent may clamp it to whatever maximum it deems appropriate. Default:
null. protocolå¯é-
The name of the sub-protocol being used on the
RTCDataChannel, if any; otherwise, the empty string (""). Default: empty string,"". This string may not be longer than 65,535 bytes. negotiatedå¯é-
By default (
false), data channels are negotiated in-band, where one side callscreateDataChannel, and the other side listens to theRTCDataChannelEventevent using theondatachannelEventHandler. Alternatively (true), they can be negotiated out of-band, where both sides callcreateDataChannelwith an agreed-upon id. Default:false. idå¯é-
An 16-bit numeric ID for the channel; permitted values are 0-65534. If you don't include this option, the user agent will select an ID for you.
夿³¨ï¼The options which can be configured using the RTCDataChannelInit dictionary represent the script-settable subset of the properties on the RTCDataChannel interface.
Return value
A new RTCDataChannel object with the specified label, configured using the options specified by options if that parameter is included; otherwise, the defaults listed above are established.
Exceptions
InvalidStateError-
The
RTCPeerConnectionis closed. TypeError-
This can happen in a couple of situations:
- The label and/or protocol string is too long; these cannot be longer than 65,535 bytes (bytes, rather than characters).
- The
idis 65535. While this is a valid unsigned 16-bit value, it's not a permitted value forid.
SyntaxError-
Values were specified for both the
maxPacketLifeTimeandmaxRetransmitsoptions. You may only specify a non-nullvalue for one of these. ResourceInUse-
An
idwas specified, but anotherRTCDataChannelis already using the same value. OperationError-
Either the specified
idis already in use or, if noidwas specified, the WebRTC layer was unable to automatically generate an ID because all IDs are in use.
Examples
This example shows how to create a data channel and set up handlers for the open and message events to send and receive messages on it (For brievity, the example assumes onnegotiationneeded is set up).
// Offerer side
var pc = new RTCPeerConnection(options);
var channel = pc.createDataChannel("chat");
channel.onopen = function (event) {
channel.send("Hi you!");
};
channel.onmessage = function (event) {
console.log(event.data);
};
// Answerer side
var pc = new RTCPeerConnection(options);
pc.ondatachannel = function (event) {
var channel = event.channel;
channel.onopen = function (event) {
channel.send("Hi back!");
};
channel.onmessage = function (event) {
console.log(event.data);
};
};
Alternatively, more symmetrical out-of-band negotiation can be used, using an agreed-upon id (0 here):
// Both sides
var pc = new RTCPeerConnection(options);
var channel = pc.createDataChannel("chat", { negotiated: true, id: 0 });
channel.onopen = function (event) {
channel.send("Hi!");
};
channel.onmessage = function (event) {
console.log(event.data);
};
For a more thorough example showing how the connection and channel are established, see A simple RTCDataChannel sample.
Specifications
| è§è |
|---|
| WebRTC: Real-Time Communication in Browsers > # dom-peerconnection-createdatachannel > |