Skip to content

Instantly share code, notes, and snippets.

@felladrin
Created November 26, 2022 00:23
Show Gist options
  • Save felladrin/3f22660ef06cc2e017688246055c1f72 to your computer and use it in GitHub Desktop.
Save felladrin/3f22660ef06cc2e017688246055c1f72 to your computer and use it in GitHub Desktop.
PeerJS Example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>PeerJS</title>
</head>
<body>
<script src="https://unpkg.com/[email protected]/dist/peerjs.min.js"></script>
<script>
const peer = new Peer();
peer.on("open", function (id) {
console.log("My peer ID is: " + id);
const handleConnection = (conn) => {
conn.on("open", function () {
conn.on("data", function (data) {
console.log("Received:", data);
});
conn.send(`Hello from ${id}`);
});
};
if (location.hash.length > 0) {
const conn = peer.connect(location.hash.substring(1));
handleConnection(conn);
} else {
peer.on("connection", handleConnection);
}
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment