feat: Implement zero-latency WebSocket live message broadcast with luxury audio chime and Apple-style toast notifications
This commit is contained in:
@@ -312,26 +312,54 @@ class ChatController
|
||||
[$uuid, $userId, $receiverId, $courseId, $lessonId, $messageText, $messageType, $mediaUrl]
|
||||
);
|
||||
|
||||
$msgPayload = [
|
||||
'id' => $messageId,
|
||||
'uuid' => $uuid,
|
||||
'sender_id' => $userId,
|
||||
'receiver_id' => $receiverId,
|
||||
'is_mine' => false,
|
||||
'course_id' => $courseId,
|
||||
'lesson_id' => $lessonId,
|
||||
'message' => $messageText,
|
||||
'message_type' => $messageType,
|
||||
'media_url' => $mediaUrl,
|
||||
'is_read' => false,
|
||||
'created_at' => date('Y-m-d H:i:s'),
|
||||
];
|
||||
|
||||
// Push directly to Workerman WebSocket server for instant 0ms live broadcast to receiver
|
||||
self::pushToWorkerman('push_chat', $receiverId, $msgPayload);
|
||||
|
||||
$response->status(201)->json([
|
||||
'status' => 'success',
|
||||
'message' => 'تم إرسال الرسالة بنجاح',
|
||||
'data' => [
|
||||
'id' => $messageId,
|
||||
'uuid' => $uuid,
|
||||
'sender_id' => $userId,
|
||||
'receiver_id' => $receiverId,
|
||||
'is_mine' => true,
|
||||
'course_id' => $courseId,
|
||||
'lesson_id' => $lessonId,
|
||||
'message' => $messageText,
|
||||
'message_type' => $messageType,
|
||||
'media_url' => $mediaUrl,
|
||||
'is_read' => false,
|
||||
'created_at' => date('Y-m-d H:i:s'),
|
||||
]
|
||||
'data' => array_merge($msgPayload, ['is_mine' => true])
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Push internal event to Workerman's internal HTTP server (port 4241)
|
||||
*/
|
||||
public static function pushToWorkerman(string $action, int $targetUserId, array $payload): bool
|
||||
{
|
||||
try {
|
||||
$ch = curl_init('http://127.0.0.1:4241');
|
||||
curl_setopt($ch, CURLOPT_POST, 1);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([
|
||||
'action' => $action,
|
||||
'target_user_id' => $targetUserId,
|
||||
'payload' => json_encode($payload, JSON_UNESCAPED_UNICODE)
|
||||
]));
|
||||
curl_setopt($ch, CURLOPT_TIMEOUT_MS, 400); // Fast non-blocking timeout
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_exec($ch);
|
||||
curl_close($ch);
|
||||
return true;
|
||||
} catch (\Throwable $e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark all messages in a conversation as read
|
||||
* POST /api/chat/read
|
||||
|
||||
Reference in New Issue
Block a user