Acusis/outbound.js
2021-01-11 00:50:11 +02:00

72 lines
2.0 KiB
JavaScript

var peer = new Peer();
peer.on('open', function(id) {
document.getElementById("id").innerHTML=id
});
setTimeout(() => {
document.getElementById("connectToPeerID").value = ""
}, 1);
function connect(){
peerID=document.getElementById("connectToPeerID").value
var conn = peer.connect(peerID)
console.log("Initiated connection to: "+peerID)
peerlist[conn.peer]=conn
conn.on('open', function() {
// Receive messages
conn.on('data', function(data) {
handleData(conn,data, "My Connection")
})
})
}
// mediaStream = navigator.mediaDevices.getUserMedia({ audio: true, video: false })
peerlist={}
function handleData(conn,data, who){
if (typeof data == "object" && data.peerID != document.getElementById("id").innerHTML && typeof peerlist[data.peerID] == "undefined"){
console.log("connecting to: "+data.peerID)
var conn = peer.connect(data.peerID)
peerlist[conn.peer]=conn
conn.on('open', function() {
// Receive messages
conn.on('data', function(data) {
handleData(conn,data, "My Connection")
})
})
} else if (typeof data == "object" && data.peerID == document.getElementById("id").innerHTML) {
// do nothing
} else if (typeof peerlist[data.peerID] != "undefined") {
// do nothing
} else {
chat(data, conn.peer)
}
}
function makeNoise(){
message=document.getElementById("chatSend").value
Object.keys(peerlist).forEach(element => {
peerlist[element].send(message)
})
chat(message, document.getElementById("id").innerHTML)
message=""
}
function startCall(){
alert("No")
}
setInterval(() => {
Object.keys(peerlist).forEach(contact => {
Object.keys(peerlist).forEach(peerDetail => {
peerlist[contact].send({peerID: peerDetail})
//console.log("Sending " +peerlist[contact].peer +" info on "+peerDetail)
})
})
}, 5000);