|
@@ -0,0 +1,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
|
|
|
|
+ let peers_try_to_take = 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(peers_try_to_take)
|
|
|
|
+ .then(() => (messages.textContent = "No ID takeover"))
|
|
|
|
+ .catch(
|
|
|
|
+ (error) => (errorMessage.textContent += JSON.stringify(error)),
|
|
|
|
+ );
|
|
|
|
+ });
|
|
|
|
+ </script>
|
|
|
|
+ </body>
|
|
|
|
+</html>
|