Created
November 26, 2022 00:23
-
-
Save felladrin/3f22660ef06cc2e017688246055c1f72 to your computer and use it in GitHub Desktop.
PeerJS Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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