Last active
November 6, 2022 11:00
-
-
Save jeromeetienne/4c56e173f17b11c40872fa31cf71e96b to your computer and use it in GitHub Desktop.
peerjs example - standalone - one webpage sending data to another
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
<script src="http://cdn.peerjs.com/0.3/peer.min.js"></script> | |
<script type="text/javascript"> | |
// https://github.com/peers/peerjs | |
// go here to get your own key | |
var peerjsPeer = new Peer({key: 'xxxYOURKEYGOESHERExxxx'}); | |
peerjsPeer.on('open', function() { | |
console.log('My peerjsPeer ID is: ', peerjsPeer.id); | |
}); | |
peerjsPeer.on('connection', function(peerjsConnection) { | |
peerjsConnection.on('open', function() { | |
// Receive messages | |
peerjsConnection.on('data', function(data) { | |
console.log('Received', data); | |
}); | |
// Send messages | |
peerjsConnection.send('Hello from markers-page!'); | |
}); | |
}); | |
// var conn = peer.connect('arjs-marker-page'); | |
</script> |
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
<script src="http://cdn.peerjs.com/0.3/peer.min.js"></script> | |
<script type="text/javascript"> | |
var peer = new Peer({key: 'xxxYOURKEYGOESHERExxxx'}); | |
peer.on('open', function(id) { | |
console.log('My peer ID is: ' + id); | |
}); | |
function connect(peerID){ | |
var conn = peer.connect(peerID); | |
conn.on('open', function() { | |
// Receive messages | |
conn.on('data', function(data) { | |
console.log('Received', data); | |
}); | |
// Send messages | |
conn.send('Hello from phone!'); | |
}); | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
excellent !