-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
108 lines (94 loc) · 3.9 KB
/
index.html
File metadata and controls
108 lines (94 loc) · 3.9 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
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>PeerJS Example</title>
</head>
<body>
<script src="https://unpkg.com/peerjs@1.5.2/dist/peerjs.min.js"></script>
<p>
<span id="myPeerId">My peer ID: pending...</span>
<button id="copyBtn" disabled>Copy</button>
<button id="shareBtn" disabled>Share</button>
</p>
<p>
<label for="peerId">Connect to ID:</label>
<input id="peerId" type="text">
<button id="connectPeerIdBtn">Connect to ID</button>
</p>
<p>
<label for="msg">Message:</label>
<input disabled id="msg" type="text">
<button disabled id="sendMsgBtn">Send Message</button>
</p>
<output id="output">
</output>
<script>
let peer = new Peer("", { debug: 3 });
const myPeerId = document.getElementById("myPeerId")
const copyBtn = document.getElementById("copyBtn")
const shareBtn = document.getElementById("shareBtn")
const peerIdInput = document.getElementById("peerId")
const connectPeerId = document.getElementById("connectPeerIdBtn")
const msgInput = document.getElementById("msg")
const sendMsg = document.getElementById("sendMsgBtn")
const output = document.getElementById("output")
const urlParams = new URLSearchParams(window.location.search);
const connectTo = urlParams.get('connectTo');
function addOutput(msg, inc){
let para = document.createElement("p")
para.innerText = (inc ? ">" : "<") + " " + msg
output.appendChild(para)
}
function onConnection (conn){
conn.on('open', function () {
console.log("conn open")
connectPeerId.disabled = true
peerIdInput.disabled = true
msgInput.disabled = false
sendMsg.disabled = false
conn.on('data', function (data) {
addOutput(data, true)
});
sendMsg.addEventListener("click", function () {
addOutput(msgInput.value, false)
conn.send(msgInput.value)
msgInput.value = ""
})
});
}
function connect(peerId){
connectPeerId.disabled = true
peerIdInput.disabled = true
let conn = peer.connect(peerId)
onConnection(conn)
}
peer.on('open', function (id) {
console.log('My peer ID is: ' + id);
myPeerId.innerText = 'My peer ID: ' + id
if (connectTo){
connect(connectTo)
}
copyBtn.disabled = false
copyBtn.addEventListener("click", function (){
navigator.clipboard.writeText(id)
copyBtn.innerText = "✓ Copied"
})
shareBtn.disabled = false
shareBtn.addEventListener("click", function (){
navigator.clipboard.writeText(location.origin + location.pathname + "?connectTo=" + id)
shareBtn.innerText = "✓ Shared"
})
});
peer.on("connection", function(conn) {
onConnection(conn)
})
connectPeerId.addEventListener("click", function () {
connect(peerIdInput.value)
})
</script>
</body>
</html>