Explorar o código

part of #18, closes #17

Michelle Bu %!s(int64=12) %!d(string=hai) anos
pai
achega
c93c38ed9f
Modificáronse 4 ficheiros con 343 adicións e 191 borrados
  1. 73 0
      examples/chat-old.html
  2. 154 47
      examples/chat.html
  3. 116 0
      examples/fancy.css
  4. 0 144
      examples/fancy.html

+ 73 - 0
examples/chat-old.html

@@ -0,0 +1,73 @@
+<!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"> 
+
+<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script> 
+<script type="text/javascript" src="http://cdn.peerjs.com/0/peer.js"></script>
+<script>
+  
+  var conn;
+  // Connect to PeerJS, have server assign an ID instead of providing one
+  var peer = new Peer({key: 'lwjd5qra8257b9', debug: true});
+  peer.on('open', function(id){
+    $('#pid').text(id);
+  });  
+  // Await connections from others
+  peer.on('connection', connect);
+  function connect(c) {
+    $('#chat_area').show();
+    conn = c;
+    $('#messages').empty().append('Now chatting with ' + conn.peer);
+    conn.on('data', function(data){
+      $('#messages').append('<br>' + conn.peer + ':<br>' + data);
+    });
+    conn.on('close', function(err){ alert(conn.peer + ' has left the chat.') });
+  }
+  $(document).ready(function() {
+    // Conect to a peer
+    $('#connect').click(function(){
+      var c = peer.connect($('#rid').val());
+      c.on('open', function(){
+        connect(c);
+      });
+      c.on('error', function(err){ alert(err) });  
+    });
+    // Send a chat message
+    $('#send').click(function(){
+      var msg = $('#text').val();
+      conn.send(msg);
+      $('#messages').append('<br>You:<br>' + msg);
+      $('#text').val('');
+    });
+    // Show browser version
+    $('#browsers').text(navigator.userAgent);
+  });
+
+</script>
+</head> 
+ 
+<body> 
+
+  Your PeerJS id is : <span id="pid"></span><br><br>
+  Connect to peer: <input type="text" id="rid" placeholder="Someone else's id">
+                   <input type="button" value="Connect" id="connect"><br><br>
+  
+  
+  <div id="chat_area" style="display: none;">
+    <div id="messages"></div>
+    <input type="text" id="text" placeholder="Enter message">
+    <input type="button" value="Send" id="send">
+  </div>
+  
+
+  
+  
+  <br><br><br>
+  <p style="font-size: 10px;">Your browser version: <span id="browsers"></span><br>
+  Currently <strong>Google Chrome 26.0.1403.0 or above</strong> is required (Dev or Canary channels)</strong><br><br>For more up to date compatibility information see <a href="http://peerjs.com/status">PeerJS WebRTC Status</a><br><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.</p>
+  
+</body> 
+</html> 

+ 154 - 47
examples/chat.html

@@ -5,69 +5,176 @@
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
 <meta http-equiv="Content-Language" content="en-us"> 
 
-<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script> 
+<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>
-  
-  var conn;
-  // Connect to PeerJS, have server assign an ID instead of providing one
-  var peer = new Peer({key: 'lwjd5qra8257b9', debug: true});
-  peer.on('open', function(id){
-    $('#pid').text(id);
-  });  
-  // Await connections from others
-  peer.on('connection', connect);
-  function connect(c) {
-    $('#chat_area').show();
-    conn = c;
-    $('#messages').empty().append('Now chatting with ' + conn.peer);
-    conn.on('data', function(data){
-      $('#messages').append('<br>' + conn.peer + ':<br>' + data);
+// Connect to PeerJS, have server assign an ID instead of providing one
+var connections = {};
+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(c.peer);
+    var messages = $('<div></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) {
+      //chatbox.addClass('active');
+      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) {
+      $('#' + c.peer).find('.messages').append('<div><span class="file">' +
+          c.peer + ' has sent you a file.</span></div>');
     });
-    conn.on('close', function(err){ alert(conn.peer + ' has left the chat.') });
   }
-  $(document).ready(function() {
-    // Conect to a peer
-    $('#connect').click(function(){
-      var c = peer.connect($('#rid').val());
-      c.on('open', function(){
-        connect(c);
-      });
-      c.on('error', function(err){ alert(err) });  
+
+  // Save connection object appropriately.
+  if (!connections[c.peer]) {
+    connections[c.peer] = {};
+  }
+  connections[c.peer][c.label] = c;
+}
+
+$(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);
     });
-    // Send a chat message
-    $('#send').click(function(){
-      var msg = $('#text').val();
-      conn.send(msg);
-      $('#messages').append('<br>You:<br>' + msg);
-      $('#text').val('');
+    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>');
+      }
     });
-    // Show browser version
-    $('#browsers').text(navigator.userAgent);
+    $('#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 = 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);
+});
+
 </script>
 </head> 
  
 <body> 
 
