123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- <!DOCTYPE HTML>
- <html lang="en">
- <head>
- <title>PeerJS chat demo</title>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <meta http-equiv="Content-Language" content="en-us">
- <link href="fancy.css" rel="stylesheet" type="text/css">
- <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/peer.js"></script>
- <script>
- // Connect to PeerJS, have server assign an ID instead of providing one
- var peer = new Peer({key: 'lwjd5qra8257b9', debug: true});
- // Show this peer's ID.
- peer.on('open', function(id){
- $('#pid').text(id);
- });
- // Await connections from others
- peer.on('connection', connect);
- // Handle a connection object.
- function connect(c) {
- // Handle a chat connection.
- if (c.label === 'chat') {
- var chatbox = $('<div></div>').addClass('connection').addClass('active').attr('id', c.peer);
- var header = $('<h1></h1>').html('Chat with <strong>' + c.peer + '</strong>');
- var messages = $('<div><em>Peer connected.</em></div>').addClass('messages');
- chatbox.append(header);
- chatbox.append(messages);
-
- // Select connection handler.
- 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();
- }
- });
- } else if (c.label === 'file') {
- c.on('data', function(data) {
- // If we're getting a file, create a URL for it.
- 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>');
- }
- });
- }
- }
- $(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() {
- // Create 2 connections, one labelled chat and another labelled file.
- var c = peer.connect($('#rid').val(), { label: 'chat' });
- c.on('open', function() {
- connect(c);
- });
- c.on('error', function(err) { alert(err); });
- var f = peer.connect($('#rid').val(), { reliable: true, label: 'file' });
- f.on('open', function() {
- connect(f);
- });
- f.on('error', function(err) { alert(err); });
- });
- // 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');
- actives.each(function() {
- var peerId = $(this).attr('id');
- var conns = peer.connections[peerId];
- var labels = Object.keys(conns);
- for (var i = 0, ii = labels.length; i < ii; i += 1) {
- var conn = conns[labels[i]];
- fn(conn, $(this));
- }
- });
- }
- // Show browser version
- $('#browsers').text(navigator.userAgent);
- });
- // Make sure things clean up properly.
- window.onunload = window.onbeforeunload = function(e) {
- if (!!peer && !peer.destroyed) {
- peer.destroy();
- }
- };
- </script>
- </head>
-
- <body>
- <a href="https://github.com/peers/peerjs"><img style="position: absolute; top: 0; right: 0; border: 0;"
- src="https://s3.amazonaws.com/github/ribbons/forkme_right_orange_ff7600.png"
- alt="Fork me on GitHub"></a>
- <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="warning browser"><div class="important">Your browser version: <span id="browsers"></span><br>
- Currently <strong>Google Chrome 26.0.1403.0 or above</strong> is required.</strong></div>For more up to date compatibility
- information see <a href="http://peerjs.com/status">PeerJS WebRTC
- Status</a><br>Note that this demo may also fail if you are behind
- stringent firewalls or both you and the remote peer and behind symmetric
- NATs.</div>
- </body>
- </html>
|