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