From 47c008e23c922a2af96bfeb0115944825b5a3680 Mon Sep 17 00:00:00 2001 From: nannal Date: Mon, 11 Jan 2021 00:50:11 +0200 Subject: [PATCH] Big chat time no css --- chat.js | 10 ++++++++ inbound.js | 12 +++++++++ index.html | 25 +++++++++++++++++++ outbound.js | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 119 insertions(+) create mode 100644 chat.js create mode 100644 inbound.js create mode 100644 index.html create mode 100644 outbound.js diff --git a/chat.js b/chat.js new file mode 100644 index 0000000..9c17f1e --- /dev/null +++ b/chat.js @@ -0,0 +1,10 @@ +chatArr=[] + +function chat(message, peer){ + chatArr.push(peer+": "+message) + chathtml = "" + chatArr.forEach(element => { + chathtml=chathtml+element+"
" + }) + document.getElementById("chat").innerHTML = chathtml +} \ No newline at end of file diff --git a/inbound.js b/inbound.js new file mode 100644 index 0000000..3d1555d --- /dev/null +++ b/inbound.js @@ -0,0 +1,12 @@ + +peer.on('connection', function(conn) { + console.log(conn.peer+" has connected to me") + conn.on('open', function() { + peerlist[conn.peer]=conn + console.log(peerlist) + // Receive messages + conn.on('data', function(data) { + handleData(conn,data,"Their Connection") + }) + }) +}) diff --git a/index.html b/index.html new file mode 100644 index 0000000..8930650 --- /dev/null +++ b/index.html @@ -0,0 +1,25 @@ + + + + + + + + + Hello Dan boys +
+ +
+ PeerID: +
+ +
+ +
+
+ +
+
+
+ + \ No newline at end of file diff --git a/outbound.js b/outbound.js new file mode 100644 index 0000000..9426a56 --- /dev/null +++ b/outbound.js @@ -0,0 +1,72 @@ +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); \ No newline at end of file