gpt4 book ai didi

WebRTC PeerJS 文本聊天 - 同时连接到多个 peerID

转载 作者:行者123 更新时间:2023-12-01 15:49:57 31 4
gpt4 key购买 nike

嗨, friend 们,当我单独连接到单个对等点时,我一直致力于连接到多个对等点 ID 以进行文本聊天

但我在同时连接多个 peerid 时遇到问题

例如,为了连接到单个对等点,我们将使用它

    var conn = peer.connect(peerId);

conn.on('open', function() {
connect(conn);
});

当我想连接到多个对等 ID 时

例如:var peerIDs = ['peerid 1', 'peerid 2', 'peerid 3']

我为此使用循环
for(var i=0 ; i < peerIDs.length ; i++){
conn = peer.connect(peerIDs[i]);

conn.on('open', function() {
connect(conn);
});
}

这是完整的代码:
var userId = new Date().getTime();
//Get the ID from the server
var peer = new Peer(userId, {host: 'localhost', port: 3031, path: '/',debug: true });

var conn;
var connections = [];

//to receive id from the server
peer.on('open', function(id){
console.log('the id is' +id);

});

//in case of error
peer.on('error', function(e){
alert(e.message);
})

//Awaits for the connection
peer.on('connection', connect);

function connect(c){

conn = c;

connections[c.peer].on('data', function(data){

var mess = document.createElement('div');
mess.innerHTML = '<span class="peer">' + c.peer + '</span>: ' + data;
angular.element( document.querySelector( '.messages' ) ).append(mess);


});

connections[c.peer].on('close', function(){

alert(c.peer + 'has left the chat');

});



}

//When user clicks the chat button
$scope.chat = function(){
alert('user clicked the connect button');

var peerIDs = [ 'peerid 1', 'peerid 2', 'peerid 3']
for(var i=0 ; i < peerIDs.length ; i++){
var conn = peer.connect(peerIDs[i]);

conn.on('open', function() {
connections.push(c);
connect(conn);
});
}


}

//send message when clicked
$scope.send = function(){

// For each active connection, send the message.
var msg = angular.element( document.querySelector( '#mess' ) ).val();

//Send message to all connected peers
for(var i in connections){
connections[i].send(msg);
}

angular.element( document.querySelector( '.messages' ) ).append('<div><span class="you">You: </span>' + msg
+ '</div>');

}

您能否提供有关如何实现这一目标的见解。您的帮助将不胜感激。

最佳答案

为了能够同时拥有多个连接,您只需要同时处理多个连接。

// Array of remote peers ID and data channel
var remotePeerIds=[],// You need this to link with specific DOM element
connections=[]; // This is where you manage multi-connections

// create a Peer
var peer = new Peer({key: 'YOUR_KEY'}); // You can use your own peerID here

// Get your local peer id
peer.on('open', function(id) {
console.log('My peer ID is: ' + id);
});

// Start connection with other peer - and handle it
getConnect(remotePeerId){
var conn = peer.connect(remotePeerId);
handleConnection(conn);
}

// Ok now you need to handle the received connection too
peer.on('connection',function(conn){
handleConnection(conn);
});

// Handle connection - this is most important part
handleConnection(conn){
remotePeerIds.push(conn.peer); // Add remote peer to list

conn.on('open', function() {
console.log("Connected with peer: "+remotePeerId);
conn.on('data',function(data){
// You can do whatever you want with the data from this connection - this is also the main part
dataHandler(conn,data);
});
conn.on('error',function(){
// handle error
connectionError(conn);
});

conn.on('close',function(){
// Handle connection closed
connectionClose(conn);
});
connections.push(conn);
});
});
}

// So now you have multi-connections. If you want to send a message to all other peer, just using for loop with all the connections
function broadcastMessage(message){
for(var i=0;i<connections.length,i++){
connections[i].send(message);
}
}

// Or if you want to send a message to a specific peer - you need to know his peerid

function privateMessage(remotePeerId,message){
for(var i=0;i<connections.length,i++){
if(connections[i].peer==remotePeerId){
connections[i].send(message);
break;
}
}
}

这是主要部分,您需要为连接处理程序添加更多代码,以防出错和关闭。

就是这样 !

关于WebRTC PeerJS 文本聊天 - 同时连接到多个 peerID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30738079/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com