JSFiddle - React, Tailwind, and code Playground
HTML
<script src="http://cdn.peerjs.com/0.3/peer.js"></script>
JavaScript
isHost = true; // Set to false to connect to peerId
peerId = '1jpugzj8wb87iudi'; // Set to the id of the host
peer = new Peer({key: 'z7wtzrysggco9a4i'});
if ( isHost ) {
print('hosting');
peer.on('open', function(id) {
print('id: ' + id);
});
peer.on('connection', function(conn) {
conn.on('data', function(data){
print('received ping');
conn.send(data);
});
});
}else {
print('connecting');
var conn = peer.connect(peerId);
conn.on('open', function(){
print('connected');
setInterval( function() {
conn.send( JSON.stringify( performance.now() ) );
}, 50);
});
conn.on('data', function(data) {
var msgTime = JSON.parse(data);
print( performance.now() - msgTime );
});
}
var printCount = 0;
function print( str ) {
if ( printCount++ > 20 ) {
document.body.innerHTML = "";
printCount = 0;
}
document.body.innerHTML += str + "<br>";
}