Guest User

comet chat

a guest
Jan 10th, 2012
857
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5.49 KB | None | 0 0
  1. index.html
  2. ----------
  3.  
  4.  
  5.  
  6. <html xmlns="http://www.w3.org/1999/xhtml">
  7. <head><title>chat</title>
  8. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  9. </head>
  10. <style type="text/css">
  11.  
  12. #content {
  13. margin:0px auto;
  14. width:350px;
  15. height:350px;
  16. overflow-y:auto;
  17. overflow-x:auto;
  18. }
  19.  
  20. #form {
  21. width:350px;
  22. margin:auto;
  23. }
  24. textarea {
  25. width:350px;
  26. border: 0;
  27. height:17px;
  28. font-size:11px;
  29. border: 1px solid #C9D0DA;
  30. outline: none;
  31. overflow: auto;
  32. overflow-x: hidden;
  33. resize: none;
  34. }
  35. </style>
  36.  
  37.  
  38. <body>
  39.  
  40. <div id="content">
  41. </div>
  42.  
  43. <div id="form">
  44.   <form action="" method="get" onKeyPress="enter(event,this.word.value);" onsubmit="send(this.word.value);return false;">
  45.    <textarea type="text" name="word" id="word" value=""/></textarea>
  46.    <input style="display:none" type="submit" name="submit" value="Send" />
  47.   </form>
  48. <div>
  49. <script type="text/javascript">
  50.  
  51. document.onkeydown = myKeyDownHandler;
  52. //change height textarea
  53. function myKeyDownHandler(){
  54.  
  55. var taille=document.getElementById('word').value.length;
  56.  
  57.     if (taille == 47)
  58.     document.getElementById('word').style.height=17+"px";
  59.  
  60.     else if (taille == 48)
  61.     document.getElementById('word').style.height=32+"px";
  62.  
  63.     else if (taille == 98)
  64.     document.getElementById('word').style.height=48+"px";
  65.  
  66.     else if (taille == 146)
  67.     document.getElementById('word').style.height=65+"px";
  68.  
  69. }
  70.  
  71.  
  72.  
  73.  
  74. function kH(e) {
  75. var pK = e ? e.which : window.event.keyCode;
  76. return pK != 13;
  77. }
  78. document.onkeypress = kH;
  79.  
  80.  
  81. function createxhr()
  82. {
  83.     if (window.XMLHttpRequest)    //  Objet standard
  84.     {
  85.         xhr = new XMLHttpRequest();     //  Firefox, Safari, ...
  86.     }
  87.     else  if (window.ActiveXObject)      //  Internet Explorer
  88.     {
  89.         xhr = new ActiveXObject("Microsoft.XMLHTTP");
  90.     }
  91.     return xhr;
  92. }
  93.  
  94.  
  95. function send(msg)
  96. {
  97.     var xhr = createxhr();
  98.  
  99. //  var url = "putmsg.php";
  100.     xhr.open("GET", "putmsg.php?msg="+msg, false);
  101. //  xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");                  
  102. //  var data = "&msg="+ msg;
  103.     xhr.send(null);
  104.     //document.getElementById('content').innerHTML += msg + "<br>";
  105.     document.getElementById('word').value = "";
  106. }
  107.  
  108.  
  109. /*
  110. function bug(time) {
  111. setTimeout(recept(time),2000);
  112. } */
  113.  
  114. function recept(timestamp) {
  115.  
  116.     var xhr = createxhr();
  117.     xhr.onreadystatechange = function()
  118.     {
  119.  
  120.  
  121.     if (xhr.readyState === 4) {  
  122.             if (xhr.status === 200) {  
  123.                 var obj = eval ("(" + xhr.responseText + ")");
  124.             time=obj.timestamp;
  125.             document.getElementById('content').innerHTML += obj.msg + "<br>";
  126.             recept(time);
  127.        
  128.             } else {  
  129.            setTimeout(function(){ recept(time) }, 5000);return false;
  130.                 }  
  131.         }  
  132.  
  133.  
  134.  
  135.     };
  136.  
  137. //  var url = "recept.php";
  138.     xhr.open("GET", "recept.php?timestamp="+timestamp, true);
  139. //  xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  140. //  var data = "&timestamp="+ timestamp;
  141.     xhr.send(null);
  142. }
  143. recept(0);
  144.  
  145.  
  146. function enter(evt,msg)
  147. {
  148.                 var charCode = (evt.which) ? evt.which : window.event.keyCode;
  149.          
  150.             if (charCode == 13)
  151.             {  
  152.             send(msg);return false;
  153.                 }
  154. }
  155.  
  156. </script>
  157. </body>
  158. </html>
  159.  
  160.  
  161.  
  162. putmsg.php
  163. ----------
  164. <?php
  165.  
  166. //put in msg message
  167. $msg = $_GET['msg'];
  168.  
  169. //enter file
  170. //$file = fopen("data.txt","a");
  171. //fputs($file,$msg);
  172. $filename  = dirname(__FILE__).'/data.txt';
  173. file_put_contents($filename,$msg);
  174.  
  175. ?>
  176.  
  177. recept.php
  178. ----------
  179. <?php
  180.  
  181. $filename  = dirname(__FILE__).'/data.txt';
  182. // infinite loop until the data file is not modified
  183. $lastmodif    = isset($_GET['timestamp']) ? $_GET['timestamp'] : 0;
  184. $currentmodif = filemtime($filename);
  185. while ($currentmodif <= $lastmodif) // check if the data file has been modified
  186. {
  187.  usleep(10000); // sleep 10ms to unload the CPU
  188.  clearstatcache();
  189.  $currentmodif = filemtime($filename);
  190. }
  191.  
  192. // return a json array
  193. $response = array();
  194. $response['msg']       = file_get_contents($filename);
  195. $response['timestamp'] = $currentmodif;
  196. echo json_encode($response);
  197. flush();
  198. /*
  199. $file = fopen("data.txt","a+");
  200. $msg = fgets($file);
  201. sleep(6);
  202. echo $msg;
  203. */
  204.  
  205. ?>
  206.  
  207.  
  208. add-on:Bonus add with old version
  209. ---------------------------------
  210.  
  211. for music
  212. ---------
  213.  
  214. function sik() {
  215. document.getElementById("odio").src = "pling.ogg";
  216. document.getElementById("odio").audioPlayer.load();
  217. document.getElementById("odio").audioPlayer.play();
  218.  
  219. }
  220. </script>
  221.  
  222. <audio style="display:none;" hidden="true" autoplay="autoplay" src="none" id="odio" controls="controls">
  223.  
  224.  
  225. for titlebar
  226. ------------
  227.  
  228. function titlebar(val)
  229. {
  230.  
  231.     var msg  = "Comet";
  232.     var speed = 500;
  233.     var pos = val;
  234.  
  235.     var msg1  = "\xAF\xAF\xAFMessage\xAF\xAF\xAF";
  236.     var msg2  = "___Message___";
  237.  
  238.     if(pos == 0){
  239.         masg = msg1;
  240.         pos = 1;
  241.         document.getElementById('favicon').setAttribute('href','favicondown.png');
  242.     }
  243.     else if(pos == 1){
  244.         masg = msg2;
  245.         pos = 0;
  246.         document.getElementById('favicon').setAttribute('href','faviconup.png');
  247.     }
  248.  
  249.     document.title = masg;
  250.     if (hasblur)
  251.     {
  252.     timer = window.setTimeout("titlebar("+pos+")",speed);
  253.     }
  254.     else {
  255.     document.getElementById('favicon').setAttribute('href','faviconup.png');
  256.     document.title="Comet chat";
  257.     return;
  258.     }
  259.        
  260. }
  261.  
  262.  
  263. in old version:
  264. -----------------
  265.  
  266. handleResponse: function(response)
  267.   {
  268.     document.getElementById('content').innerHTML += '<div>' + response['msg'] + '</div>';
  269.     document.getElementById('content').scrollTop = 9000;
  270.  if (hasblur) {
  271.         /* [--[++]-->> */ titlebar(1);
  272.         /* [--[++]-->> */ sik();
  273.               }
  274.   },
Advertisement
Add Comment
Please, Sign In to add comment