createAnswer()
method on the
RTCPeerConnection
interface creates an
SDP
answer to an offer received from a remote peer during the offer/answer negotiation of a WebRTC connection. The answer contains information about any media already attached to the session, codecs and options supported by the browser, and any
ICE
candidates already gathered. The answer is delivered to the returned
Promise
, and should then be sent to the source of the offer to continue the negotiation process.
aPromise = RTCPeerConnection.createAnswer([options]);
RTCPeerConnection.createAnswer(successCallback, failureCallback[, options]);
选项
可选
RTCAnswerOptions
字典。
In older code and documentation, you may see a callback-based version of this function. This has been deprecated and its use is
strongly
discouraged. You should update any existing code to use the
Promise
-based version of
createAnswer()
instead. The parameters for this form of
createAnswer()
are described below, to aid in updating existing code.
successCallback
RTCSessionDescriptionCallback
which will be passed a single
RTCSessionDescription
object describing the newly-created answer.
failureCallback
RTCPeerConnectionErrorCallback
which will be passed a single
DOMException
object explaining why the request to create an answer failed.
选项
可选
RTCOfferOptions
object providing options requested for the answer.
NotReadableError
The identity provider wasn't able to provide an identity assertion.
OperationError
Generation of the SDP failed for some reason; this is a general failure catch-all exception.
A
Promise
whose fulfillment handler is called with an object conforming to the
RTCSessionDescriptionInit
dictionary which contains the SDP answer to be delivered to the other peer.
Here is a segment of code taken from the code that goes with the article Signaling and video calling . This code comes from the handler for the message sent to carry an offer to another peer across the signaling channel.
Keep in mind that this is part of the signaling process, the transport layer for which is an implementation detail that's entirely up to you. In this case, a
WebSocket
connection is used to send a
JSON
message with a
type
field with the value "video-answer" to the other peer, carrying the answer to the device which sent the offer to connect. The contents of the object being passed to the
sendToServer()
function, along with everything else in the promise fulfillment handler, depend entirely on your design
pc.createAnswer().then(function(answer) {
return pc.setLocalDescription(answer);
})
.then(function() {
// Send the answer to the remote peer through the signaling server.
})
.catch(handleGetUserMediaError);
This asks
RTCPeerConnection
to create and return a new answer. In our promise handler, the returned answer is set as the description of the local end of the connection by calling
setLocalDescription()
.
Once that succeeds, the answer is sent to the signaling server using whatever protocol you see fit.
Promise.catch()
is used to trap and handle errors.
见 Handling the invitation in Signaling and video calling to see the complete code, in context, from which this snippet is derived; that will help you understand the signaling process and how answers work.
| 规范 | 状态 | 注释 |
|---|---|---|
|
WebRTC 1.0: Real-time Communication Between Browsers
The definition of 'createAnswer()' in that specification. |
候选推荐 | 初始定义。 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
createAnswer
|
Chrome
51
|
Edge 15 | Firefox 22 | IE No |
Opera
43
|
Safari 11 |
WebView Android
51
|
Chrome Android
51
|
Firefox Android 44 |
Opera Android
43
|
Safari iOS Yes |
Samsung Internet Android
6.0
|
完整支持
不支持
见实现注意事项。
RTCPeerConnection
canTrickleIceCandidates
connectionState
currentLocalDescription
currentRemoteDescription
getDefaultIceServers()
iceConnectionState
iceGatheringState
localDescription
onaddstream
onconnectionstatechange
ondatachannel
onicecandidate
oniceconnectionstatechange
onicegatheringstatechange
onidentityresult
onidpassertionerror
onidpvalidationerror
onnegotiationneeded
onpeeridentity
onremovestream
onsignalingstatechange
ontrack
peerIdentity
pendingLocalDescription
pendingRemoteDescription
remoteDescription
sctp
signalingState
addIceCandidate()
addStream()
addTrack()
close()
createAnswer()
createDataChannel()
createOffer()
generateCertificate()
getConfiguration()
getIdentityAssertion()
getReceivers()
getSenders()
getStats()
getStreamById()
getTransceivers()
removeStream()
removeTrack()
restartIce()
setConfiguration()
setIdentityProvider()
setLocalDescription()
setRemoteDescription()
MediaDevices.getUserMedia()
Navigator.mediaDevices
RTCCertificate
RTCDTMFSender
RTCDTMFToneChangeEvent
RTCDataChannel
RTCDataChannelEvent
RTCDtlsTransport
RTCErrorEvent
RTCIceCandidate
RTCIceTransport
RTCPeerConnectionIceErrorEvent
RTCPeerConnectionIceEvent
RTCRtpReceiver
RTCRtpSender
RTCRtpTransceiver
RTCSctpTransport
RTCSessionDescription
RTCStatsEvent
RTCStatsReport
RTCTrackEvent