WebRTCvideo聊天

我正在尝试与webrtc和RTCPeerConnection APIbuild立一对一的video聊天。 我的代码的一个问题是,在初始用户连接到服务器后,当其他用户通过socket.io发送消息时,它不会从服务器接收消息。 客户端只收到自己发出的消息。 这是我的一些代码。 完整的项目在Github上: https : //github.com/rashadrussell/webrtc_experiment

客户端

var isInitiator = false; socket.on('initiatorFound', function(data) { isInitiator = data.setInitiator; console.log("Is Initiator? " + isInitiator); }); navigator.getMedia = ( navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia ); navigator.getMedia( {video: true, audio: false}, function(stream) { var video = document.getElementById("localView"); video.src = window.URL.createObjectURL(stream); console.log("Add Stream"); sendMessage('streamAdd', {streamAdded: 'stream-added'}); createPeerConnection(); pc.addStream(stream); if(isInitiator) { callPeer(); } }, function(err) { console.log("The following error occured: "); console.dir(err); } ); function sendMessage(type, message) { console.log("Sending Message"); socket.emit('message',{ "type": type, "message": message }); } function createPeerConnection() { pc = new rtcPeerConnection(servers, options); console.dir(pc); pc.onicecandidate = function(evt) { if(evt.candidate == null) return; pc.onicecandidate = null; console.log("Send Ice Candidate"); sendMessage("iceCandidate", JSON.stringify(evt.candidate)); }; pc.onaddstream = function(evt) { document.body.append("<video id='remoteVideo' autoplay></video>"); var remoteVid = document.getElementById("remoteVideo"); remoteVid.src = window.URL.createObjectURL(evt.stream); }; } function callPeer() { pc.createOffer(function (offer) { pc.setLocalDescription(offer, function() { sendMessage("offer", JSON.stringify(offer)); }); console.log("Send Offer"); }, function(err) { console.log("Offer Error: " + err) }, videoConstraints ); } function answerPeer() { pc.createAnswer(function(answer) { pc.setLocalDescription(answer); sendMessage("answer", JSON.stringify(answer)) }, function(err) { console.log("Sending Answer Error: " + err) }, videoConstraints ); } socket.on('message', function(message) { console.log("CONSOLE MESSAGE:"); console.dir(message); if(message.type == 'streamAdd') { console.log('Stream was added'); createPeerConnection(); if(isInitiator) { callPeer(); } } else if(message.type == 'offer') { pc.setRemoteDescription( new rtcSessionDescription(JSON.parse(message.message))); if(!isInitiator) { console.log("Sending Answer"); answerPeer(); } } else if(message.type == 'answer') { pc.setRemoteDescription( new rtcSessionDescription(JSON.parse(message.message))); } else if(message.type == 'iceCandidate') { console.log("Get Ice Candidate"); pc.addIceCandidate(new rtcIceCandidate(JSON.parse(message.message)) ); } }); 

服务器端

 var isInitiator = false; io.sockets.on('connection', function(socket) { if (!isInitiator) { isInitiator = true; socket.emit('initiatorFound', {setInitiator: isInitiator}); } else { socket.emit('initiatorFound', {setInitiator: !isInitiator}); } // Signaling Channel socket.on('message', function(message) { if (message.type == 'streamAdd') { console.log('Got message: ' + message); } //socket.emit('message' ,message); // Should be: socket.broadcast.emit('message', message); }); }); 

       

网上收集的解决方案 "WebRTCvideo聊天"

看看你是否要发送消息给特定的用户你设置了唯一的ID(socket.id),但是现在你只是试着正确的改变你的服务器端代码

var isInitiator = false;

 io.sockets.on('connection', function(socket) { if (!isInitiator) { isInitiator = true; socket.emit('initiatorFound', {setInitiator: isInitiator}); } else { socket.emit('initiatorFound', {setInitiator: !isInitiator}); } // Signaling Channel socket.on('message', function(message) { if (message.type == 'streamAdd') { console.log('Got message: ' + message); } socket.emit('message' ,message); }); }); 

看到这里socket.emit('消息',消息); 这个套接字对象包含你的ID,所以它发送一条消息给你,如果你想发送一个消息给特定的用户,你知道unique.id任何方式发送消息到每个连接期望这个套接字(平均当前套接字)

 io.sockets.on('connection', function(socket) { if (!isInitiator) { isInitiator = true; socket.emit('initiatorFound', {setInitiator: isInitiator}); } else { socket.emit('initiatorFound', {setInitiator: !isInitiator}); } // Signaling Channel socket.on('message', function(message) { if (message.type == 'streamAdd') { console.log('Got message: ' + message); } socket.broadcast.emit('message' ,message); }); }); 

我发现为什么其他客户在发出消息时没有得到通知。 在我的服务器端代码中,在信令通道部分,socket.emit应该是socket.broadcast.emit或io.sockets.emit。

Socket.emit只将消息中继给发起服务器呼叫的客户端。 socket.broadcast.emit将消息转发给除发起呼叫的客户以外的所有客户端,而io.sockets.emit将消息中继给所有客户端,包括发起呼叫的客户端。