我创建了一个聊天系统,可让您与人们进行视频聊天。
问题是,我需要通过webRTC连接两个客户端,这些客户端基于ID或允许我区分客户端的东西。
目前,连接是随机的。我的代码只搜索现有的候选人并连接到他们。
我尝试以这种方式为客户设置ID ...
var WebSocketServer = require('ws').Server,
wss = new WebSocketServer({port: 8080}),
CLIENTS=[];
wss.on('connection', function(ws) {
CLIENTS.push(ws);
ws.on('message', function(message) {
console.log('received: %s', message);
sendAll(message);
});
ws.send("NEW USER JOINED");
});
CLIENTS
将跟踪每个客户的情况。
但这仍然无法解决我的问题。这是我的client.js代码......
window.onload = function(){
navigator.getUserMedia = navigator.getUserMedia || navigator.mozGetUserMedia || navigator.webkitGetUserMedia;
window.RTCPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
window.RTCIceCandidate = window.RTCIceCandidate || window.mozRTCIceCandidate || window.webkitRTCIceCandidate;
window.RTCSessionDescription = window.RTCSessionDescription || window.mozRTCSessionDescription || window.webkitRTCSessionDescription;
window.RTCPeerConnectionErrorCallback = window.RTCPeerConnectionErrorCallback || window.mozRTCPeerConnectionErrorCallback || window.webkitRTCPeerConnectionErrorCallback;
serverConnection = new WebSocket('ws://127.0.0.1:3434');
serverConnection.onmessage = gotMessageFromServer;
serverConnection.onmessage = gotMessageFromServer;
var constraints = {
video: true,
audio: false,
};
if(navigator.getUserMedia) {
navigator.getUserMedia(constraints, getUserMediaSuccess, getUserMediaError);
} else {
alert('Your browser does not support getUserMedia API');
}
}
function getUserMediaSuccess(stream) {
localStream = stream;
video.src = window.URL.createObjectURL(stream);
}
function getUserMediaError(error) {
console.log(error);
}
function errorHandler(error) {
console.log(error);
}
function start(isCaller) {
peerConnection = new RTCPeerConnection(peerConnectionConfig);
peerConnection.onicecandidate = gotIceCandidate;
peerConnection.onaddstream = gotRemoteStream;
peerConnection.addStream(localStream);
if(isCaller) {
peerConnection.createOffer(gotDescription, createOfferError);
}
}
function gotDescription(description) {
console.log('got description');
peerConnection.setLocalDescription(description, function () {
serverConnection.send(JSON.stringify({'sdp': description}));
}, function() {console.log('set description error')});
}
function gotIceCandidate(event) {
if(event.candidate != null) {
serverConnection.send(JSON.stringify({'ice': event.candidate}));
}
}
function gotRemoteStream(event) {
console.log("got remote stream");
remote.src = window.URL.createObjectURL(event.stream);
}
function createOfferError(error) {
console.log(error);
}
function gotMessageFromServer(message) {
if(!peerConnection) start(false);
var signal = JSON.parse(message.data);
if(signal.sdp) {
peerConnection.setRemoteDescription(new RTCSessionDescription(signal.sdp), function() {
peerConnection.createAnswer(gotDescription, errorHandler);
}, errorHandler);
} else if(signal.ice) {
peerConnection.addIceCandidate(new RTCIceCandidate(signal.ice));
}
}
如何为客户端分配ID并根据其ID连接它们。例如,如果我有client1
并希望将他与client2
相关联,那么这两个人可以互相分享视频,我该怎么办呢?
答案 0 :(得分:1)
我建议的可能不是最好的解决方案,但一个简单的方法可能是: