How to implement chat moderation in AJAX live chat?
I've made a fairly basic AJAX chat with PHP & MySQL. The chat messages are stored in the MySQL database, and I poll (I know, I know) every few seconds to check for new messages.
In order to keep the bandwi开发者_运维百科dth and update times down, I've set it up so that on the first request it returns all of the messages, and then each request after that only returns messages with an id greater than the last message id received by the client, adding these new messages to the bottom of the chat and removing old ones at the top to keep the chat at a set 150 messages at all times.
This works great, but there is one fatal flaw. When a chat message is deleted by a moderator, it won't get removed from the chat screen unless you reload the page. How can I either alter my system to allow for messages to be deleted, or change my approach so that this will work?
You could keep track of the deleted ids somehow. Either just flag them deleted in the database, or if you have to truly delete the record, keep track of the IDs of those deleted messages. Then add a new section to your AJAX response that lists those deleted IDs.
The clients could then go through the message history and delete the corresponding entries. Assuming you've been attaching the message IDs to the messages in the chat history, this should be a relatively trivial operation.
精彩评论