Big chat time no css
This commit is contained in:
parent
ed06c52ae3
commit
47c008e23c
10
chat.js
Normal file
10
chat.js
Normal file
@ -0,0 +1,10 @@
|
||||
chatArr=[]
|
||||
|
||||
function chat(message, peer){
|
||||
chatArr.push(peer+": "+message)
|
||||
chathtml = ""
|
||||
chatArr.forEach(element => {
|
||||
chathtml=chathtml+element+"</br>"
|
||||
})
|
||||
document.getElementById("chat").innerHTML = chathtml
|
||||
}
|
12
inbound.js
Normal file
12
inbound.js
Normal file
@ -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")
|
||||
})
|
||||
})
|
||||
})
|
25
index.html
Normal file
25
index.html
Normal file
@ -0,0 +1,25 @@
|
||||
<html>
|
||||
<head>
|
||||
<script src="https://unpkg.com/peerjs@1.3.1/dist/peerjs.min.js"></script>
|
||||
<script src="outbound.js"></script>
|
||||
<script src="inbound.js"></script>
|
||||
<script src="chat.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
Hello Dan boys
|
||||
<div id="id"></div>
|
||||
|
||||
<form action="javascript:void(0); connect()">
|
||||
PeerID: <input type="text" id="connectToPeerID"></input> <button>Connect</button>
|
||||
</form>
|
||||
|
||||
<form action="javascript:void(0); startCall()">
|
||||
<button>Call your buddy</button>
|
||||
</form>
|
||||
<form action="javascript:void(0); makeNoise()">
|
||||
<input type="text" id="chatSend"></input><button>Make Noise</button>
|
||||
</form>
|
||||
<div id="chat"></div>
|
||||
<div id="peers"></div>
|
||||
</body>
|
||||
</html>
|
72
outbound.js
Normal file
72
outbound.js
Normal file
@ -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);
|
Loading…
Reference in New Issue
Block a user