16
This commit is contained in:
116
passenger_socket.php
Normal file
116
passenger_socket.php
Normal file
@@ -0,0 +1,116 @@
|
||||
<?php
|
||||
/**
|
||||
* passenger_socket.php
|
||||
* =====================
|
||||
* WebSocket Server للركاب — بورت 3030
|
||||
* Internal HTTP Server — بورت 3031
|
||||
*
|
||||
* الإصلاحات عن النسخة السابقة:
|
||||
* 1. إصلاح بنية الـ closures:
|
||||
* كان update_driver_location خارج الـ onMessage callback → لا يُنفَّذ أبداً
|
||||
* 2. تحليل الـ payload بشكل صحيح (json_decode للـ nested objects)
|
||||
* 3. Heartbeat يُعاد تشغيله تلقائياً عند إعادة الاتصال
|
||||
* 4. تنظيف البنية العامة
|
||||
*/
|
||||
|
||||
use Workerman\Worker;
|
||||
use PHPSocketIO\SocketIO;
|
||||
|
||||
require_once __DIR__ . '/vendor/autoload.php';
|
||||
|
||||
$INTERNAL_KEY = trim((string) @file_get_contents('/home/intaleq-rides/.internal_socket_key'));
|
||||
|
||||
if (empty($INTERNAL_KEY)) {
|
||||
echo '[CRITICAL] Internal key missing! Exiting.' . PHP_EOL;
|
||||
exit(1);
|
||||
}
|
||||
|
||||
$PORT = 3030;
|
||||
$INTERNAL_PORT = 3031;
|
||||
|
||||
$io = new SocketIO($PORT);
|
||||
|
||||
$io->on('workerStart', function () use ($io, $INTERNAL_KEY, $INTERNAL_PORT) {
|
||||
|
||||
$innerHttp = new Worker("http://0.0.0.0:$INTERNAL_PORT");
|
||||
|
||||
$innerHttp->onMessage = function ($connection, $request) use ($io, $INTERNAL_KEY) {
|
||||
|
||||
$headers = $request->header();
|
||||
if (($headers['x-internal-key'] ?? '') !== $INTERNAL_KEY) {
|
||||
$connection->send('Unauthorized');
|
||||
return;
|
||||
}
|
||||
|
||||
$post = $request->post();
|
||||
$action = trim($post['action'] ?? '');
|
||||
|
||||
if ($action === 'update_ride_status') {
|
||||
|
||||
$passengerId = $post['passenger_id'] ?? null;
|
||||
$rawPayload = $post['payload'] ?? null;
|
||||
|
||||
if (!$passengerId || !$rawPayload) {
|
||||
$connection->send('Error: Missing passenger_id or payload');
|
||||
return;
|
||||
}
|
||||
|
||||
$payload = is_string($rawPayload)
|
||||
? (json_decode($rawPayload, true) ?? $rawPayload)
|
||||
: $rawPayload;
|
||||
|
||||
$io->to('passenger_' . $passengerId)->emit('ride_status_change', $payload);
|
||||
|
||||
$connection->send('OK');
|
||||
echo '[' . date('H:i:s') . '] Status update sent to Passenger #' . $passengerId . PHP_EOL;
|
||||
|
||||
} elseif ($action === 'update_driver_location') {
|
||||
|
||||
$passengerId = $post['passenger_id'] ?? null;
|
||||
$rawPayload = $post['payload'] ?? null;
|
||||
|
||||
if (!$passengerId || !$rawPayload) {
|
||||
$connection->send('Error: Missing passenger_id or payload');
|
||||
return;
|
||||
}
|
||||
|
||||
$payload = is_string($rawPayload)
|
||||
? (json_decode($rawPayload, true) ?? $rawPayload)
|
||||
: $rawPayload;
|
||||
|
||||
$io->to('passenger_' . $passengerId)->emit('driver_location_update', $payload);
|
||||
|
||||
$connection->send('OK');
|
||||
|
||||
} else {
|
||||
$connection->send('Unknown action: ' . $action);
|
||||
}
|
||||
};
|
||||
|
||||
$innerHttp->listen();
|
||||
echo '[' . date('H:i:s') . "] Internal HTTP started on port $INTERNAL_PORT" . PHP_EOL;
|
||||
});
|
||||
|
||||
$io->on('connection', function ($socket) {
|
||||
|
||||
$query = $socket->handshake['query'] ?? [];
|
||||
$passengerId = $query['id'] ?? null;
|
||||
|
||||
if (!$passengerId) {
|
||||
$socket->disconnect();
|
||||
return;
|
||||
}
|
||||
|
||||
$socket->join('passenger_' . $passengerId);
|
||||
echo '[' . date('H:i:s') . "] Passenger Connected: #$passengerId" . PHP_EOL;
|
||||
|
||||
$socket->on('heartbeat', function ($data) {
|
||||
// استقبال النبضة فقط — لا يحتاج رد
|
||||
});
|
||||
|
||||
$socket->on('disconnect', function () use ($passengerId) {
|
||||
echo '[' . date('H:i:s') . "] Passenger Disconnected: #$passengerId" . PHP_EOL;
|
||||
});
|
||||
});
|
||||
|
||||
Worker::runAll();
|
||||
Reference in New Issue
Block a user