I have made an AJAX chat using Javascript, PHP, and MySQL.
I send the chat data with JSON from a PHP page that gets the data from a MySQL database. Each chat message is inserted into an array and pulled with an AJAX call every few seconds. Each row in the database has the timestamp of when the message was posted (the timestamp has the milliseconds appended onto the timestamp in this form: 1288147058.77943).
On every AJAX request a column in the user database is updated with the timestamp of when the chat data was last downloaded. A query like the following is used to retrieve chat messages that have been posted since your last AJAX call.
SELECT user, mes开发者_JAVA百科sage FROM chat WHERE posted_time >= '$last_check_get'
However, sometimes 2 requests go through so quickly that two requests send back the same messages, so they get displayed twice even though they weren't actually posted twice. How can I avoid this?
Instead of a using time-stamp, assign each message a serial number. Checking for new messages remains essentially the same.
精彩评论