Message withdrawal and revocation function of real-time chat system based on PHP
Introduction:
With the rapid development and popularity of the Internet, real-time chat system has become a daily important way of communication. When developing a chat system, implementing message recall and revocation functions is a common requirement. This article will introduce how to use PHP to write a real-time chat system based on WebSocket and implement message withdrawal and revocation functions.
The following is a simple example using the Ratchet library:
require 'vendor/autoload.php'; use RatchetMessageComponentInterface; use RatchetConnectionInterface; class Chat implements MessageComponentInterface { protected $clients; public function __construct() { $this->clients = new SplObjectStorage; } public function onOpen(ConnectionInterface $conn) { $this->clients->attach($conn); } public function onMessage(ConnectionInterface $from, $msg) { // 解析接收到的消息 $data = json_decode($msg, true); // 将消息保存到数据库 $message = new ChatMessage(); $message->sender_id = $data['sender_id']; $message->receiver_id = $data['receiver_id']; $message->message = $data['message']; $message->timestamp = time(); $message->save(); // 将消息发送给接收者 foreach ($this->clients as $client) { if ($client !== $from && $client->resourceId == $data['receiver_id']) { $client->send($data['message']); break; } } } public function onClose(ConnectionInterface $conn) { $this->clients->detach($conn); } public function onError(ConnectionInterface $conn, Exception $e) { $conn->close(); } } $server = IoServer::factory( new HttpServer( new WsServer( new Chat() ) ), 8080 ); $server->run();
Modify the onMessage function and add the setting of the status field before saving the message to the database:
$message = new ChatMessage(); $message->sender_id = $data['sender_id']; $message->receiver_id = $data['receiver_id']; $message->message = $data['message']; $message->timestamp = time(); $message->status = 1; // 设置消息状态为正常 $message->save();
To implement the withdrawal function, the client can send a withdrawal instruction to the server and set the corresponding message status to withdrawal:
public function onMessage(ConnectionInterface $from, $msg) { // 解析接收到的消息 $data = json_decode($msg, true); // 根据消息ID更新状态为撤回 ChatMessage::where('id', $data['message_id']) ->update(['status' => 2]); // 广播撤回消息给接收者 $this->broadcastMessage($data['message_id'], $from, $data['receiver_id']); } public function broadcastMessage($messageId, ConnectionInterface $from, $receiverId) { foreach ($this->clients as $client) { if ($client !== $from && $client->resourceId == $receiverId) { $client->send(json_encode(['action' => 'revoke', 'message_id' => $messageId])); break; } } }
To implement the revocation function, you can send a revocation instruction to the server on the client side, and set the corresponding message status to revocation:
public function onMessage(ConnectionInterface $from, $msg) { // 解析接收到的消息 $data = json_decode($msg, true); // 根据消息ID更新状态为撤销 ChatMessage::where('id', $data['message_id']) ->update(['status' => 3]); // 广播撤销消息给接收者 $this->broadcastMessage($data['message_id'], $from, $data['receiver_id']); } public function broadcastMessage($messageId, ConnectionInterface $from, $receiverId) { foreach ($this->clients as $client) { if ($client !== $from && $client->resourceId == $receiverId) { $client->send(json_encode(['action' => 'revoke', 'message_id' => $messageId])); break; } } }
Summary:
This article introduces how to use PHP to build a real-time chat system based on WebSocket and implement message withdrawal and revocation functions. These features can be easily implemented by using the Ratchet library and database to store and process messages. In actual projects, corresponding expansion and optimization can be carried out according to needs.
The above is the detailed content of Message withdrawal and revocation function of real-time chat system based on PHP. For more information, please follow other related articles on the PHP Chinese website!