Initial V2 commit
This commit is contained in:
74
app/Services/SocketService.php
Normal file
74
app/Services/SocketService.php
Normal file
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
/**
|
||||
* Socket Communication Service
|
||||
*
|
||||
* Communicates with Node.js socket servers for real-time events.
|
||||
* Replaces hardcoded IPs in V1 with .env configuration.
|
||||
*/
|
||||
class SocketService
|
||||
{
|
||||
private string $locationServerUrl;
|
||||
private string $rideSocketUrl;
|
||||
private string $internalKey;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->locationServerUrl = config('intaleq.location_server_url');
|
||||
$this->rideSocketUrl = config('intaleq.ride_socket_url');
|
||||
|
||||
$keyPath = config('intaleq.internal_socket_key_path');
|
||||
$this->internalKey = file_exists($keyPath) ? trim(file_get_contents($keyPath)) : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Notify passenger via ride socket server
|
||||
*/
|
||||
public function notifyPassenger(string $passengerId, array $payload): void
|
||||
{
|
||||
$this->sendAsync($this->rideSocketUrl, array_merge($payload, [
|
||||
'action' => 'notify_passenger',
|
||||
'passenger_id' => $passengerId,
|
||||
]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Send event to location server (e.g., ride_taken, cancel_ride)
|
||||
*/
|
||||
public function sendToLocationServer(string $event, array $data): void
|
||||
{
|
||||
$this->sendAsync($this->locationServerUrl, array_merge($data, [
|
||||
'action' => $event,
|
||||
]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Non-blocking HTTP POST (fire and forget with short timeout)
|
||||
*/
|
||||
private function sendAsync(string $url, array $data): void
|
||||
{
|
||||
try {
|
||||
$ch = curl_init($url);
|
||||
curl_setopt_array($ch, [
|
||||
CURLOPT_RETURNTRANSFER => true,
|
||||
CURLOPT_POST => true,
|
||||
CURLOPT_POSTFIELDS => http_build_query($data),
|
||||
CURLOPT_TIMEOUT_MS => 500,
|
||||
CURLOPT_NOSIGNAL => 1,
|
||||
]);
|
||||
|
||||
if ($this->internalKey) {
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, ["x-internal-key: {$this->internalKey}"]);
|
||||
}
|
||||
|
||||
curl_exec($ch);
|
||||
curl_close($ch);
|
||||
} catch (\Exception $e) {
|
||||
Log::warning("[Socket] Failed to send to {$url}: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user