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

How to connect two specific clients by ID using websockets

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

问题是,我需要通过基于 ID 或允许我区分客户端的东西通过 webRTC 连接两个客户端。

目前,连接是随机的。我的代码只是搜索现有候选人并连接到他们。

我试过用这种方式给客户设置一个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 对于初学者来说非常困难,任何指导都是有帮助的。

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

  • client1 看到可用对等点列表(client2 是其中之一),client1 选择 client2 并单击呼叫按钮,
  • 现在在服务器上,您可以为调用生成一个随机令牌,将其用作 websocket 房间名称并可能调用 id
  • 添加client1连接到这个websocket房间,将调用请求转发给client2
  • 如果 client2 接受呼叫,将 client2 的连接添加到房间
  • 您的服务器可以监听加入房间的对等点,并要求将该消息广播给已经属于该房间的所有对等点
  • 现在已经在房间里的对等点(client1)可以向新的对等点(client2)发起 WebRTC 调用