-
Notifications
You must be signed in to change notification settings - Fork 98
/
Server.php
132 lines (107 loc) · 3.53 KB
/
Server.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
namespace Laravel\Reverb\Protocols\Pusher;
use Exception;
use Illuminate\Support\Str;
use Laravel\Reverb\Contracts\Connection;
use Laravel\Reverb\Events\MessageReceived;
use Laravel\Reverb\Loggers\Log;
use Laravel\Reverb\Protocols\Pusher\Contracts\ChannelManager;
use Laravel\Reverb\Protocols\Pusher\Exceptions\InvalidOrigin;
use Laravel\Reverb\Protocols\Pusher\Exceptions\PusherException;
class Server
{
/**
* Create a new server instance.
*/
public function __construct(protected ChannelManager $channels, protected EventHandler $handler)
{
//
}
/**
* Handle the a client connection.
*/
public function open(Connection $connection): void
{
try {
$this->verifyOrigin($connection);
$connection->touch();
$this->handler->handle($connection, 'pusher:connection_established');
Log::info('Connection Established', $connection->id());
} catch (Exception $e) {
$this->error($connection, $e);
}
}
/**
* Handle a new message received by the connected client.
*/
public function message(Connection $from, string $message): void
{
Log::info('Message Received', $from->id());
Log::message($message);
$from->touch();
try {
$event = json_decode($message, associative: true, flags: JSON_THROW_ON_ERROR);
match (Str::startsWith($event['event'], 'pusher:')) {
true => $this->handler->handle(
$from,
$event['event'],
$event['data'] ?? [],
$event['channel'] ?? null
),
default => ClientEvent::handle($from, $event)
};
Log::info('Message Handled', $from->id());
MessageReceived::dispatch($from, $message);
} catch (Exception $e) {
$this->error($from, $e);
}
}
/**
* Handle a client disconnection.
*/
public function close(Connection $connection): void
{
$this->channels
->for($connection->app())
->unsubscribeFromAll($connection);
$connection->disconnect();
Log::info('Connection Closed', $connection->id());
}
/**
* Handle an error.
*/
public function error(Connection $connection, Exception $exception): void
{
if ($exception instanceof PusherException) {
$connection->send(json_encode($exception->payload()));
Log::error('Message from '.$connection->id().' resulted in a pusher error');
Log::info($exception->getMessage());
return;
}
$connection->send(json_encode([
'event' => 'pusher:error',
'data' => json_encode([
'code' => 4200,
'message' => 'Invalid message format',
]),
]));
Log::error('Message from '.$connection->id().' resulted in an unknown error');
Log::info($exception->getMessage());
}
/**
* Verify the origin of the connection.
*
* @throws \Laravel\Reverb\Exceptions\InvalidOrigin
*/
protected function verifyOrigin(Connection $connection): void
{
$allowedOrigins = $connection->app()->allowedOrigins();
if (in_array('*', $allowedOrigins)) {
return;
}
$origin = parse_url($connection->origin(), PHP_URL_HOST);
if (! $origin || ! in_array($origin, $allowedOrigins)) {
throw new InvalidOrigin;
}
}
}