Skip to content

Instantly share code, notes, and snippets.

@endaaman
Last active February 21, 2016 10:25
Show Gist options
  • Save endaaman/2e057c01ca903b667515 to your computer and use it in GitHub Desktop.
Save endaaman/2e057c01ca903b667515 to your computer and use it in GitHub Desktop.
<html>
<body>
<div id="audioElementList"></div>
<input type="text" id="userInput">
<button id="connectButton">connect</button>
</body>
<script src="https://code.jquery.com/jquery-2.2.0.min.js"></script>
<script src="http://cdn.peerjs.com/0.3/peer.js"></script>
<script type="text/javascript">
// gd9azb2i9b4t2o6r
var userName = window.prompt('Input your user name', '');
var audioElements = $('#connectButton')
var peer = new Peer(userName, {key: 'gd9azb2i9b4t2o6r'})
var getUserMedia = navigator.webkitGetUserMedia.bind(navigator)
peer.on('open', function(id) {
console.log('My peer ID is: ' + id);
});
var addAudio = function(stream) {
audioElement = document.createElement('AUDIO')
audioElement.contorols = true
audioElementList.appendChild(audioElement)
audioElement.src = URL.createObjectURL(stream)
audioElement.play()
}
var connectedUsers = []
// wait connection
peer.on('call', function(call) {
console.log('called:', call)
getUserMedia({video: false, audio: true}, function(stream) {
call.answer(stream)
call.on('stream', function(remoteStream) {
addAudio(remoteStream)
});
}, function(err) {
console.log('Failed to get local stream' ,err);
});
})
// connect another
$('#connectButton').on('click', function() {
var target = $('#userInput').val()
console.log('trying to connect ' + target)
getUserMedia({video: false, audio: true}, function(stream) {
var call = peer.call(target, stream)
call.on('stream', function(remoteStream) {
addAudio(remoteStream)
});
}, function(err) {
console.log('Failed to get local stream' ,err);
});
})
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment