You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

66 lines
1.4 KiB

<?php
namespace App\Channels;
use Illuminate\Notifications\Notification;
use GuzzleHttp\Client as HttpClient;
class SocketChannel
{
/**
* The API URL for Socket.
*
* @var string
*/
protected $socket_url;
/**
* The HTTP client instance.
*
* @var \GuzzleHttp\Client
*/
protected $http;
/**
* Create a new Socket channel instance.
*
* @param \GuzzleHttp\Client $http
* @return void
*/
public function __construct(HttpClient $http, string $socket_url)
{
$this->http = $http;
$this->socket_url = $socket_url;
}
/**
* Send the given notification.
*
* @param mixed $notifiable
* @param \Illuminate\Notifications\Notification $notification
* @return void
*/
public function send($notifiable, Notification $notification)
{
$message = $notification->toSocket($notifiable);
$message->to($notifiable->routeNotificationFor('socket', $notification));
if (! $message->to) {
return;
}
try {
$this->http->post($this->socket_url . '/emit/' . $message->to, [
'headers' => [
'Content-Type' => 'application/json',
],
'json' => ['data' => $message->data],
]);
} catch (\GuzzleHttp\Exception\ConnectException $e) {
report($e);
}
}
}