You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
2.4 KiB

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 (document.getElementById("preChat").style.visibility != "hidden"){
document.getElementById("preChat").style.visibility="hidden"
document.getElementById("preChat").style.height=0
document.getElementById("connectButton").style.height=0
document.getElementById("chatWrapper").style.visibility="visible"
}
if (typeof data == "object" && data.peerID != document.getElementById("id").innerHTML && typeof peerlist[data.peerID] == "undefined"){
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
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(() => {
// We can make this nicer by sending the last contact everything and every contact our most recent, but not sending everything to everyone except periodically, like once a minute
Object.keys(peerlist).forEach(contact => {
Object.keys(peerlist).forEach(peerDetail => {
peerlist[contact].send({peerID: peerDetail})
})
})
}, 5000);