-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.js
More file actions
135 lines (95 loc) · 4.02 KB
/
main.js
File metadata and controls
135 lines (95 loc) · 4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
var username = "bogo binte";
var mainID = "zxcmasldkfnsdfvblxk";
var selfpeer;
var establishFlag = false;
var _index = 0;
async function establishPeer(index){// recursion (lmao)
selfpeer = new Peer(mainID+index);
selfpeer.on('open', function(id) {// this function should only be called once a succesful peer has been established
// console.log("in selfpeer open");
console.log("id: " + id);
establishFlag = true;
});
selfpeer.on('error', async function (err){
// console.log(err.type);
if(err.type == 'unavailable-id'){
console.log("index " + index + " taken");
if(index < 100){// arbitrary limit to prevent inf loop
return await establishPeer(index+1);
}else{console.log("peer index failure: no open slot");return;}
}else{console.log('peer init error: ' + err);}
});
while(establishFlag == false){
await delay(1000)
}
if(selfpeer.id.replace(mainID, "") == `${index}`){// such that this connection is only used once (running .on multiple times stacks the functions)
console.log("initialized peer");
selfpeer.on('connection', function(c){
console.log('connection gotten');
for(var i = 0;i<conns.length;i++){// no repeat connections in conns
if(conns[i].peer == c.peer){
return;
}
}
newConnIndex = conns.push(c) - 1;// add it to the list
conns[newConnIndex].on('data', function(data){receiveMessage(data);});
conns[newConnIndex].on('close', function(){
console.log("lost connection");
});
});
_index = index;
}
return;
}
function delay(ms){return new Promise(resolve => setTimeout(resolve, ms));}
function receiveMessage(msg){
if(msg == ""){return;}
console.log("rec: " + msg);
// alert(msg);
// var sampleMessageBox = document.getElementById("samplebox");
var newBox = document.getElementById('samplebox').cloneNode(true);// deep clone (for all internals)
newBox.classList.remove('hidden');
newBox.innerHTML = newBox.innerHTML.replace("[[[message]]]", msg["message"]);
newBox.innerHTML = newBox.innerHTML.replace("[[[username]]]", msg["username"]);
newBox.innerHTML = newBox.innerHTML.replace("[[[timestamp]]]", "at " + ((Math.floor(msg["timestamp"]/1000))%10000));
document.getElementById('displaybox').appendChild(newBox);
}
function sendMessage(){
var toSend = {'message':document.getElementById('messagebox').innerHTML,'username':document.getElementById('usernamebox').innerText,'timestamp':Date.now()}
for(var i = 0;i<conns.length;i++){conns[i].send(toSend);}
receiveMessage(toSend);// temp
document.getElementById('messagebox').innerText = '';
}
var conns = [];
async function init(){
document.getElementById('messagebox').addEventListener('keydown', function(ev){
if(ev.key=="Enter"&&ev.shiftKey==false){
ev.preventDefault();
sendMessage();
}
})
await establishPeer(0);
await delay(500);// idk why this is needed but it is
console.log("establishing connections");
await establishConns();
}
async function establishConns(){
for(var i = Math.max(0, _index-4);i<_index;i++){
var alreadyIn = false;
for(var ii = 0;ii<conns.length;ii++){
if(conns[ii].peer == mainID+i){
alreadyIn = true;
}
}
console.log("checked " + i + ", conn preexists: " + alreadyIn);
if(!alreadyIn){
console.log("connect to " + i);
conns.push(selfpeer.connect(mainID + i));
conns[i].on('data', function(data){receiveMessage(data);});
conns[i].on('close', function(){
console.log("lost connection");
});
}
}
}
init();