2017-05-31 31 views
-1

我想使用peerJs和PeerServer製作視頻聊天應用程序。我傳遞一個在創建對等構造函數時隨機生成的ID。這是我在客戶端如何使用PeerServer檢索其他對等ID

 var generatedID = $('#my_id').text(); 

     var peer = new Peer(generatedID , { host: 'localhost', port: 7880, path: '/' }); 

,然後我創建一個連接,從他們在GitHub上的文檔,我們通過其他同行的ID來初始化連接。

 var conn = peer.connect('another-peers-id'); 
     conn.on('open', function(){ 
       // the the logic here 
     }); 

現在,我想知道如何獲得其他同齡人的ID。

回答

0

我看了API,我沒有看到任何關於它的東西。所以你可以數一數。所以你可以爲當前的同行定義一個數組。

var currentConnectionSize = 0; 
var peers = []; 

conn.on('connection', function(id){ 
    peers.push(id); 
    currentConnectionSize++; 
    console.log(currentConnectionSize); 
}); 

conn.on('disconnected', function(id){ 
    peers.slice(peers.indexOf(id),1); 
    currentConnectionSize--; 
    console.log(currentConnectionSize); 
}); 

也許你需要爲「錯誤」和「關閉」事件定義它。

+0

可能是我的問題不清楚,我想要實現的邏輯是,獲取其他對等id爲了使兩個對等之間的連接,並允許他們溝通和發送消息 – Danny

相關問題