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