所以我试图通过一个信令服务器建立一个 webRTC 视频连接socket.io
。我已经过去了setLocalDescription
,这让我可以得到冰候选人,我相信从阅读中是正确的方法,但是我如何添加冰候选人。我看到我必须使用类似以下的东西:myPeerConnection.addIceCandidate(RTCIceCandidate);
但是我是否将它添加到我的远程和本地对等连接中?我是否将其发送evt.candidates
到我的信令服务器并将它们添加到那里?如果是这样,怎么办?添加到对等连接的变量不是全局的。我已经为此工作了几天,我想我现在只需要一个指南,比在线教程更好,而且我已经看了所有我能找到的东西。
这是附加到我的 html 的代码:client.js
var socket = io.connect();
var myPeerConnection;
var remotePeerConnection;
var offer;
var PeerConnectionWindow = (window.RTCPeerConnection || window.mozRTCPeerConnection
|| window.webkitRTCPeerConnection || window.msRTCPeerConnection);
var SessionDescription = (window.RTCSessionDescription || window.mozRTCSessionDescription
|| window.webkitRTCSessionDescription || window.msRTCSessionDescription);
function hasUserMedia() {
//check if the browser supports the WebRTC
return !!(navigator.getUserMedia || navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia || navigator.msGetUserMedia);
}
if (hasUserMedia()) {
var configuration = {
"iceServers": [{ "url": "stun:stun.1.google.com:19302" }]
};
myPeerConnection = new PeerConnectionWindow(configuration);
console.log(myPeerConnection);
remotePeerConnection = new PeerConnectionWindow(configuration);
console.log(remotePeerConnection);
myPeerConnection.createOffer(gotDescription, onError);
myPeerConnection.onicecandidate = function(evt){
if(event.candidate){
socket.emit('ice', JSON.stringify({'ice': evt.candidate}));
remotePeerConnection.addIceCandidate(evt.candidate);
console.log("connected my to remote");
}
};
remotePeerConnection.onicecandidate = function(evt){
if(event.candidate){
socket.emit('ice', JSON.stringify({'ice': evt.candidate}));
myPeerConnection.addIceCandidate(evt.candidate);
console.log("connected my to remote");
}
};
remotePeerConnection.onaddstream = function (evt) {
console.log("on added stream");
var remoteVid = document.getElementById('remote');
remoteVid.src = window.URL.createObjectURL(evt.stream);
}
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia
|| navigator.mozGetUserMedia || navigator.msGetUserMedia;
//enabling video and audio channels
navigator.getUserMedia({ video: true, audio: true }, function (stream) {
var video = document.getElementById('local');
// //inserting our stream to the video tag
video.src = window.URL.createObjectURL(stream);
myPeerConnection.addStream(stream);
}, onError);
} else {
alert("WebRTC is not supported");
}
function gotDescription(desc) {
offer = desc
myPeerConnection.setLocalDescription(offer, myPeerConnLocalDescSet, onError);
socket.emit('description', JSON.stringify({'sdp': desc}));
}
function onError(err){
console.log(err);
}
function myPeerConnLocalDescSet(){
remotePeerConnection.setRemoteDescription(offer, remotePeerConnRemoteDescSet,onError);
}
function remotePeerConnRemoteDescSet(){
remotePeerConnection.createAnswer(onAnswerCreated, onError);
}
function onAnswerCreated(description){
answer = description;
remotePeerConnection.setLocalDescription(answer, remotePeerLocalDescSet,onError);
}
function remotePeerLocalDescSet(){
myPeerConnection.setRemoteDescription(answer, myPeerRemoteDescSet,onError);
}
function myPeerRemoteDescSet(){
console.log('did we get it?');
}
这是我的信令服务器:server.js
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
app.use(express.static(__dirname + '/'));
server.listen(process.env.PORT || 3000, function(){
console.log('listening on *:3000');
});
io.sockets.on('connection', function(socket){
console.log("in the socket on server");
socket.on('disconnect', function(){
console.log('user disconnected');
});
socket.on('description', function(data){
console.log(data);
});
socket.on('ice', function(data){
// Do I attempt to add the candidate here like so?: myPeerConnection.addIceCandidate(new RTCIceCandidate(data));
});
});
更新
所以看起来一切都是按顺序流动的,所以我更新了我的代码。现在我只需要关于如何在两个对等方之间交换 SDP 信息的帮助。我现在所拥有的是将它们发送给Socket.io
双方来领取和申请……对吗?有人有一些示例代码来说明这是如何完成的,他们可以向我展示一个解释。请并感谢您的帮助!