Răsfoiți Sursa

simple chat example

ericz 12 ani în urmă
părinte
comite
0ce560d093
2 a modificat fișierele cu 82 adăugiri și 55 ștergeri
  1. 73 0
      examples/chat.html
  2. 9 55
      examples/p2pchat.html

+ 73 - 0
examples/chat.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('');
+    });
+    
+  });
+
+</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>
+  
+</body> 
+</html> 

+ 9 - 55
examples/p2pchat.html

@@ -54,63 +54,17 @@
 	      </div>
 	  	</div>
 		<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" type="text/javascript"></script>
-		<script src="http://cdn.peerjs.com/0/peer.min.js" ></script>
+		<script src="file:///C:/node/peers/dist/peer.js" ></script>
 		<script type="text/javascript">
-			$('#connectbtn').click(function()
-				{
-					var username = $('#i_username').val();
-					var peer = new Peer(username, {key: 'x7fwx2kavpy6tj4i', debug: true});
-					peer.on('error',function(error)
-					{
-						alert(error);
-						$('#enter .step2').hide();
-						$('#enter .step1').show();
-					});
-					peer.on('open',function()
-					{
-						console.log('Connected to PeerJS Server.');
-						$('#enter .step1').hide();
-						$('#enter .step2').show();
-						function log(data)
-						{
-							$('#chatlog').append("<b>"+data.origin+"</b>:"+data.message+"<br/>");
-						}
-						var connection = null;
-						function handleConnect(connection)
-						{
-								$('#enter').hide();
-								$('#stage').show();
-								console.log('Connected to Peer');
-								connection.on('data',function(data)
-								{
-									log(data);
-								}).on('close',function()
-								{
-									alert('Peer has disconnected.');
-								});
-							$('#sendbtn').click(function()
-							{
-								var data = {origin:username,message:$('#message').val()};
-								connection.send(data);
-								log(data);
-							});
-						}
-						peer.on('connection',function(connection)
-							{
-								$('#peerbtn').off('click');
-								handleConnect(connection);
-							});
-						$('#peerbtn').click(function()
-							{
-								var connection = peer.connect($('#i_peer').val()).on('error',function(error)
-									{
-										alert(error);
-									}).on('open',function()
-									{
-										handleConnect(connection);
-									});
-							});
+			
+					var peer = new Peer('peer1', {host: 'localhost', port: 9000, debug: true});
+					peer.on('error',function(error) {
+						console.log('error', arguments);
 					});
+					peer.on('open',function() {
+            console.log('open!')
+          });
+          peer.on('connection', function(){};
 				});
 		</script>
 	</body>