Created
August 29, 2013 21:15
-
-
Save nikoncode/6383503 to your computer and use it in GitHub Desktop.
long polling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$db = new mysqli("localhost", "root", "", "test"); | |
$db->query("INSERT INTO messages (text, time) VALUES ('".$_POST["text"]."', '".time()."');"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$db = new mysqli("localhost", "root", "", "test"); | |
$q = $db->query("SELECT * FROM messages ORDER BY time DESC LIMIT 1"); | |
$row = $q->fetch_assoc(); | |
$last_mtime = $row["time"]; | |
$last_qtime = $_GET["time"]; | |
$i=0; | |
while ($last_qtime >= $last_mtime) { | |
usleep("5000"); | |
$q = $db->query("SELECT * FROM messages ORDER BY time DESC LIMIT 1"); | |
$row = $q->fetch_assoc(); | |
$last_mtime = $row["time"]; | |
++$i; | |
} | |
$row["cycles"] = $i; | |
exit(json_encode($row)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>Long-Polling chat</title> | |
<script src="http://codeorigin.jquery.com/jquery-1.10.2.min.js"></script> | |
<script> | |
function request(last_mtime) { | |
window.connection = $.getJSON("get.php?time="+last_mtime) | |
.done(function (data) { | |
$("<li>"+data.text+"</li>").appendTo("ul"); | |
console.log("LP: сообщение получено, перезапуск запроса CYCLES: "+data.cycles); | |
request(data.time); | |
}) | |
.fail(function (jqXHR) { | |
console.log(jqXHR); | |
if (jqXHR.statusText=="abort") { | |
console.log("LP: сообщение было отправлено, перезапуск запроса") | |
request(last_mtime); | |
} else { | |
console.log("LP: что-то не так, через 15 сек. попробуем еще"); | |
setTimeout("request("+last_mtime+");", 15000); | |
} | |
}); | |
} | |
function send(e) { | |
window.connection.abort(); | |
$.post("add.php", $(e).serialize() , function () { | |
$(e).find("textarea").val("").focus(); | |
}); | |
} | |
$(function () { | |
$("textarea").keyup(function (e) { | |
if (e.keyCode==13) { | |
send("form"); | |
return false; | |
} | |
}); | |
}); | |
request(0); | |
</script> | |
</head> | |
<body> | |
<ul> | |
<li>Тут появятся сообщения...</li> | |
</ul> | |
<hr> | |
<form onsubmit="send(this);return false;"> | |
<textarea placeholder="Введите сообщение" name="text"></textarea> | |
<input type="submit" value="GO!" /> | |
</form> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment