-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathid-taken.html
More file actions
56 lines (54 loc) · 1.52 KB
/
Copy pathid-taken.html
File metadata and controls
56 lines (54 loc) · 1.52 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
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title></title>
<link rel="stylesheet" href="../style.css" />
</head>
<body>
<h1>ID-TAKEN</h1>
<div id="messages"></div>
<div id="error-message"></div>
<script src="/dist/peerjs.js"></script>
<script type="application/javascript">
/**
* @type {typeof import("../..").Peer}
*/
const Peer = window.peerjs.Peer;
const messages = document.getElementById("messages");
const errorMessage = document.getElementById("error-message");
// Peer A should be created without an error
const peerA = new Peer()
.once(
"error",
(err) => (errorMessage.textContent += JSON.stringify(err)),
)
.once("open", (id) => {
// Create 10 new `Peer`s that will try to steel A's id
const steeling_peers = Array.from(
{ length: 10 },
(_, i) =>
new Promise((resolve, reject) =>
new Peer(id)
.once("open", () =>
reject(`Peer ${i} failed! Connection got established.`),
)
.once("error", (error) => {
if (error.type === "unavailable-id") {
resolve(`ID already taken. (${i})`);
} else {
reject(error);
}
}),
),
);
Promise.all(steeling_peers)
.then(() => (messages.textContent = "No ID takeover"))
.catch(
(error) => (errorMessage.textContent += JSON.stringify(error)),
);
});
</script>
</body>
</html>