📜  WebRTC-发送消息

📅  最后修改于: 2020-10-17 05:41:02             🧑  作者: Mango


现在让我们创建一个简单的示例。首先,通过“节点服务器”运行在“信令服务器”教程中创建的信令服务器。

页面上将有三个文本输入,一个用于登录,一个用于用户名,一个用于我们要发送给另一对等方的消息。创建一个index.html文件并添加以下代码-

 
   
    
    
      

我们还添加了三个按钮用于登录,建立连接和发送消息。现在创建一个client.js文件并添加以下代码-

var connection = new WebSocket('ws://localhost:9090'); 
var name = "";

var loginInput = document.querySelector('#loginInput'); 
var loginBtn = document.querySelector('#loginBtn'); 

var otherUsernameInput = document.querySelector('#otherUsernameInput'); 
var connectToOtherUsernameBtn = document.querySelector('#connectToOtherUsernameBtn'); 
var msgInput = document.querySelector('#msgInput'); 
var sendMsgBtn = document.querySelector('#sendMsgBtn'); 
var connectedUser, myConnection, dataChannel;
  
//when a user clicks the login button 
loginBtn.addEventListener("click", function(event) { 
   name = loginInput.value; 
    
   if(name.length > 0) { 
      send({ 
         type: "login", 
         name: name 
      }); 
   } 
}); 
 
//handle messages from the server 
connection.onmessage = function (message) { 
   console.log("Got message", message.data); 
   var data = JSON.parse(message.data); 
    
   switch(data.type) { 
      case "login": 
         onLogin(data.success); 
         break; 
      case "offer": 
         onOffer(data.offer, data.name); 
         break; 
      case "answer":
         onAnswer(data.answer); 
         break; 
      case "candidate": 
         onCandidate(data.candidate); 
         break; 
      default: 
         break; 
   } 
}; 
 
//when a user logs in 
function onLogin(success) { 

   if (success === false) { 
      alert("oops...try a different username"); 
   } else { 
      //creating our RTCPeerConnection object 
      var configuration = { 
         "iceServers": [{ "url": "stun:stun.1.google.com:19302" }] 
      }; 
        
      myConnection = new webkitRTCPeerConnection(configuration, { 
         optional: [{RtpDataChannels: true}] 
      }); 
        
      console.log("RTCPeerConnection object was created"); 
      console.log(myConnection); 
  
      //setup ice handling 
      //when the browser finds an ice candidate we send it to another peer 
      myConnection.onicecandidate = function (event) { 
        
         if (event.candidate) { 
            send({ 
               type: "candidate", 
               candidate: event.candidate 
            });
         } 
      }; 
        
      openDataChannel();
        
   } 
};
  
connection.onopen = function () { 
   console.log("Connected"); 
}; 
 
connection.onerror = function (err) { 
   console.log("Got error", err); 
};
  
// Alias for sending messages in JSON format 
function send(message) { 
   if (connectedUser) { 
      message.name = connectedUser; 
   }
    
   connection.send(JSON.stringify(message)); 
};

您可以看到我们建立了到信令服务器的套接字连接。当用户单击登录按钮时,应用程序会将其用户名发送到服务器。如果登录成功,则应用程序将创建RTCPeerConnection对象并设置onicecandidate处理程序,该处理程序将所有找到的icecandidates发送到另一个对等方。它还运行openDataChannel()函数,该函数创建一个dataChannel。请注意,在创建RTCPeerConnection对象时,如果使用的是Chrome或Opera,则必须使用构造函数可选的第二个参数[{RtpDataChannels:true}]。下一步是向其他同伴创建要约。将以下代码添加到client.js文件中-

//setup a peer connection with another user 
connectToOtherUsernameBtn.addEventListener("click", function () {
  
   var otherUsername = otherUsernameInput.value;
   connectedUser = otherUsername;
    
   if (otherUsername.length > 0) { 
      //make an offer 
      myConnection.createOffer(function (offer) { 
         console.log(); 
            
         send({ 
            type: "offer", 
            offer: offer 
         }); 
            
         myConnection.setLocalDescription(offer); 
      }, function (error) { 
         alert("An error has occurred."); 
      }); 
   } 
});
  
//when somebody wants to call us 
function onOffer(offer, name) { 
   connectedUser = name; 
   myConnection.setRemoteDescription(new RTCSessionDescription(offer));
    
   myConnection.createAnswer(function (answer) { 
      myConnection.setLocalDescription(answer); 
        
      send({ 
         type: "answer", 
         answer: answer 
      }); 
        
   }, function (error) { 
      alert("oops...error"); 
   }); 
}

//when another user answers to our offer 
function onAnswer(answer) { 
   myConnection.setRemoteDescription(new RTCSessionDescription(answer)); 
}
  
//when we got ice candidate from another user 
function onCandidate(candidate) { 
   myConnection.addIceCandidate(new RTCIceCandidate(candidate)); 
}

您可以看到,当用户单击“建立连接”按钮时,应用程序向另一对等方发出SDP报价。我们还设置了onAnsweronCandidate处理程序。最后,让我们实现openDataChannel()函数,该函数创建我们的dataChannel。将以下代码添加到client.js文件中-

//creating data channel 
function openDataChannel() { 

   var dataChannelOptions = { 
      reliable:true 
   }; 
    
   dataChannel = myConnection.createDataChannel("myDataChannel", dataChannelOptions);
    
   dataChannel.onerror = function (error) { 
      console.log("Error:", error); 
   };
    
   dataChannel.onmessage = function (event) { 
      console.log("Got message:", event.data); 
   };  
}
  
//when a user clicks the send message button 
sendMsgBtn.addEventListener("click", function (event) { 
   console.log("send message");
   var val = msgInput.value; 
   dataChannel.send(val); 
});

在这里,我们为连接创建dataChannel,并为“发送消息”按钮添加事件处理程序。现在,在两个选项卡中打开此页面,使用两个用户登录,建立连接并尝试发送消息。您应该在控制台输出中看到它们。请注意,上面的示例已在Opera中进行了测试。

歌剧中的例子

现在您可能会看到RTCDataChannel是WebRTC API的极其强大的一部分。此对象还有许多其他用例,例如对等游戏或基于种子的文件共享。