1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Laravoole; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException; |
8
|
|
|
use Illuminate\Broadcasting\Broadcasters\Broadcaster; |
9
|
|
|
|
10
|
|
|
use Laravoole\LaravooleFacade; |
11
|
|
|
|
12
|
|
|
class LaravooleBroadcaster extends Broadcaster |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Authenticate the incoming request for a given channel. |
16
|
|
|
* |
17
|
|
|
* @param \Illuminate\Http\Request $request |
18
|
|
|
* @return mixed |
19
|
|
|
*/ |
20
|
|
|
public function auth($request) |
21
|
|
|
{ |
22
|
|
|
if (Str::startsWith($request->channel_name, ['private-', 'presence-']) && |
23
|
|
|
! $request->user()) { |
24
|
|
|
throw new HttpException(403); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
return parent::verifyUserCanAccessChannel( |
|
|
|
|
28
|
|
|
$request, str_replace(['private-', 'presence-'], '', $request->channel_name) |
29
|
|
|
); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Return the valid authentication response. |
34
|
|
|
* |
35
|
|
|
* @param \Illuminate\Http\Request $request |
36
|
|
|
* @param mixed $result |
37
|
|
|
* @return mixed |
38
|
|
|
*/ |
39
|
|
|
public function validAuthenticationResponse($request, $result) |
40
|
|
|
{ |
41
|
|
|
if (is_bool($result)) { |
42
|
|
|
return json_encode($result); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return json_encode(['channel_data' => [ |
46
|
|
|
'user_id' => $request->user()->getKey(), |
47
|
|
|
'user_info' => $result, |
48
|
|
|
]]); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Broadcast the given event. |
53
|
|
|
* |
54
|
|
|
* @param array $channels |
55
|
|
|
* @param string $event |
56
|
|
|
* @param array $payload |
57
|
|
|
* @return void |
58
|
|
|
*/ |
59
|
|
|
public function broadcast(array $channels, $event, array $payload = []) |
60
|
|
|
{ |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
$socket = Arr::pull($payload, 'socket'); |
64
|
|
|
|
65
|
|
|
$payload = [ |
66
|
|
|
'event' => $event, 'data' => $payload, 'socket' => $socket, |
67
|
|
|
]; |
68
|
|
|
|
69
|
|
|
foreach ($this->formatChannels($channels) as $channel) { |
70
|
|
|
LaravooleFacade::task(['channel' => $channel, 'payload' => $payload]); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()
method in theSon
calls the wrong method in the parent class.