-  Your PeerJS id is : <span id="pid"></span><br><br>
-  Connect to peer: <input type="text" id="rid" placeholder="Someone else's id">
-                   <input type="button" value="Connect" id="connect"><br><br>
-  
-  
-  <div id="chat_area" style="display: none;">
-    <div id="messages"></div>
-    <input type="text" id="text" placeholder="Enter message">
-    <input type="button" value="Send" id="send">
+  <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>
   
   
-  <br><br><br>
-  <p style="font-size: 10px;">Your browser version: <span id="browsers"></span><br>
-  Currently <strong>Google Chrome 26.0.1403.0 or above</strong> is required (Dev or Canary channels)</strong><br><br>For more up to date compatibility information see <a href="http://peerjs.com/status">PeerJS WebRTC Status</a><br><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.</p>
-  
+  <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 (Dev
+  or Canary channels)</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> 

+ 116 - 0
examples/fancy.css

@@ -0,0 +1,116 @@
+
+body,html {
+  padding: 0;
+  margin: 0;
+  font-family: Arial;
+  background-color: #eee;
+  color: #444;
+  font-size: 15px;
+  line-height: 18px;
+  text-shadow: 0 1px 0 rgba(255,255,255,0.4);
+}
+.connection {
+  float: left;
+  height: 250px;
+  width: 200px;
+  overflow: scroll;
+  background-color: #fff;
+  padding: 10px;
+  margin: 5px;
+  border: 1px solid #ccc;
+}
+.connection div {
+  margin: 5px 0;
+}
+.connection.active {
+  border-color: #FF7500;
+}
+h1 {
+  font-size: 15px;
+  padding: 5px 10px;
+  margin: 0 -10px 0 -10px;
+  border-bottom: 1px solid #ccc;
+}
+#wrap {
+  background-color: #ddd;
+  border-width: 1px 0;
+  border-color: #ccc;
+  border-style: solid;
+}
+.clear {
+  clear: both;
+}
+.peer {
+  color: #FF7500;
+  font-weight: 600;
+}
+.you {
+  color: #0C6BA1;
+  font-weight: 600;
+}
+#pid {
+  display: inline-block;
+  padding: 5px;
+  margin: 5px;
+  border: 1px solid #ccc;
+  background-color: #fff;
+  color: #0C6BA1;
+  font-weight: 600;
+}
+.warning {
+  font-size: 12px;
+  line-height: 20px;
+  padding: 10px;
+  background-color: #333;
+  color: #fff;
+  text-shadow: 0 1px 0 rgba(0,0,0,0.4);
+  font-weight: 300;
+  border-width: 1px 0;
+  border-color: #000;
+  border-style: solid;
+}
+a {
+  color: #F2C777;
+}
+.filler {
+  display: block;
+  padding: 10px;
+}
+#actions {
+  padding: 10px;
+}
+
+button, input {
+  font-size: 15px;
+  font-family: Arial;
+  border: 1px solid #ccc;
+  border-right: 0;
+  padding: 5px;
+  background: #fff;
+  margin: 0;
+  color: #444;
+  text-shadow: 0 1px 0 rgba(255,255,255,0.4);
+}
+input:focus {
+  outline: none;
+}
+.button, button {
+  cursor: pointer;
+  background-color: #ddd;
+  border: 1px solid #ccc;
+
+
+}
+form {
+  display: inline-block;
+}
+#close {
+  background-color: #FF7500;
+  border-color: #c15c06;
+}
+
+.important {
+  font-size: 15px;
+  color: #F2C777;
+  margin-bottom: 10px;
+}

