1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BeyondCode\LaravelWebSockets\WebSockets; |
4
|
|
|
|
5
|
|
|
use BeyondCode\LaravelWebSockets\Apps\App; |
6
|
|
|
use BeyondCode\LaravelWebSockets\Dashboard\DashboardLogger; |
7
|
|
|
use BeyondCode\LaravelWebSockets\Facades\StatisticsLogger; |
8
|
|
|
use BeyondCode\LaravelWebSockets\PubSub\ReplicationInterface; |
9
|
|
|
use BeyondCode\LaravelWebSockets\QueryParameters; |
10
|
|
|
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager; |
11
|
|
|
use BeyondCode\LaravelWebSockets\WebSockets\Exceptions\ConnectionsOverCapacity; |
12
|
|
|
use BeyondCode\LaravelWebSockets\WebSockets\Exceptions\OriginNotAllowed; |
13
|
|
|
use BeyondCode\LaravelWebSockets\WebSockets\Exceptions\UnknownAppKey; |
14
|
|
|
use BeyondCode\LaravelWebSockets\WebSockets\Exceptions\WebSocketException; |
15
|
|
|
use BeyondCode\LaravelWebSockets\WebSockets\Messages\PusherMessageFactory; |
16
|
|
|
use Exception; |
17
|
|
|
use Ratchet\ConnectionInterface; |
18
|
|
|
use Ratchet\RFC6455\Messaging\MessageInterface; |
19
|
|
|
use Ratchet\WebSocket\MessageComponentInterface; |
20
|
|
|
|
21
|
|
|
class WebSocketHandler implements MessageComponentInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* The channel manager. |
25
|
|
|
* |
26
|
|
|
* @var \BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager |
27
|
|
|
*/ |
28
|
|
|
protected $channelManager; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The replicator client. |
32
|
|
|
* |
33
|
|
|
* @var ReplicationInterface |
34
|
|
|
*/ |
35
|
|
|
protected $replicator; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Initialize a new handler. |
39
|
|
|
* |
40
|
|
|
* @param \BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager $channelManager |
41
|
|
|
* @return void |
|
|
|
|
42
|
|
|
*/ |
43
|
|
|
public function __construct(ChannelManager $channelManager) |
44
|
|
|
{ |
45
|
|
|
$this->channelManager = $channelManager; |
46
|
|
|
$this->replicator = app(ReplicationInterface::class); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Handle the socket opening. |
51
|
|
|
* |
52
|
|
|
* @param \Ratchet\ConnectionInterface $connection |
53
|
|
|
* @return void |
54
|
|
|
*/ |
55
|
|
|
public function onOpen(ConnectionInterface $connection) |
56
|
|
|
{ |
57
|
|
|
$this->verifyAppKey($connection) |
58
|
|
|
->verifyOrigin($connection) |
59
|
|
|
->limitConcurrentConnections($connection) |
60
|
|
|
->generateSocketId($connection) |
61
|
|
|
->establishConnection($connection); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Handle the incoming message. |
66
|
|
|
* |
67
|
|
|
* @param \Ratchet\ConnectionInterface $connection |
68
|
|
|
* @param \Ratchet\RFC6455\Messaging\MessageInterface $message |
69
|
|
|
* @return void |
70
|
|
|
*/ |
71
|
|
|
public function onMessage(ConnectionInterface $connection, MessageInterface $message) |
72
|
|
|
{ |
73
|
|
|
$message = PusherMessageFactory::createForMessage($message, $connection, $this->channelManager); |
74
|
|
|
|
75
|
|
|
$message->respond(); |
76
|
|
|
|
77
|
|
|
StatisticsLogger::webSocketMessage($connection->app->id); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Handle the websocket close. |
82
|
|
|
* |
83
|
|
|
* @param \Ratchet\ConnectionInterface $connection |
84
|
|
|
* @return void |
85
|
|
|
*/ |
86
|
|
|
public function onClose(ConnectionInterface $connection) |
87
|
|
|
{ |
88
|
|
|
$this->channelManager->removeFromAllChannels($connection); |
89
|
|
|
|
90
|
|
|
DashboardLogger::log($connection->app->id, DashboardLogger::TYPE_DISCONNECTED, [ |
|
|
|
|
91
|
|
|
'socketId' => $connection->socketId, |
|
|
|
|
92
|
|
|
]); |
93
|
|
|
|
94
|
|
|
StatisticsLogger::disconnection($connection->app->id); |
|
|
|
|
95
|
|
|
|
96
|
|
|
$this->replicator->unsubscribeFromApp($connection->app->id); |
|
|
|
|
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Handle the websocket errors. |
101
|
|
|
* |
102
|
|
|
* @param \Ratchet\ConnectionInterface $connection |
103
|
|
|
* @param WebSocketException $exception |
104
|
|
|
* @return void |
105
|
|
|
*/ |
106
|
|
|
public function onError(ConnectionInterface $connection, Exception $exception) |
107
|
|
|
{ |
108
|
|
|
if ($exception instanceof WebSocketException) { |
109
|
|
|
$connection->send(json_encode( |
110
|
|
|
$exception->getPayload() |
111
|
|
|
)); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$this->replicator->unsubscribeFromApp($connection->app->id); |
|
|
|
|
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Verify the app key validity. |
119
|
|
|
* |
120
|
|
|
* @param \Ratchet\ConnectionInterface $connection |
121
|
|
|
* @return $this |
122
|
|
|
*/ |
123
|
|
|
protected function verifyAppKey(ConnectionInterface $connection) |
124
|
|
|
{ |
125
|
|
|
$appKey = QueryParameters::create($connection->httpRequest)->get('appKey'); |
|
|
|
|
126
|
|
|
|
127
|
|
|
if (! $app = App::findByKey($appKey)) { |
128
|
|
|
throw new UnknownAppKey($appKey); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$connection->app = $app; |
|
|
|
|
132
|
|
|
|
133
|
|
|
return $this; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Verify the origin. |
138
|
|
|
* |
139
|
|
|
* @param \Ratchet\ConnectionInterface $connection |
140
|
|
|
* @return $this |
141
|
|
|
*/ |
142
|
|
|
protected function verifyOrigin(ConnectionInterface $connection) |
143
|
|
|
{ |
144
|
|
|
if (! $connection->app->allowedOrigins) { |
|
|
|
|
145
|
|
|
return $this; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
$header = (string) ($connection->httpRequest->getHeader('Origin')[0] ?? null); |
|
|
|
|
149
|
|
|
|
150
|
|
|
$origin = parse_url($header, PHP_URL_HOST) ?: $header; |
151
|
|
|
|
152
|
|
|
if (! $header || ! in_array($origin, $connection->app->allowedOrigins)) { |
|
|
|
|
153
|
|
|
throw new OriginNotAllowed($connection->app->key); |
|
|
|
|
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return $this; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Limit the connections count by the app. |
161
|
|
|
* |
162
|
|
|
* @param \Ratchet\ConnectionInterface $connection |
163
|
|
|
* @return $this |
164
|
|
|
*/ |
165
|
|
|
protected function limitConcurrentConnections(ConnectionInterface $connection) |
166
|
|
|
{ |
167
|
|
|
if (! is_null($capacity = $connection->app->capacity)) { |
|
|
|
|
168
|
|
|
$connectionsCount = $this->channelManager->getConnectionCount($connection->app->id); |
|
|
|
|
169
|
|
|
|
170
|
|
|
if ($connectionsCount >= $capacity) { |
171
|
|
|
throw new ConnectionsOverCapacity(); |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
return $this; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Create a socket id. |
180
|
|
|
* |
181
|
|
|
* @param \Ratchet\ConnectionInterface $connection |
182
|
|
|
* @return $this |
183
|
|
|
*/ |
184
|
|
|
protected function generateSocketId(ConnectionInterface $connection) |
185
|
|
|
{ |
186
|
|
|
$socketId = sprintf('%d.%d', random_int(1, 1000000000), random_int(1, 1000000000)); |
187
|
|
|
|
188
|
|
|
$connection->socketId = $socketId; |
|
|
|
|
189
|
|
|
|
190
|
|
|
return $this; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Establish connection with the client. |
195
|
|
|
* |
196
|
|
|
* @param \Ratchet\ConnectionInterface $connection |
197
|
|
|
* @return $this |
198
|
|
|
*/ |
199
|
|
|
protected function establishConnection(ConnectionInterface $connection) |
200
|
|
|
{ |
201
|
|
|
$connection->send(json_encode([ |
202
|
|
|
'event' => 'pusher:connection_established', |
203
|
|
|
'data' => json_encode([ |
204
|
|
|
'socket_id' => $connection->socketId, |
|
|
|
|
205
|
|
|
'activity_timeout' => 30, |
206
|
|
|
]), |
207
|
|
|
])); |
208
|
|
|
|
209
|
|
|
/** @var \GuzzleHttp\Psr7\Request $request */ |
210
|
|
|
$request = $connection->httpRequest; |
|
|
|
|
211
|
|
|
|
212
|
|
|
DashboardLogger::log($connection->app->id, DashboardLogger::TYPE_CONNECTED, [ |
|
|
|
|
213
|
|
|
'origin' => "{$request->getUri()->getScheme()}://{$request->getUri()->getHost()}", |
214
|
|
|
'socketId' => $connection->socketId, |
|
|
|
|
215
|
|
|
]); |
216
|
|
|
|
217
|
|
|
StatisticsLogger::connection($connection->app->id); |
|
|
|
|
218
|
|
|
|
219
|
|
|
$this->replicator->subscribeToApp($connection->app->id); |
|
|
|
|
220
|
|
|
|
221
|
|
|
return $this; |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.