--- title: RTCPeerConnection.setConfiguration() slug: Web/API/RTCPeerConnection/setConfiguration translation_of: Web/API/RTCPeerConnection/setConfiguration ---

{{APIRef("WebRTC")}}{{SeeCompatTable}}

The RTCPeerConnection.setConfiguration() method sets the current configuration of the {{domxref("RTCPeerConnection")}} based on the values included in the specified {{domxref("RTCConfiguration")}} object. This lets you change the ICE servers used by the connection and which transport policies to use.

The most common use case for this method (and even then, probably not a very common use case) is to replace the set of ICE servers to be used. Two potential scenarios in which this might be done:

You cannot change the identity information for a connection once it's already been set.

语法

RTCPeerConnection.setConfiguration(configuration);

参数

configuration
{{domxref("RTCConfiguration")}}对象提供一些可以设置的选项。这些选项的改动不会附加到原来的设置,相反,新的选项会完全替代旧的选项。

异常

InvalidAccessError
One or more of the URLs specified in configuration.iceServers is a {{Glossary("TURN")}} server, but complete login information is not provided (that is, either the {{domxref("RTCIceServer.username")}} or {{domxref("RTCIceServer.credentials")}} is missing). This prevents successful login to the server.
InvalidModificationError
The configuration includes changed identity information, but the connection already has identity information specified. This happens if configuration.peerIdentity or configuration.certificates is set and their values differ from the current configuration.
InvalidStateError
{{domxref("RTCPeerConnection")}} 被关闭.
SyntaxError
configuration.iceServers 列表提供的一个或多个URL是无效的

Example

In this example, it has already been determined that ICE restart is needed, and that negotiation needs to be done using a different ICE server.

var restartConfig = { iceServers: [{
                          urls: "turn:asia.myturnserver.net",
                          username: "allie@oopcode.com",
                          credential: "topsecretpassword"
                      }]
};

myPeerConnection.setConfiguration(restartConfig);

myPeerConnection.createOffer({"iceRestart": true}).then(function(offer) {
  return myPeerConnection.setLocalDescription(offer);
})
.then(function() {
  // send the offer to the other peer using the signaling server
})
.catch(reportError);

First, a new {{domxref("RTCConfiguration")}} is created, restartConfig, specifying the new ICE server and its credentials. This is then passed into setConfiguration(). ICE negotiation is restarted by calling {{domxref("RTCPeerConnection.createOffer()", "createOffer()")}}, specifying true as the value of the iceRestart option. From there, we handle the process as usual, by setting the local description to the returned offer and then sending that offer to the other peer.

Specifications

Specification Status Comment
{{SpecName('WebRTC 1.0', '#dom-rtcpeerconnection-setconfiguration', 'setConfiguration()')}} {{Spec2('WebRTC 1.0')}} Initial definition.

Browser compatibility

{{Compat("api.RTCPeerConnection.setConfiguration")}}

See also