1
|
|
|
<?php |
2
|
|
|
namespace PHPDaemon\SockJS; |
3
|
|
|
|
4
|
|
|
use PHPDaemon\HTTPRequest\Generic; |
5
|
|
|
use PHPDaemon\Core\Daemon; |
6
|
|
|
use PHPDaemon\Structures\ObjectStorage; |
7
|
|
|
use PHPDaemon\Core\Debug; |
8
|
|
|
use PHPDaemon\Servers\WebSocket\Pool as WebSocketPool; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @package Libraries |
12
|
|
|
* @subpackage SockJS |
13
|
|
|
* @author Vasily Zorin <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class Application extends \PHPDaemon\Core\AppInstance { |
16
|
|
|
protected $redis; |
17
|
|
|
protected $sessions; |
18
|
|
|
|
19
|
|
|
public $wss; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Setting default config options |
23
|
|
|
* @return array|bool |
24
|
|
|
*/ |
25
|
|
|
protected function getConfigDefaults() { |
26
|
|
|
return [ |
27
|
|
|
/* [string] @todo redis-name */ |
|
|
|
|
28
|
|
|
'redis-name' => '', |
29
|
|
|
|
30
|
|
|
/* [string] @todo redis-prefix */ |
|
|
|
|
31
|
|
|
'redis-prefix' => 'sockjs:', |
32
|
|
|
|
33
|
|
|
/* [string] @todo wss-name */ |
|
|
|
|
34
|
|
|
'wss-name' => '', |
35
|
|
|
|
36
|
|
|
/* [Double] @todo batch-delay */ |
|
|
|
|
37
|
|
|
'batch-delay' => new \PHPDaemon\Config\Entry\Double('0.05'), |
38
|
|
|
|
39
|
|
|
/* [Double] @todo heartbeat-interval */ |
|
|
|
|
40
|
|
|
'heartbeat-interval' => new \PHPDaemon\Config\Entry\Double('25'), |
41
|
|
|
|
42
|
|
|
/* [Time] @todo dead-session-timeout */ |
|
|
|
|
43
|
|
|
'dead-session-timeout' => new \PHPDaemon\Config\Entry\Time('1h'), |
44
|
|
|
|
45
|
|
|
/* [Size] @todo gc-max-response-size */ |
|
|
|
|
46
|
|
|
'gc-max-response-size' => new \PHPDaemon\Config\Entry\Size('128k'), |
47
|
|
|
|
48
|
|
|
/* [Time] @todo network-timeout-read */ |
|
|
|
|
49
|
|
|
'network-timeout-read' => new \PHPDaemon\Config\Entry\Time('2h'), |
50
|
|
|
|
51
|
|
|
/* [Time] @todo network-timeout-write */ |
|
|
|
|
52
|
|
|
'network-timeout-write' => new \PHPDaemon\Config\Entry\Time('120s'), |
53
|
|
|
]; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Set Redis instance |
58
|
|
|
* @param \PHPDaemon\Clients\Redis\Pool $redis |
59
|
|
|
* @return $this |
60
|
|
|
*/ |
61
|
|
|
public function setRedis(\PHPDaemon\Clients\Redis\Pool $redis) { |
62
|
|
|
$this->redis = $redis; |
63
|
|
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* getLocalSubscribersCount |
68
|
|
|
* @param string $chan |
69
|
|
|
* @return integer |
70
|
|
|
*/ |
71
|
|
|
public function getLocalSubscribersCount($chan) { |
72
|
|
|
return $this->redis->getLocalSubscribersCount($this->config->redisprefix->value . $chan); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* subscribe |
77
|
|
|
* @param string $chan [@todo description] |
|
|
|
|
78
|
|
|
* @param callable $cb [@todo description] |
|
|
|
|
79
|
|
|
* @param callable $opcb [@todo description] |
|
|
|
|
80
|
|
|
* @callback $cb ( ) |
81
|
|
|
* @callback $opcb ( ) |
82
|
|
|
* @return void |
83
|
|
|
*/ |
84
|
|
|
public function subscribe($chan, $cb, $opcb = null) { |
85
|
|
|
$this->redis->subscribe($this->config->redisprefix->value . $chan, $cb, $opcb); |
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* setnx |
90
|
|
|
* @param string $key [@todo description] |
|
|
|
|
91
|
|
|
* @param mixed $value [@todo description] |
|
|
|
|
92
|
|
|
* @param callable $cb [@todo description] |
|
|
|
|
93
|
|
|
* @callback $cb ( ) |
94
|
|
|
* @return void |
95
|
|
|
*/ |
96
|
|
|
public function setnx($key, $value, $cb = null) { |
97
|
|
|
$this->redis->setnx($this->config->redisprefix->value . $key, $value, $cb); |
|
|
|
|
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* setkey |
102
|
|
|
* @param string $key [@todo description] |
|
|
|
|
103
|
|
|
* @param mixed $value [@todo description] |
|
|
|
|
104
|
|
|
* @param callable $cb [@todo description] |
|
|
|
|
105
|
|
|
* @callback $cb ( ) |
106
|
|
|
* @return void |
107
|
|
|
*/ |
108
|
|
|
public function setkey($key, $value, $cb = null) { |
109
|
|
|
$this->redis->set($this->config->redisprefix->value . $key, $value, $cb); |
|
|
|
|
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* getkey |
114
|
|
|
* @param string $key [@todo description] |
|
|
|
|
115
|
|
|
* @param callable $cb [@todo description] |
|
|
|
|
116
|
|
|
* @callback $cb ( ) |
117
|
|
|
* @return void |
118
|
|
|
*/ |
119
|
|
|
public function getkey($key, $cb = null) { |
120
|
|
|
$this->redis->get($this->config->redisprefix->value . $key, $cb); |
|
|
|
|
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* expire |
125
|
|
|
* @param string $key [@todo description] |
|
|
|
|
126
|
|
|
* @param integer $seconds [@todo description] |
|
|
|
|
127
|
|
|
* @param callable $cb [@todo description] |
|
|
|
|
128
|
|
|
* @callback $cb ( ) |
129
|
|
|
* @return void |
130
|
|
|
*/ |
131
|
|
|
public function expire($key, $seconds, $cb = null) { |
132
|
|
|
$this->redis->expire($this->config->redisprefix->value . $key, $seconds, $cb); |
|
|
|
|
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* unsubscribe |
137
|
|
|
* @param string $chan [@todo description] |
|
|
|
|
138
|
|
|
* @param callable $cb [@todo description] |
|
|
|
|
139
|
|
|
* @param callable $opcb [@todo description] |
|
|
|
|
140
|
|
|
* @callback $cb ( ) |
141
|
|
|
* @callback $opcb ( ) |
142
|
|
|
* @return void |
143
|
|
|
*/ |
144
|
|
|
public function unsubscribe($chan, $cb, $opcb = null) { |
145
|
|
|
$this->redis->unsubscribe($this->config->redisprefix->value . $chan, $cb, $opcb); |
|
|
|
|
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* unsubscribeReal |
150
|
|
|
* @param string $chan [@todo description] |
|
|
|
|
151
|
|
|
* @param callable $opcb [@todo description] |
|
|
|
|
152
|
|
|
* @callback $opcb ( ) |
153
|
|
|
* @return void |
154
|
|
|
*/ |
155
|
|
|
public function unsubscribeReal($chan, $opcb = null) { |
156
|
|
|
$this->redis->unsubscribeReal($this->config->redisprefix->value . $chan, $opcb); |
|
|
|
|
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* publish |
161
|
|
|
* @param string $chan [@todo description] |
|
|
|
|
162
|
|
|
* @param callable $cb [@todo description] |
|
|
|
|
163
|
|
|
* @param callable $opcb [@todo description] |
|
|
|
|
164
|
|
|
* @callback $cb ( ) |
165
|
|
|
* @callback $opcb ( ) |
166
|
|
|
* @return void |
167
|
|
|
*/ |
168
|
|
|
public function publish($chan, $cb, $opcb = null) { |
169
|
|
|
$this->redis->publish($this->config->redisprefix->value . $chan, $cb, $opcb); |
|
|
|
|
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Called when the worker is ready to go |
174
|
|
|
* @return void |
175
|
|
|
*/ |
176
|
|
|
public function onReady() { |
177
|
|
|
$this->redis = \PHPDaemon\Clients\Redis\Pool::getInstance($this->config->redisname->value); |
178
|
|
|
$this->sessions = new ObjectStorage; |
179
|
|
|
$this->wss = new ObjectStorage; |
180
|
|
|
foreach (preg_split('~\s*;\s*~', $this->config->wssname->value) as $wssname) { |
181
|
|
|
$this->attachWss(WebSocketPool::getInstance(trim($wssname))); |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* onFinish |
187
|
|
|
* @return void |
188
|
|
|
*/ |
189
|
|
|
public function onFinish() { |
190
|
|
|
foreach ($this->attachedTo as $wss) { |
|
|
|
|
191
|
|
|
$this->detachWss($wss); |
192
|
|
|
} |
193
|
|
|
parent::onFinish(); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* attachWss |
198
|
|
|
* @param \PHPDaemon\Network\Pool $wss |
199
|
|
|
* @return boolean |
200
|
|
|
*/ |
201
|
|
|
public function attachWss($wss) { |
202
|
|
|
if ($this->wss->contains($wss)) { |
203
|
|
|
return false; |
204
|
|
|
} |
205
|
|
|
$this->wss->attach($wss); |
206
|
|
|
$wss->bind('customTransport', [$this, 'wsHandler']); |
|
|
|
|
207
|
|
|
return true; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* wsHandler |
212
|
|
|
* @param object $ws [@todo description] |
|
|
|
|
213
|
|
|
* @param string $path [@todo description] |
|
|
|
|
214
|
|
|
* @param object $client [@todo description] |
|
|
|
|
215
|
|
|
* @param callable $state [@todo description] |
|
|
|
|
216
|
|
|
* @callback $state ( object $route ) |
217
|
|
|
* @return boolean |
218
|
|
|
*/ |
219
|
|
|
public function wsHandler($ws, $path, $client, $state) { |
220
|
|
|
$e = explode('/', $path); |
221
|
|
|
$method = array_pop($e); |
222
|
|
|
$serverId = null; |
|
|
|
|
223
|
|
|
$sessId = null; |
|
|
|
|
224
|
|
|
if ($method !== 'websocket') { |
225
|
|
|
return false; |
226
|
|
|
} |
227
|
|
|
if (sizeof($e) < 3 || !isset($e[sizeof($e) - 2]) || !ctype_digit($e[sizeof($e) - 2])) { |
228
|
|
|
return false; |
229
|
|
|
} |
230
|
|
|
$sessId = array_pop($e); |
|
|
|
|
231
|
|
|
$serverId = array_pop($e); |
|
|
|
|
232
|
|
|
$path = implode('/', $e); |
233
|
|
|
$client = new WebSocketConnectionProxy($this, $client); |
234
|
|
|
$route = $ws->getRoute($path, $client, true); |
235
|
|
|
if (!$route) { |
236
|
|
|
$state($route); |
237
|
|
|
return false; |
238
|
|
|
} |
239
|
|
|
$route = new WebSocketRouteProxy($this, $route); |
240
|
|
|
$state($route); |
241
|
|
|
return true; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* detachWss |
246
|
|
|
* @param object $wss [@todo description] |
|
|
|
|
247
|
|
|
* @return boolean |
248
|
|
|
*/ |
249
|
|
|
public function detachWss($wss) { |
250
|
|
|
if (!$this->wss->contains($wss)) { |
251
|
|
|
return false; |
252
|
|
|
} |
253
|
|
|
$this->wss->detach($wss); |
254
|
|
|
$wss->unbind('transport', [$this, 'wsHandler']); |
255
|
|
|
return true; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* beginSession |
260
|
|
|
* @param string $path [@todo description] |
|
|
|
|
261
|
|
|
* @param string $sessId [@todo description] |
|
|
|
|
262
|
|
|
* @param string $server [@todo description] |
|
|
|
|
263
|
|
|
* @return object |
|
|
|
|
264
|
|
|
*/ |
265
|
|
|
public function beginSession($path, $sessId, $server) { |
266
|
|
|
$session = new Session($this, $sessId, $server); |
|
|
|
|
267
|
|
|
foreach ($this->wss as $wss) { |
268
|
|
|
if ($session->route = $wss->getRoute($path, $session)) { |
269
|
|
|
break; |
270
|
|
|
} |
271
|
|
|
} |
272
|
|
|
if (!$session->route) { |
273
|
|
|
return false; |
274
|
|
|
} |
275
|
|
|
$this->sessions->attach($session); |
276
|
|
|
$session->onHandshake(); |
277
|
|
|
return $session; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* getRouteOptions |
282
|
|
|
* @param string $path [@todo description] |
|
|
|
|
283
|
|
|
* @return array |
284
|
|
|
*/ |
285
|
|
|
public function getRouteOptions($path) { |
286
|
|
|
$opts = [ |
287
|
|
|
'websocket' => true, |
288
|
|
|
'origins' => ['*:*'], |
289
|
|
|
'cookie_needed' => false, |
290
|
|
|
]; |
291
|
|
|
foreach ($this->wss as $wss) { |
292
|
|
|
if ($wss->routeExists($path)) { |
293
|
|
|
foreach ($wss->getRouteOptions($path) as $k => $v) { |
294
|
|
|
$opts[$k] = $v; |
295
|
|
|
} |
296
|
|
|
break; |
297
|
|
|
} |
298
|
|
|
} |
299
|
|
|
return $opts; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* endSession |
304
|
|
|
* @param Session $session |
305
|
|
|
* @return void |
306
|
|
|
*/ |
307
|
|
|
public function endSession($session) { |
308
|
|
|
$this->sessions->detach($session); |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* Creates Request. |
313
|
|
|
* @param object $req Request. |
314
|
|
|
* @param object $upstream Upstream application instance. |
315
|
|
|
* @return object Request. |
316
|
|
|
*/ |
317
|
|
|
public function beginRequest($req, $upstream) { |
318
|
|
|
$e = array_map('rawurldecode', explode('/', $req->attrs->server['DOCUMENT_URI'])); |
319
|
|
|
|
320
|
|
|
$serverId = null; |
321
|
|
|
$sessId = null; |
322
|
|
|
|
323
|
|
|
/* Route discovery */ |
324
|
|
|
$path = null; |
325
|
|
|
$extra = []; |
326
|
|
|
do { |
327
|
|
|
foreach ($this->wss as $wss) { |
328
|
|
|
$try = implode('/', $e); |
329
|
|
|
if ($try === '') { |
330
|
|
|
$try = '/'; |
331
|
|
|
} |
332
|
|
|
if ($wss->routeExists($try)) { |
333
|
|
|
$path = $try; |
334
|
|
|
break 2; |
335
|
|
|
} |
336
|
|
|
} |
337
|
|
|
array_unshift($extra, array_pop($e)); |
338
|
|
|
} while (sizeof($e) > 0); |
339
|
|
|
|
340
|
|
|
if ($path === null) { |
341
|
|
|
return $this->callMethod('NotFound', $req, $upstream); |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
if (sizeof($extra) > 0 && end($extra) === '') { |
345
|
|
|
array_pop($extra); |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
$method = sizeof($extra) ? array_pop($extra) : null; |
349
|
|
|
|
350
|
|
|
if ($method === null) { |
351
|
|
|
$method = 'Welcome'; |
352
|
|
|
} |
353
|
|
|
elseif ($method === 'info') { |
|
|
|
|
354
|
|
|
|
355
|
|
|
} |
356
|
|
|
elseif (preg_match('~^iframe(?:-([^/]*))?\.html$~', $method, $m)) { |
357
|
|
|
$method = 'Iframe'; |
358
|
|
|
$version = isset($m[1]) ? $m[1] : null; |
359
|
|
|
} else { |
360
|
|
|
if (sizeof($extra) < 2) { |
361
|
|
|
return $this->callMethod('NotFound', $req, $upstream); |
362
|
|
|
} |
363
|
|
|
$sessId = array_pop($extra); |
364
|
|
|
$serverId = array_pop($extra); |
365
|
|
|
if ($sessId === '' || $serverId === '' || strpos($sessId, '.') !== false || strpos($serverId, '.') !== false) { |
366
|
|
|
return $this->callMethod('NotFound', $req, $upstream); |
367
|
|
|
} |
368
|
|
|
} |
369
|
|
|
$req->attrs->sessId = $sessId; |
370
|
|
|
$req->attrs->serverId = $serverId; |
371
|
|
|
$req->attrs->path = $path; |
372
|
|
|
$req = $this->callMethod($method, $req, $upstream); |
373
|
|
|
if ($req instanceof Methods\Iframe && strlen($version)) { |
374
|
|
|
$req->attrs->version = $version; |
|
|
|
|
375
|
|
|
} |
376
|
|
|
return $req; |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
/** |
380
|
|
|
* callMethod |
381
|
|
|
* @param string $method [@todo description] |
|
|
|
|
382
|
|
|
* @param object $req [@todo description] |
|
|
|
|
383
|
|
|
* @param object $upstream [@todo description] |
|
|
|
|
384
|
|
|
* @return object |
385
|
|
|
*/ |
386
|
|
|
public function callMethod($method, $req, $upstream) { |
387
|
|
|
$method = strtr(ucwords(strtr($method, ['_' => ' '])), [' ' => '']); |
388
|
|
|
if (strtolower($method) === 'generic') { |
389
|
|
|
$method = 'NotFound'; |
390
|
|
|
} |
391
|
|
|
$class = __NAMESPACE__ . '\\Methods\\' . $method; |
392
|
|
|
if (!class_exists($class)) { |
393
|
|
|
$class = __NAMESPACE__ . '\\Methods\\NotFound'; |
394
|
|
|
} |
395
|
|
|
return new $class($this, $upstream, $req); |
396
|
|
|
} |
397
|
|
|
} |
398
|
|
|
|