CSS and append chat
This commit is contained in:
parent
47c008e23c
commit
ac3c9050b1
23
chat.js
23
chat.js
@ -1,10 +1,17 @@
|
|||||||
chatArr=[]
|
chatArr = []
|
||||||
|
|
||||||
function chat(message, peer){
|
function chat(message, peer) {
|
||||||
chatArr.push(peer+": "+message)
|
chatArr.push(peer + ": " + message)
|
||||||
chathtml = ""
|
//message=encodeURI(message)
|
||||||
chatArr.forEach(element => {
|
messageDiv = document.createElement("div")
|
||||||
chathtml=chathtml+element+"</br>"
|
text = document.createTextNode(chatArr[chatArr.length - 1])
|
||||||
})
|
messageDiv.appendChild(text)
|
||||||
document.getElementById("chat").innerHTML = chathtml
|
|
||||||
|
// chatArr.forEach(element => {
|
||||||
|
// chathtml = chathtml + element + "</br>"
|
||||||
|
|
||||||
|
// })
|
||||||
|
//document.getElementById("chat" + (chatArr.length)).innerHTML = chathtml
|
||||||
|
|
||||||
|
document.getElementById("chatWrapper").appendChild(messageDiv)
|
||||||
}
|
}
|
@ -3,7 +3,6 @@ peer.on('connection', function(conn) {
|
|||||||
console.log(conn.peer+" has connected to me")
|
console.log(conn.peer+" has connected to me")
|
||||||
conn.on('open', function() {
|
conn.on('open', function() {
|
||||||
peerlist[conn.peer]=conn
|
peerlist[conn.peer]=conn
|
||||||
console.log(peerlist)
|
|
||||||
// Receive messages
|
// Receive messages
|
||||||
conn.on('data', function(data) {
|
conn.on('data', function(data) {
|
||||||
handleData(conn,data,"Their Connection")
|
handleData(conn,data,"Their Connection")
|
||||||
|
30
index.html
30
index.html
@ -4,22 +4,30 @@
|
|||||||
<script src="outbound.js"></script>
|
<script src="outbound.js"></script>
|
||||||
<script src="inbound.js"></script>
|
<script src="inbound.js"></script>
|
||||||
<script src="chat.js"></script>
|
<script src="chat.js"></script>
|
||||||
|
<link rel="stylesheet" href="style.css">
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
Hello Dan boys
|
Your ID is: <div id="id">Loading</div>
|
||||||
<div id="id"></div>
|
<div id = "preChat">
|
||||||
|
<br>
|
||||||
|
Acquire a peerID from anyone in an active chat or, to create a new chat, a user who is not in a chat.
|
||||||
|
</br>
|
||||||
|
</br>
|
||||||
|
<form action="javascript:void(0); connect()">
|
||||||
|
PeerID: <input type="text" id="connectToPeerID"></input> <button>Connect</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
|
||||||
<form action="javascript:void(0); connect()">
|
<form action="javascript:void(0); startCall()" id="connectButton" class="callButton">
|
||||||
PeerID: <input type="text" id="connectToPeerID"></input> <button>Connect</button>
|
|
||||||
</form>
|
|
||||||
|
|
||||||
<form action="javascript:void(0); startCall()">
|
|
||||||
<button>Call your buddy</button>
|
<button>Call your buddy</button>
|
||||||
</form>
|
</form>
|
||||||
<form action="javascript:void(0); makeNoise()">
|
<div class="chatWrapper" id="chatWrapper">
|
||||||
<input type="text" id="chatSend"></input><button>Make Noise</button>
|
<div id="chat1">
|
||||||
</form>
|
</div>
|
||||||
<div id="chat"></div>
|
<form action="javascript:void(0); makeNoise()" class="composeForm">
|
||||||
|
<input type="text" id="chatSend" autocomplete="off"></input><button>Make Noise</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
<div id="peers"></div>
|
<div id="peers"></div>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
13
outbound.js
13
outbound.js
@ -29,8 +29,15 @@ peerlist={}
|
|||||||
|
|
||||||
|
|
||||||
function handleData(conn,data, who){
|
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"){
|
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)
|
var conn = peer.connect(data.peerID)
|
||||||
peerlist[conn.peer]=conn
|
peerlist[conn.peer]=conn
|
||||||
conn.on('open', function() {
|
conn.on('open', function() {
|
||||||
@ -51,6 +58,7 @@ function handleData(conn,data, who){
|
|||||||
|
|
||||||
function makeNoise(){
|
function makeNoise(){
|
||||||
message=document.getElementById("chatSend").value
|
message=document.getElementById("chatSend").value
|
||||||
|
document.getElementById("chatSend").value = ""
|
||||||
Object.keys(peerlist).forEach(element => {
|
Object.keys(peerlist).forEach(element => {
|
||||||
peerlist[element].send(message)
|
peerlist[element].send(message)
|
||||||
})
|
})
|
||||||
@ -63,10 +71,11 @@ function startCall(){
|
|||||||
}
|
}
|
||||||
|
|
||||||
setInterval(() => {
|
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(contact => {
|
||||||
Object.keys(peerlist).forEach(peerDetail => {
|
Object.keys(peerlist).forEach(peerDetail => {
|
||||||
peerlist[contact].send({peerID: peerDetail})
|
peerlist[contact].send({peerID: peerDetail})
|
||||||
//console.log("Sending " +peerlist[contact].peer +" info on "+peerDetail)
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}, 5000);
|
}, 5000);
|
25
style.css
Normal file
25
style.css
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
body {
|
||||||
|
background-color: whitesmoke;
|
||||||
|
}
|
||||||
|
|
||||||
|
.chatWrapper {
|
||||||
|
height: 80%;
|
||||||
|
overflow-y: scroll;
|
||||||
|
background-color: silver;
|
||||||
|
visibility: hidden;
|
||||||
|
size: 0px;
|
||||||
|
border: 50px;
|
||||||
|
border-color: red;
|
||||||
|
}
|
||||||
|
|
||||||
|
.composeForm{
|
||||||
|
position: fixed;
|
||||||
|
bottom:0px;
|
||||||
|
height:30px;
|
||||||
|
width: 100%;
|
||||||
|
}
|
||||||
|
|
||||||
|
.callButton {
|
||||||
|
visibility: hidden;
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user