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.

61 lines
1.3 KiB

  1. <?php
  2. namespace App\Channels;
  3. use Illuminate\Notifications\Notification;
  4. use GuzzleHttp\Client as HttpClient;
  5. class SocketChannel
  6. {
  7. /**
  8. * The API URL for Socket.
  9. *
  10. * @var string
  11. */
  12. protected $socket_url;
  13. /**
  14. * The HTTP client instance.
  15. *
  16. * @var \GuzzleHttp\Client
  17. */
  18. protected $http;
  19. /**
  20. * Create a new Socket channel instance.
  21. *
  22. * @param \GuzzleHttp\Client $http
  23. * @return void
  24. */
  25. public function __construct(HttpClient $http, string $socket_url)
  26. {
  27. $this->http = $http;
  28. $this->socket_url = $socket_url;
  29. }
  30. /**
  31. * Send the given notification.
  32. *
  33. * @param mixed $notifiable
  34. * @param \Illuminate\Notifications\Notification $notification
  35. * @return void
  36. */
  37. public function send($notifiable, Notification $notification)
  38. {
  39. $message = $notification->toSocket($notifiable);
  40. $message->to($notifiable->routeNotificationFor('socket', $notification));
  41. if (! $message->to) {
  42. return;
  43. }
  44. $this->http->post($this->socket_url . '/emit/' . $message->to, [
  45. 'headers' => [
  46. 'Content-Type' => 'application/json',
  47. ],
  48. 'json' => ['data' => $message->data],
  49. ]);
  50. }
  51. }