|
5 | 5 | use Illuminate\Broadcasting\BroadcastException; |
6 | 6 | use Illuminate\Support\Arr; |
7 | 7 | use Illuminate\Support\Str; |
| 8 | +use Pusher\ApiErrorException; |
8 | 9 | use Pusher\Pusher; |
9 | 10 | use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; |
10 | 11 |
|
@@ -110,20 +111,44 @@ public function broadcast(array $channels, $event, array $payload = []) |
110 | 111 | { |
111 | 112 | $socket = Arr::pull($payload, 'socket'); |
112 | 113 |
|
113 | | - $response = $this->pusher->trigger( |
114 | | - $this->formatChannels($channels), $event, $payload, $socket, true |
115 | | - ); |
| 114 | + if ($this->pusherServerIsVersionFiveOrGreater()) { |
| 115 | + $parameters = $socket !== null ? ['socket_id' => $socket] : []; |
| 116 | + |
| 117 | + try { |
| 118 | + $this->pusher->trigger( |
| 119 | + $this->formatChannels($channels), $event, $payload, $parameters |
| 120 | + ); |
| 121 | + } catch (ApiErrorException $e) { |
| 122 | + throw new BroadcastException( |
| 123 | + sprintf('Pusher error: %s.', $e->getMessage()) |
| 124 | + ); |
| 125 | + } |
| 126 | + } else { |
| 127 | + $response = $this->pusher->trigger( |
| 128 | + $this->formatChannels($channels), $event, $payload, $socket, true |
| 129 | + ); |
| 130 | + |
| 131 | + if ((is_array($response) && $response['status'] >= 200 && $response['status'] <= 299) |
| 132 | + || $response === true) { |
| 133 | + return; |
| 134 | + } |
116 | 135 |
|
117 | | - if ((is_array($response) && $response['status'] >= 200 && $response['status'] <= 299) |
118 | | - || $response === true) { |
119 | | - return; |
| 136 | + throw new BroadcastException( |
| 137 | + ! empty($response['body']) |
| 138 | + ? sprintf('Pusher error: %s.', $response['body']) |
| 139 | + : 'Failed to connect to Pusher.' |
| 140 | + ); |
120 | 141 | } |
| 142 | + } |
121 | 143 |
|
122 | | - throw new BroadcastException( |
123 | | - ! empty($response['body']) |
124 | | - ? sprintf('Pusher error: %s.', $response['body']) |
125 | | - : 'Failed to connect to Pusher.' |
126 | | - ); |
| 144 | + /** |
| 145 | + * Determine if the Pusher PHP server is version 5.0 or greater. |
| 146 | + * |
| 147 | + * @return bool |
| 148 | + */ |
| 149 | + protected function pusherServerIsVersionFiveOrGreater() |
| 150 | + { |
| 151 | + return class_exists(ApiErrorException::class); |
127 | 152 | } |
128 | 153 |
|
129 | 154 | /** |
|
0 commit comments