Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 138249

How to implement long pool in my chat application?

$
0
0

I feel terribly stupid but I don't have the strength to do it anymore. Whatever I do, it doesn't work, so I'd like someone to do it for me,

I need to implement the Long pool into this chat. I read many articles, but each one was made slightly differently and did not match my code (despite 6 hours of trying to match different versions - none of them worked:c)

I would like someone to correct my code and implement simple long pool to it. The client should use AJAX (as it is in the code).

Client JavaScript:

function checked() {
    return document.getElementById("check").checked; 
}

function checkValues() {
    return document.getElementById("nick").value && document.getElementById("message").value; 
}


function update() {
    document.getElementById("chat").innerHTML = ""; 

    var xmlhttp;
    if (window.XMLHttpRequest) { // Dla przegladarek IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    } else { // Dla przegladarek IE6, IE5...
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==3 && xmlhttp.status==200) { 
            if (checked()) { 
                document.getElementById("chat").innerHTML=xmlhttp.responseText;
            }
        }
        if (xmlhttp.readyState==4) { 
            xmlhttp.open("GET","messages.php",true);
            xmlhttp.send();
        }
    }
    xmlhttp.open("GET", "messages.php", true); 
    xmlhttp.send(); 

}

function send() {
    var xmlhttp;
    if (window.XMLHttpRequest) {
        xmlhttp=new XMLHttpRequest();
    } else {
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }

    var nickValue = encodeURIComponent(document.getElementById("nick").value); 
    var messageValue = encodeURIComponent(document.getElementById("message").value);

    xmlhttp.open("GET", "send.php?nick="+nickValue+"&message="+messageValue, true); 
    xmlhttp.send();

    document.getElementById("message").value = ""; 
}

funny Server :c

<?php
$filename = "messages.txt";
if (!file_exists($filename)) {
    $file = fopen($filename, "w");
    fwrite($file, "Rozpoczynamy czat!\n");
    fclose($file);
} else {
    $file = fopen($filename, "r");
    $text = fread($file, filesize($filename));
    fclose($file);
    echo $text;
}
?>

script to send messages

<?php
$filename = "messages.txt";
$file = fopen($filename, "a");
$count = count(file($filename));
$text = $_GET["nick"].": ".$_GET["message"]."\n";
fwrite($file, $text);
fclose($file);

while ($count > 1000) { // max 1000 wiadomosci w pliku
    $file = file($filename);
    unset($file[0]);
    file_put_contents($filename, $file);
    $count--;
}

?>

html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="pl" lang="pl">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <script type="text/javascript" src="chat.js"></script>  
    <title>Chat</title>
</head>

<body >
<div id="chatdiv">
<input type="checkbox" name="check" id="check" onchange="update();"/>Run a chat<br/>
<textarea rows="20" cols="80" id="chat" style="background: #FFF; color:black" disabled></textarea><br/>
nick: <br/><input type="text" name="nick" id="nick" /><br/>
Message: <br/><input type="text" name="message" id="message" /><br/>
<button type="button" value="Send" onclick="if (checked() && checkValues()) { send(); } else { alert('Run a chat and after that write nick and message'); }">Wyślij</button>
</div>
</body>
</html>

Viewing all articles
Browse latest Browse all 138249

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>