È una cosa complicatissima. Io ti posso dare il "la" (con esempi presi in giro tra i miei file), ma credo che demorderai prestissimo. Comincia con lo studiarti quest'argomento: https://developer.mozilla.org/en-US/...er-sent_events e col crearti le seguenti tre pagine:
index.html (la chat):
codice:
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Chat</title>
<script type="text/javascript">
var createChat = (function () {
function updateOutput (oEvent) {
var oNewElement = document.createElement("p");
oNewElement.innerHTML = "message: " + oEvent.data;
this.output.appendChild(oNewElement);
}
function inputResponse () {
if (this.responseText.trim() !== "message sent") {
// in caso di errore...
alert(this.responseText);
}
}
function chatting (oEvent) {
if ((oEvent || window.event).keyCode !== 13) { return true; }
var oReq = new XMLHttpRequest();
oReq.onload = inputResponse;
oReq.open("post", "write.php", true);
oReq.setRequestHeader("Content-Type", "application\/x-www-form-urlencoded");
oReq.send("message=" + escape(this.innerHTML));
this.innerHTML = "";
return false;
}
return function (oParent) {
var oStream = new EventSource("stream.php"), oContainer = document.createElement("div"), oInput = document.createElement("div");
oStream.output = document.createElement("div");
oContainer.className= "chat-container"
oInput.className = "chat-input";
oInput.contentEditable = true;
oInput.onkeypress = chatting;
oStream.output.className = "chat-output";
oStream.onmessage = updateOutput;
oContainer.appendChild(oStream.output);
oContainer.appendChild(oInput);
oParent.appendChild(oContainer);
};
})();
onload = function () {
createChat(document.body);
};
</script>
<style type="text/css">
div.chat-container {
width: 402px;
padding: 10px;
border: 1px #cccccc solid;
}
div.chat-output {
width: 400px;
height: 200px;
border: 1px #cccccc solid;
overflow: auto;
}
div.chat-input {
width: 400px;
height: 20px;
border: 1px #cccccc solid;
}
</style>
</head>
<body>
</body>
</html>
stream.php (il generatore di output - si tratta di un output casuale in questo caso):
codice:
<?php
// questo e' un generatore di eventi casuale
date_default_timezone_set("America/New_York");
header("Content-Type: text/event-stream");
$counter = rand(1, 10);
while (1) {
// Every second, sent a "ping" event.
echo "event: ping\n";
$curDate = date(DATE_ISO8601);
echo 'data: {"time": "' . $curDate . '"}';
echo "\n\n";
// Send a simple message at random intervals.
$counter--;
if (!$counter) {
echo 'data: This is a message at time ' . $curDate . "\n\n";
$counter = rand(1, 10);
}
ob_flush();
flush();
sleep(1);
}
?>
write.php (la pagina che deve curare di ricevere i nuovi messaggi):
codice:
<?php
header("Content-Type: text/plain");
if (isset($_POST["message"])) {
$new_message = $_POST["message"];
// fai qualcosa con $new_message (il messaggio inviato)... e poi...:
echo "message sent";
} else {
echo "error";
}
?>
Per rendere funzionante il tutto devi studiarti i sockets in php: http://devzone.zend.com/209/writing-...ervers-in-php/. Buona fortuna!