如何使用websockets通过ID连接两个特定客户端

时间:2016-05-19 03:20:11

标签: node.js websocket webrtc p2p

我创建了一个聊天系统,可让您与人们进行视频聊天。

问题是,我需要通过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相关联,那么这两个人可以互相分享视频,我该怎么办呢?

作为初学者,webRTC非常困难,任何指导都是有帮助的。

1 个答案:

答案 0 :(得分:1)

我建议的可能不是最好的解决方案,但一个简单的方法可能是:

  • client1查看可用对等项列表(client2是其中之一),client1选择client2并单击调用按钮,
  • 现在在服务器上,您可以为该呼叫生成一个随机令牌,将其用作websocket roomname并可能调用id
  • 将client1的连接添加到此websocket会议室,将呼叫请求转发给client2
  • 如果client2接受呼叫,则添加client2与房间的连接
  • 您的服务器可以收听同伴加入会议室,并向已经属于该会议室的所有同行广播该消息
  • 现在已经在房间(client1)的对等方可以发起对新对等方(client2)的WebRTC调用