Created
January 24, 2015 04:47
-
-
Save Akiyah/3283fe0eba41d52b70a5 to your computer and use it in GitHub Desktop.
2015-01-24 1st
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
* { | |
margin: 0; | |
padding: 0; | |
border: 0; | |
} | |
body { | |
background: #ddd; | |
font: 30px sans-serif; | |
} | |
input { | |
border:solid thin black; | |
} |
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 type="text/javascript" src="/lib/jquery-1.8.3/js"></script--> | |
<!--script type="text/javascript" src="http://cdn.peerjs.com/0.3/peer.min.js"></script--> | |
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script> | |
<script type="text/javascript" src="http://cdn.peerjs.com/0.3/peer.js"></script> | |
<div id="actions"> | |
Your PeerJS ID is <span id="pid"></span><br> | |
Connect to a peer: <input type="text" id="rid" placeholder="Someone else's id"> | |
<input class="button" type="button" value="Connect" id="connect"><br><br> | |
<form id="send"> | |
<input type="text" id="text" placeholder="Enter message"> | |
<input class="button" type="submit" value="Send to selected peers"> | |
</form> | |
<button id="close">Close selected connections</button> | |
</div> | |
<div id="wrap"><div id="connections"><span class="filler">You have not yet | |
made any connections.</span></div> | |
<div class="clear"></div></div> | |
<div id="box" style="background: #fff; font-size: 18px;padding:40px 30px; text-align: center;"> | |
Drag file here to send to active connections. | |
</div> | |
<div class="log" style="color:#FF7500;text-shadow:none;padding:15px;background:#eee"> | |
<strong>Connection status</strong>:<br> | |
</div> |
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
var peer = new Peer({ | |
key: 'qkblwya9br1z6w29', | |
debug: 3, | |
logFunction: function() { | |
var copy = Array.prototype.slice.call(arguments).join(' '); | |
$('.log').append(copy + '<br>'); | |
} | |
}); | |
var connectedPeers = {}; | |
peer.on('open', function(id){ | |
$('#pid').text(id); | |
}); | |
peer.on('connection', connect); | |
peer.on('error', function(err) { | |
console.log(err); | |
}) | |
function connect(c) { | |
if (c.label === 'chat') { | |
var chatbox = $('<div></div>').addClass('connection').addClass('active').attr('id', c.peer); | |
var header = $('<h3></h3>').html('Chat with ' + c.peer + ''); | |
var messages = $('<div><em>Peer connected.</em></div>').addClass('messages'); | |
chatbox.append(header); | |
chatbox.append(messages); | |
chatbox.on('click', function() { | |
if ($(this).attr('class').indexOf('active') === -1) { | |
$(this).addClass('active'); | |
} else { | |
$(this).removeClass('active'); | |
} | |
}); | |
$('.filler').hide(); | |
$('#connections').append(chatbox); | |
c.on('data', function(data) { | |
messages.append('<div><span class="peer">' + c.peer + '</span>: ' + data + | |
'</div>'); | |
}); | |
c.on('close', function() { | |
alert(c.peer + ' has left the chat.'); | |
chatbox.remove(); | |
if ($('.connection').length === 0) { | |
$('.filler').show(); | |
} | |
delete connectedPeers[c.peer]; | |
}); | |
} else if (c.label === 'file') { | |
c.on('data', function(data) { | |
if (data.constructor === ArrayBuffer) { | |
var dataView = new Uint8Array(data); | |
var dataBlob = new Blob([dataView]); | |
var url = window.URL.createObjectURL(dataBlob); | |
$('#' + c.peer).find('.messages').append('<div><span class="file">' + | |
c.peer + ' has sent you a <a target="_blank" href="' + url + '">file</a>.</span></div>'); | |
} | |
}); | |
} | |
connectedPeers[c.peer] = 1; | |
} | |
$(document).ready(function() { | |
// Prepare file drop box. | |
var box = $('#box'); | |
box.on('dragenter', doNothing); | |
box.on('dragover', doNothing); | |
box.on('drop', function(e){ | |
e.originalEvent.preventDefault(); | |
var file = e.originalEvent.dataTransfer.files[0]; | |
eachActiveConnection(function(c, $c) { | |
if (c.label === 'file') { | |
c.send(file); | |
$c.find('.messages').append('<div><span class="file">You sent a file.</span></div>'); | |
} | |
}); | |
}); | |
function doNothing(e){ | |
e.preventDefault(); | |
e.stopPropagation(); | |
} | |
// Connect to a peer | |
$('#connect').click(function() { | |
var requestedPeer = $('#rid').val(); | |
if (!connectedPeers[requestedPeer]) { | |
// Create 2 connections, one labelled chat and another labelled file. | |
var c = peer.connect(requestedPeer, { | |
label: 'chat', | |
serialization: 'none', | |
metadata: {message: 'hi i want to chat with you!'} | |
}); | |
c.on('open', function() { | |
connect(c); | |
}); | |
c.on('error', function(err) { alert(err); }); | |
var f = peer.connect(requestedPeer, { label: 'file', reliable: true }); | |
f.on('open', function() { | |
connect(f); | |
}); | |
f.on('error', function(err) { alert(err); }); | |
} | |
connectedPeers[requestedPeer] = 1; | |
}); | |
// Close a connection. | |
$('#close').click(function() { | |
eachActiveConnection(function(c) { | |
c.close(); | |
}); | |
}); | |
// Send a chat message to all active connections. | |
$('#send').submit(function(e) { | |
e.preventDefault(); | |
// For each active connection, send the message. | |
var msg = $('#text').val(); | |
eachActiveConnection(function(c, $c) { | |
if (c.label === 'chat') { | |
c.send(msg); | |
$c.find('.messages').append('<div><span class="you">You: </span>' + msg | |
+ '</div>'); | |
} | |
}); | |
$('#text').val(''); | |
$('#text').focus(); | |
}); | |
// Goes through each active peer and calls FN on its connections. | |
function eachActiveConnection(fn) { | |
var actives = $('.active'); | |
var checkedIds = {}; | |
actives.each(function() { | |
var peerId = $(this).attr('id'); | |
if (!checkedIds[peerId]) { | |
var conns = peer.connections[peerId]; | |
for (var i = 0, ii = conns.length; i < ii; i += 1) { | |
var conn = conns[i]; | |
fn(conn, $(this)); | |
} | |
} | |
checkedIds[peerId] = 1; | |
}); | |
} | |
}); | |
window.onunload = window.onbeforeunload = function(e) { | |
if (!!peer && !peer.destroyed) { | |
peer.destroy(); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment