Hallo, ich bin gerade dabei ein Chat Script zu Programmieren (in PHP).
Für die User Verständigung möchte ich ein Chat einbauen.
Nun das Problem: Ich möchte, dass per JavaScript (Ajax) die neuen Nachrichten sofort angezeigt werden.
Ich habe schon danach gegoogelt und kein Beispiel mit mysql(i) gefunden.
Dann habe ich selber herumversucht:
index.php:
<?php </pre>
//----------------Config-----------------//
include ('../../../config.php');
//---------------------------------------//
session\_start();
$db = connectDb();
$sqlnachrichten = "SELECT \* FROM `nachrichten` WHERE userid1 = '".$\_SESSION["id"]."' and status = 'ungelesen' ORDER BY id DESC LIMIT 1";
$result = $db-\>query( $sqlnachrichten );
//$result = mysql\_query("SELECT id FROM chatpoll ORDER BY id DESC LIMIT 1");
while($row = $result-\>fetch\_object())
{
$old\_msg\_id = $row-\>id;
}
?\>
function getContent(timestamp)
{
var queryString = {'timestamp' : timestamp};
$.ajax(
{
type: 'GET',
url: 'server.php',
data: queryString,
success: function(data){
// put result data into "obj"
var obj = jQuery.parseJSON(data);
// put the data\_from\_file into #response
$('#response').html(obj.data\_from\_file);
// call the function again, this time with the timestamp we just got from server.php
getContent(obj.timestamp);
}
}
);
}
// initialize jQuery
$(function() {
getContent(<?php echo $old_msg_id; ?>);
});
Response from server:
server.php:
<?php </pre>
//----------------Config-----------------//
include ('../../../config.php');
//---------------------------------------//
session\_start();
$db = connectDb();
$old\_msg\_id = $\_GET['old\_msg\_id'];
$sqlnachrichten = "SELECT \* FROM `nachrichten` WHERE userid1 = '".$\_SESSION["id"]."' and status = 'ungelesen' ORDER BY id DESC LIMIT 1";
$result = $db-\>query( $sqlnachrichten );
while($row = $result-\>fetch\_object())
{
$last\_msg\_id = $row-\>id;
}
// set php runtime to unlimited
set\_time\_limit(0);
// where does the data come from ? In real world this would be a SQL query or something
//$data\_source\_file = 'data.txt';
// main loop
while (true) {
// if ajax request has send a timestamp, then $last\_ajax\_call = timestamp, else $last\_ajax\_call = null
$last\_ajax\_call = $\_GET['timestamp'];
// PHP caches file data, like requesting the size of a file, by default. clearstatcache() clears that cache
clearstatcache();
// get timestamp of when file has been changed the last time
$last\_change\_in\_data\_file = $last\_msg\_id;
// if no timestamp delivered via ajax or data.txt has been changed SINCE last ajax timestamp
if ($last\_ajax\_call == null || $last\_change\_in\_data\_file \> $last\_ajax\_call) {
// get content of data.txt
$data = $last\_msg\_id;
// put data.txt's content and timestamp of last data.txt change into array
$result = array(
'data\_from\_file' =\> $data,
'timestamp' =\> $last\_change\_in\_data\_file
);
// encode to JSON, render the result (for AJAX)
$json = json\_encode($result);
echo $json;
// leave this loop step
break;
} else {
// wait for 1 sec (not very sexy as this blocks the PHP/Apache process, but that's how it goes)
sleep( 1 );
continue;
}
}
könnt ihr mir bitte eine funktionierende Lösung zeigen?
Simon