我参考以下文档在我的 Peer Communicate Web 应用程序中使用完美的协商模式进行构建:
我的网页内容如下:
<html>
<head>
<meta charset="UTF-8">
<title>WebRTC Caller</title>
<script src="/socket.io/socket.io.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
var peer=null;
var socket = io.connect();
var dc,pc,polite=false;
var localVideo,remoteVideo;
var makingOffer = false, ignoreOffer = false;
var configuration = {iceServers:
[{urls: "stun:stun.stunprotocol.org"},
{urls: "stun:stun.l.google.com:19302"},
{urls: "turn:numb.viagenie.ca", credential: "***", username: "***"}
]};
$( document ).ready(function() {
localVideo=document.getElementById("selfView");
remoteVideo=document.getElementById("remoteView");
});
function call() {
polite=true;
createConnection();
}
function createConnection(){
pc=new RTCPeerConnection(configuration);
pc.onnegotiationneeded=negotiationEventHandler;
pc.onicecandidate=iceCandidateEventHandler;
dc= pc.createDataChannel('chat');
}
function hangUp() {
dc.close();
pc.close();
} …Run Code Online (Sandbox Code Playgroud)