+ 0 - 144
examples/fancy.html

@@ -1,144 +0,0 @@
-<!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"> 
-
-<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/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 connections = {};
-  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><b</div>').addClass('connection').attr('data-peer', c.peer);
-      // Select connection handler.
-      chatbox.on('click', function() {
-        if ($(this).attr('class').indexOf('active') === -1) {
-          $(this).addClass('active');
-        } else {
-          $(this).removeClass('active');
-        }
-      });
-      $('#connections').append(chatbox);
-
-      c.on('data', function(data) {
-        chatbox.append('<br><span class="peer">' + c.peer + '</span>: ' + data);
-      });
-      c.on('close', function(err) { alert(c.peer + ' has left the chat.'); });
-    } else {
-
-    }
-
-    // Save connection object appropriately.
-    if (!connections[c.peer]) {
-      connections[c.peer] = {};
-    }
-    connections[c.peer][c.label] = c;
-  }
-
-  $(document).ready(function() {
-    // Prepare file drop box.
-    var box = $('#box');
-    box.on('dragenter', doNothing);
-    box.on('dragover', doNothing);
-    box.text('Drag file to send to active connection here.');
-    box.on('drop', function(e){
-      e.originalEvent.preventDefault();
-      var file = e.originalEvent.dataTransfer.files[0];
-      for (var i = 0, ii = connections.length; i < ii; i += 1) {
-        var connection = connections[i];
-        connection.send(file);
-      }
-    });
-    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(), 'chat');
-      var f = peer.connect($('#rid').val(), 'file');
-      c.on('open', function() {
-        connect(c);
-      });
-      f.on('open', function() {
-        connect(f);
-      });
-      c.on('error', function(err) { alert(err); });
-      f.on('error', function(err) { alert(err); });
-    });
-
-    // Close a connection.
-    $('#close').click(function() {
-      eachActiveConnection(function(c) {
-
-      });
-    });
-
-    // Send a chat message to all active connections.
-    $('#send').click(function() {
-      // For each active connection, send the message.
-      var msg = $('#text').val();
-      conn.send(msg);
-      $('#messages').append('<br>You:<br>' + msg);
-      $('#text').val('');
-    });
-
-    // Goes through each active peer and calls FN on its connections.
-    function eachActiveConnection(fn) {
-      var actives = $('.active');
-      actives.each(function() {
-        var peerId = $(this).attr('data-peer');
-        fn(connections[peerId]);
-      });
-    }
-
-    // Show browser version
-    $('#browsers').text(navigator.userAgent);
-  });
-
-</script>
-</head> 
- 
-<body> 
-
-  Your PeerJS id is : <span id="pid"></span><br><br>
-  Connect to peer: <input type="text" id="rid" placeholder="Someone else's id">
-                   <input type="button" value="Connect" id="connect"><br><br>
-  
-  
-  <div id="chat_area" style="display: none;">
-    <div id="messages"></div>
-    <input type="text" id="text" placeholder="Enter message">
-    <input type="button" value="Send" id="send">
-  </div>
-  
-
-  <div id="box" style="background: #eee; font-size: 26px; width: 400px;
-    height:300px;line-height: 300px; margin: 0 auto; text-align: center;">
-    Drag files here.
-  </div>
-  
-  
-  <br><br><br>
-  <p style="font-size: 10px;">Your browser version: <span id="browsers"></span><br>
-  Currently <strong>Google Chrome 26.0.1403.0 or above</strong> is required (Dev or Canary channels)</strong><br><br>For more up to date compatibility information see <a href="http://peerjs.com/status">PeerJS WebRTC Status</a><br><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.</p>
-  
-</body> 
-</html>