Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Comet Chat by Zek
- ==================
- This chat based on comet without framwork, without jquery and without prototype ;D
- This chat simple and short !
- This chat without mysql.
- If you like contact me debiais.sebastien@gmail.com
- Code.html
- ----------
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head><title>chat</title>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- </head>
- <style type="text/css">
- #content {
- margin:0px auto;
- width:350px;
- height:350px;
- overflow-y:auto;
- overflow-x:auto;
- }
- #form {
- width:350px;
- margin:auto;
- }
- textarea {
- width:350px;
- border: 0;
- border: 1px solid #C9D0DA;
- outline: none;
- overflow: auto;
- overflow-x: hidden;
- resize: none;
- }
- </style>
- <body>
- <div id="content">
- </div>
- <div id="form">
- <form action="" method="get" onKeyPress="enter(event,this.word.value);" onsubmit="send(this.word.value);return false;">
- <textarea type="text" name="word" id="word" value=""/></textarea>
- <input style="display:none" type="submit" name="submit" value="Send" />
- </form>
- <div>
- <script type="text/javascript">
- function kH(e) {
- var pK = e ? e.which : window.event.keyCode;
- return pK != 13;
- }
- document.onkeypress = kH;
- function createxhr()
- {
- if (window.XMLHttpRequest) // Objet standard
- {
- xhr = new XMLHttpRequest(); // Firefox, Safari, ...
- }
- else if (window.ActiveXObject) // Internet Explorer
- {
- xhr = new ActiveXObject("Microsoft.XMLHTTP");
- }
- return xhr;
- }
- function send(msg)
- {
- var xhr = createxhr();
- // var url = "putmsg.php";
- xhr.open("GET", "putmsg.php?msg="+msg, false);
- // xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
- // var data = "&msg="+ msg;
- xhr.send(null);
- //document.getElementById('content').innerHTML += msg + "<br>";
- document.getElementById('word').value = "";
- }
- function bug() {
- setTimeout(recept,1000);
- }
- function recept(timestamp) {
- var xhr = createxhr();
- xhr.onreadystatechange = function()
- {
- if (xhr.readyState === 4) {
- if (xhr.status === 200) {
- var obj = eval ("(" + xhr.responseText + ")");
- time=obj.timestamp;
- document.getElementById('content').innerHTML += obj.msg + "<br>";
- recept(time);
- } else {
- bug();return false;
- }
- }
- };
- // var url = "recept.php";
- xhr.open("GET", "recept.php?timestamp="+timestamp, true);
- // xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
- // var data = "×tamp="+ timestamp;
- xhr.send(null);
- }
- recept(0);
- function enter(evt,msg)
- {
- var charCode = (evt.which) ? evt.which : window.event.keyCode;
- if (charCode == 13)
- {
- send(msg);return false;
- }
- }
- </script>
- </body>
- </html>
- putmsg.php
- ----------
- <?php
- //put in msg message
- $msg = $_GET['msg'];
- //enter file
- //$file = fopen("data.txt","a");
- //fputs($file,$msg);
- $filename = dirname(__FILE__).'/data.txt';
- file_put_contents($filename,$msg);
- ?>
- recept.php
- ----------
- <?php
- $filename = dirname(__FILE__).'/data.txt';
- // infinite loop until the data file is not modified
- $lastmodif = isset($_GET['timestamp']) ? $_GET['timestamp'] : 0;
- $currentmodif = filemtime($filename);
- while ($currentmodif <= $lastmodif) // check if the data file has been modified
- {
- usleep(10000); // sleep 10ms to unload the CPU
- clearstatcache();
- $currentmodif = filemtime($filename);
- }
- // return a json array
- $response = array();
- $response['msg'] = file_get_contents($filename);
- $response['timestamp'] = $currentmodif;
- echo json_encode($response);
- flush();
- /*
- $file = fopen("data.txt","a+");
- $msg = fgets($file);
- sleep(6);
- echo $msg;
- */
- ?>
- ---
- END
- ---
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement