-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathp2p.js
64 lines (56 loc) · 1.44 KB
/
p2p.js
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
if(location.hash === '#transmitter') {
document.body.classList.remove('receiver');
}
var modal = document.querySelector('modal-el');
document.querySelector('#handshake').addEventListener('click', e => {
if(modal.hasAttribute('open')) {
modal.removeAttribute('open');
} else {
modal.setAttribute('open', true);
}
});
var p = new Peer({
// host: '//hydra-blockly-peer.glitch.me',
host: '//0.peerjs.com',
port: 443,
path: '/'
});
var conn;
// new SimplePeer({
// initiator: location.hash === '#transmitter',
// trickle: false
// })
p.on('open', function(id) {
if(location.hash === '#transmitter') {
document.querySelector('#outgoing').textContent = id;
}
});
p.on('error', err => console.log('error', err))
p.on('signal', data => {
console.log('hello', JSON.stringify(data))
document.querySelector('#outgoing').textContent = JSON.stringify(data)
})
document.querySelector('form').addEventListener('submit', ev => {
ev.preventDefault();
conn = p.connect(document.querySelector('#incoming').value);
conn.on('open', () => {
modal.close();
console.log('CONNECT');
});
conn.on('data', data => {
showOverlay();
updateURL(data);
loadState(data);
});
});
p.on('connection', function(c) {
conn = c;
// uncomment to allow receiver to transmit
// conn.on('data', data => {
// showOverlay();
// updateURL(data);
// loadState(data);
// });
modal.close();
console.log('CONNECT');
});