|
1
|
|
|
<?php |
|
2
|
|
|
namespace PHPDaemon\SockJS; |
|
3
|
|
|
|
|
4
|
|
|
use PHPDaemon\Core\Timer; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* @package Libraries |
|
8
|
|
|
* @subpackage SockJS |
|
9
|
|
|
* @author Vasily Zorin <[email protected]> |
|
10
|
|
|
*/ |
|
11
|
|
|
class WebSocketRouteProxy implements \PHPDaemon\WebSocket\RouteInterface |
|
12
|
|
|
{ |
|
13
|
|
|
use \PHPDaemon\Traits\StaticObjectWatchdog; |
|
14
|
|
|
|
|
15
|
|
|
protected $heartbeatTimer; |
|
16
|
|
|
|
|
17
|
|
|
protected $realRoute; |
|
18
|
|
|
|
|
19
|
|
|
protected $sockjs; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* __construct |
|
23
|
|
|
* @param Application $sockjs |
|
24
|
|
|
* @param object $conn |
|
|
|
|
|
|
25
|
|
|
*/ |
|
26
|
|
|
public function __construct($sockjs, $route) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->sockjs = $sockjs; |
|
29
|
|
|
$this->realRoute = $route; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* __get |
|
34
|
|
|
* @param string $k |
|
35
|
|
|
* @return mixed |
|
36
|
|
|
*/ |
|
37
|
|
|
public function &__get($k) |
|
38
|
|
|
{ |
|
39
|
|
|
return $this->realRoute->{$k}; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* __call |
|
44
|
|
|
* @param string $method |
|
45
|
|
|
* @param array $args |
|
46
|
|
|
* @return mixed |
|
47
|
|
|
*/ |
|
48
|
|
|
public function __call($method, $args) |
|
49
|
|
|
{ |
|
50
|
|
|
$func = [$this->realRoute, $method]; |
|
51
|
|
|
$func(...$args); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Called when new frame received. |
|
56
|
|
|
* @param string $data Frame's contents. |
|
57
|
|
|
* @param integer $type Frame's type. |
|
58
|
|
|
* @return void |
|
59
|
|
|
*/ |
|
60
|
|
|
public function onFrame($data, $type) |
|
61
|
|
|
{ |
|
62
|
|
|
foreach (explode("\n", $data) as $pct) { |
|
63
|
|
|
if ($pct === '') { |
|
64
|
|
|
continue; |
|
65
|
|
|
} |
|
66
|
|
|
$pct = json_decode($pct, true); |
|
67
|
|
|
if (isset($pct[0])) { |
|
68
|
|
|
foreach ($pct as $i) { |
|
69
|
|
|
$this->onPacket(rtrim($i, "\n"), $type); |
|
70
|
|
|
} |
|
71
|
|
|
} else { |
|
72
|
|
|
$this->onPacket($pct, $type); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* onPacket |
|
79
|
|
|
* @param string $data Frame's contents. |
|
|
|
|
|
|
80
|
|
|
* @param integer $type Frame's type. |
|
81
|
|
|
* @return void |
|
82
|
|
|
*/ |
|
83
|
|
|
public function onPacket($frame, $type) |
|
84
|
|
|
{ |
|
85
|
|
|
$this->realRoute->onFrame($frame, $type); |
|
|
|
|
|
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* realRoute onBeforeHandshake |
|
90
|
|
|
* @param callable $cb |
|
91
|
|
|
* @return void|false |
|
92
|
|
|
*/ |
|
93
|
|
|
public function onBeforeHandshake($cb) |
|
94
|
|
|
{ |
|
95
|
|
|
if (!method_exists($this->realRoute, 'onBeforeHandshake')) { |
|
96
|
|
|
return false; |
|
97
|
|
|
} |
|
98
|
|
|
$this->realRoute->onBeforeHandshake($cb); |
|
|
|
|
|
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @TODO DESCR |
|
103
|
|
|
* @return void |
|
104
|
|
|
*/ |
|
105
|
|
|
public function onHandshake() |
|
106
|
|
|
{ |
|
107
|
|
|
$this->realRoute->client->sendFrameReal('o'); |
|
108
|
|
|
if (($f = $this->sockjs->config->heartbeatinterval->value) > 0) { |
|
109
|
|
|
$this->heartbeatTimer = setTimeout(function ($timer) { |
|
110
|
|
|
$this->realRoute->client->sendFrameReal('h'); |
|
111
|
|
|
$timer->timeout(); |
|
112
|
|
|
}, $f * 1e6); |
|
113
|
|
|
$this->realRoute->onHandshake(); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @TODO DESCR |
|
119
|
|
|
* @return void |
|
120
|
|
|
*/ |
|
121
|
|
|
public function onWrite() |
|
122
|
|
|
{ |
|
123
|
|
|
if (method_exists($this->realRoute, 'onWrite')) { |
|
124
|
|
|
$this->realRoute->onWrite(); |
|
|
|
|
|
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @TODO DESCR |
|
130
|
|
|
* @return void |
|
131
|
|
|
*/ |
|
132
|
|
|
public function onFinish() |
|
133
|
|
|
{ |
|
134
|
|
|
Timer::remove($this->heartbeatTimer); |
|
135
|
|
|
if ($this->realRoute) { |
|
136
|
|
|
$this->realRoute->onFinish(); |
|
137
|
|
|
$this->realRoute = null; |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.