|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LaraComponents\Centrifuge; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use Illuminate\Broadcasting\BroadcastException; |
|
7
|
|
|
use Illuminate\Broadcasting\Broadcasters\Broadcaster; |
|
8
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException; |
|
9
|
|
|
use LaraComponents\Centrifuge\Contracts\Centrifuge as CentrifugeContract; |
|
10
|
|
|
|
|
11
|
|
|
class CentrifugeBroadcaster extends Broadcaster |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* The Centrifuge SDK instance. |
|
15
|
|
|
* |
|
16
|
|
|
* @var \LaraComponents\Centrifuge\Contracts\Centrifuge |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $centrifuge; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Create a new broadcaster instance. |
|
22
|
|
|
* |
|
23
|
|
|
* @param \LaraComponents\Centrifuge\Contracts\Centrifuge $centrifuge |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct(CentrifugeContract $centrifuge) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->centrifuge = $centrifuge; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Authenticate the incoming request for a given channel. |
|
32
|
|
|
* |
|
33
|
|
|
* @param \Illuminate\Http\Request $request |
|
34
|
|
|
* @return mixed |
|
35
|
|
|
*/ |
|
36
|
|
|
public function auth($request) |
|
37
|
|
|
{ |
|
38
|
|
|
if ($request->user()) { |
|
39
|
|
|
$client = $request->get('client', ''); |
|
40
|
|
|
$channels = $request->get('channels', []); |
|
41
|
|
|
$channels = is_array($channels) ? $channels : [$channels]; |
|
42
|
|
|
|
|
43
|
|
|
$response = []; |
|
44
|
|
|
$info = json_encode([]); |
|
45
|
|
|
foreach ($channels as $channel) { |
|
46
|
|
|
$channelName = (substr($channel, 0, 1) === '$') ? substr($channel, 1) : $channel; |
|
47
|
|
|
|
|
48
|
|
|
try { |
|
49
|
|
|
$result = $this->verifyUserCanAccessChannel($request, $channelName); |
|
50
|
|
|
} catch (HttpException $e) { |
|
51
|
|
|
$result = false; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$response[$channel] = $result ? [ |
|
55
|
|
|
'sign' => $this->centrifuge->generateToken($client, $channel, $info), |
|
56
|
|
|
'info' => $info, |
|
57
|
|
|
] : [ |
|
58
|
|
|
'status' => 403, |
|
59
|
|
|
]; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return response()->json($response); |
|
63
|
|
|
} else { |
|
64
|
|
|
throw new HttpException(401); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Return the valid authentication response. |
|
70
|
|
|
* |
|
71
|
|
|
* @param \Illuminate\Http\Request $request |
|
72
|
|
|
* @param mixed $result |
|
73
|
|
|
* @return mixed |
|
74
|
|
|
*/ |
|
75
|
|
|
public function validAuthenticationResponse($request, $result) |
|
76
|
|
|
{ |
|
77
|
|
|
return $result; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Broadcast the given event. |
|
82
|
|
|
* |
|
83
|
|
|
* @param array $channels |
|
84
|
|
|
* @param string $event |
|
85
|
|
|
* @param array $payload |
|
86
|
|
|
* @return void |
|
87
|
|
|
*/ |
|
88
|
|
|
public function broadcast(array $channels, $event, array $payload = []) |
|
89
|
|
|
{ |
|
90
|
|
|
$payload['event'] = $event; |
|
91
|
|
|
|
|
92
|
|
|
$socket = null; |
|
93
|
|
|
if(array_key_exists('socket', $payload)) { |
|
94
|
|
|
$socket = $payload['socket']; |
|
95
|
|
|
unset($payload['socket']); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
$response = $this->centrifuge->broadcast($this->formatChannels($channels), $payload, $socket); |
|
99
|
|
|
|
|
100
|
|
|
if (is_array($response) && is_null($response['error'])) { |
|
101
|
|
|
return; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
throw new BroadcastException( |
|
105
|
|
|
$response['error'] instanceof Exception ? $response['error']->getMessage() : $response['error'] |
|
106
|
|
|
); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Get the Centrifuge instance. |
|
111
|
|
|
* |
|
112
|
|
|
* @return \LaraComponents\Centrifuge\Contracts\Centrifuge |
|
113
|
|
|
*/ |
|
114
|
|
|
public function getCentrifuge() |
|
115
|
|
|
{ |
|
116
|
|
|
return $this->centrifuge; |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|