Advertisement
Guest User

Comet Chat.

a guest
Jan 8th, 2012
613
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 3.77 KB | None | 0 0
  1. Comet Chat by Zek
  2. ==================
  3.  
  4.  
  5. This chat based on comet without framwork, without jquery and without prototype ;D
  6.  
  7. This chat simple and short !
  8.  
  9. This chat without mysql.
  10.  
  11. If you like contact me debiais.sebastien@gmail.com
  12.  
  13.  
  14. Code.html
  15. ----------
  16.  
  17. <html xmlns="http://www.w3.org/1999/xhtml">
  18. <head><title>chat</title>
  19. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  20. </head>
  21. <style type="text/css">
  22.  
  23. #content {
  24. margin:0px auto;
  25. width:350px;
  26. height:350px;
  27. overflow-y:auto;
  28. overflow-x:auto;
  29. }
  30.  
  31. #form {
  32. width:350px;
  33. margin:auto;
  34. }
  35. textarea {
  36. width:350px;
  37. border: 0;
  38. border: 1px solid #C9D0DA;
  39. outline: none;
  40. overflow: auto;
  41. overflow-x: hidden;
  42. resize: none;
  43. }
  44. </style>
  45.  
  46.  
  47. <body>
  48.  
  49. <div id="content">
  50. </div>
  51.  
  52.  
  53.  
  54. <div id="form">
  55.   <form action="" method="get" onKeyPress="enter(event,this.word.value);" onsubmit="send(this.word.value);return false;">
  56.    <textarea type="text" name="word" id="word" value=""/></textarea>
  57.    <input style="display:none" type="submit" name="submit" value="Send" />
  58.   </form>
  59. <div>
  60. <script type="text/javascript">
  61.  
  62. function kH(e) {
  63. var pK = e ? e.which : window.event.keyCode;
  64. return pK != 13;
  65. }
  66. document.onkeypress = kH;
  67.  
  68.  
  69. function createxhr()
  70. {
  71.     if (window.XMLHttpRequest)    //  Objet standard
  72.     {
  73.         xhr = new XMLHttpRequest();     //  Firefox, Safari, ...
  74.     }
  75.     else  if (window.ActiveXObject)      //  Internet Explorer
  76.     {
  77.         xhr = new ActiveXObject("Microsoft.XMLHTTP");
  78.     }
  79.     return xhr;
  80. }
  81.  
  82.  
  83. function send(msg)
  84. {
  85.     var xhr = createxhr();
  86.  
  87. //  var url = "putmsg.php";
  88.     xhr.open("GET", "putmsg.php?msg="+msg, false);
  89. //  xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");                  
  90. //  var data = "&msg="+ msg;
  91.     xhr.send(null);
  92.     //document.getElementById('content').innerHTML += msg + "<br>";
  93.     document.getElementById('word').value = "";
  94. }
  95.  
  96.  
  97.  
  98. function bug() {
  99. setTimeout(recept,1000);
  100.  
  101. }
  102.  
  103. function recept(timestamp) {
  104.  
  105.     var xhr = createxhr();
  106.     xhr.onreadystatechange = function()
  107.     {
  108.  
  109.  
  110.     if (xhr.readyState === 4) {  
  111.             if (xhr.status === 200) {  
  112.                 var obj = eval ("(" + xhr.responseText + ")");
  113.             time=obj.timestamp;
  114.             document.getElementById('content').innerHTML += obj.msg + "<br>";
  115.             recept(time);
  116.         } else {  
  117. bug();return false;
  118.         }  
  119.     }  
  120.  
  121.  
  122.  
  123.     };
  124.  
  125. //  var url = "recept.php";
  126.     xhr.open("GET", "recept.php?timestamp="+timestamp, true);
  127. //  xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  128. //  var data = "&timestamp="+ timestamp;
  129.     xhr.send(null);
  130. }
  131. recept(0);
  132.  
  133.  
  134. function enter(evt,msg)
  135. {
  136.                 var charCode = (evt.which) ? evt.which : window.event.keyCode;
  137.          
  138.             if (charCode == 13)
  139.             {  
  140.             send(msg);return false;
  141.                 }
  142. }
  143.  
  144. </script>
  145. </body>
  146. </html>
  147.  
  148.  
  149.  
  150.  
  151.  
  152. putmsg.php
  153. ----------
  154.  
  155. <?php
  156.  
  157. //put in msg message
  158. $msg = $_GET['msg'];
  159.  
  160. //enter file
  161. //$file = fopen("data.txt","a");
  162. //fputs($file,$msg);
  163. $filename  = dirname(__FILE__).'/data.txt';
  164. file_put_contents($filename,$msg);
  165.  
  166. ?>
  167.  
  168.  
  169. recept.php
  170. ----------
  171.  
  172. <?php
  173.  
  174. $filename  = dirname(__FILE__).'/data.txt';
  175. // infinite loop until the data file is not modified
  176. $lastmodif    = isset($_GET['timestamp']) ? $_GET['timestamp'] : 0;
  177. $currentmodif = filemtime($filename);
  178. while ($currentmodif <= $lastmodif) // check if the data file has been modified
  179. {
  180.  usleep(10000); // sleep 10ms to unload the CPU
  181.  clearstatcache();
  182.  $currentmodif = filemtime($filename);
  183. }
  184.  
  185. // return a json array
  186. $response = array();
  187. $response['msg']       = file_get_contents($filename);
  188. $response['timestamp'] = $currentmodif;
  189. echo json_encode($response);
  190. flush();
  191. /*
  192. $file = fopen("data.txt","a+");
  193. $msg = fgets($file);
  194. sleep(6);
  195. echo $msg;
  196. */
  197.  
  198. ?>
  199.  
  200.  
  201.  
  202. ---
  203. END
  204. ---
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement