1
|
|
|
<?php |
2
|
|
|
namespace PHPDaemon\WebSocket; |
3
|
|
|
|
4
|
|
|
use PHPDaemon\Core\Daemon; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Web socket route |
8
|
|
|
* |
9
|
|
|
* @package Core |
10
|
|
|
* |
11
|
|
|
* @author Vasily Zorin <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class Route implements RouteInterface |
14
|
|
|
{ |
15
|
|
|
use \PHPDaemon\Traits\StaticObjectWatchdog; |
16
|
|
|
use \PHPDaemon\Traits\Sessions; |
17
|
|
|
use \PHPDaemon\Traits\DeferredEventHandlers; |
18
|
|
|
|
19
|
|
|
public $attrs; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var \PHPDaemon\Servers\WebSocket\Connection |
23
|
|
|
*/ |
24
|
|
|
public $client; // Remote client |
25
|
|
|
/** |
26
|
|
|
* @var \PHPDaemon\Core\AppInstance |
27
|
|
|
*/ |
28
|
|
|
public $appInstance; |
29
|
|
|
|
30
|
|
|
protected $running = true; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Called when client connected. |
34
|
|
|
* @param \PHPDaemon\Servers\WebSocket\Connection $client Remote client |
35
|
|
|
* @param \PHPDaemon\Core\AppInstance $appInstance |
|
|
|
|
36
|
|
|
*/ |
37
|
|
|
public function __construct($client, $appInstance = null) |
38
|
|
|
{ |
39
|
|
|
$this->client = $client; |
40
|
|
|
|
41
|
|
|
$this->attrs = new \stdClass; |
42
|
|
|
$this->attrs->get =& $client->get; |
43
|
|
|
$this->attrs->cookie =& $client->cookie; |
44
|
|
|
$this->attrs->server =& $client->server; |
45
|
|
|
$this->attrs->session = null; |
46
|
|
|
|
47
|
|
|
if ($appInstance) { |
48
|
|
|
$this->appInstance = $appInstance; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Set the cookie |
54
|
|
|
* @param string $name Name of cookie |
55
|
|
|
* @param string $value Value |
56
|
|
|
* @param integer $maxage Optional. Max-Age. Default is 0. |
57
|
|
|
* @param string $path Optional. Path. Default is empty string. |
58
|
|
|
* @param string $domain Optional. Domain. Default is empty string. |
59
|
|
|
* @param boolean $secure Optional. Secure. Default is false. |
60
|
|
|
* @param boolean $HTTPOnly Optional. HTTPOnly. Default is false. |
61
|
|
|
* @return void |
62
|
|
|
*/ |
63
|
|
View Code Duplication |
public function setcookie( |
|
|
|
|
64
|
|
|
$name, |
65
|
|
|
$value = '', |
66
|
|
|
$maxage = 0, |
67
|
|
|
$path = '', |
68
|
|
|
$domain = '', |
69
|
|
|
$secure = false, |
70
|
|
|
$HTTPOnly = false |
71
|
|
|
) { |
72
|
|
|
$this->client->header( |
73
|
|
|
'Set-Cookie: ' . $name . '=' . rawurlencode($value) |
74
|
|
|
. (empty($domain) ? '' : '; Domain=' . $domain) |
75
|
|
|
. (empty($maxage) ? '' : '; Max-Age=' . $maxage) |
76
|
|
|
. (empty($path) ? '' : '; Path=' . $path) |
77
|
|
|
. (!$secure ? '' : '; Secure') |
78
|
|
|
. (!$HTTPOnly ? '' : '; HttpOnly'), |
79
|
|
|
false |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Called when the request wakes up |
85
|
|
|
* @return void |
86
|
|
|
*/ |
87
|
|
|
public function onWakeup() |
88
|
|
|
{ |
89
|
|
|
$this->running = true; |
90
|
|
|
Daemon::$context = $this; |
91
|
|
|
$_SESSION = &$this->attrs->session; |
92
|
|
|
$_GET = &$this->attrs->get; |
93
|
|
|
$_POST = []; |
94
|
|
|
$_COOKIE = &$this->attrs->cookie; |
95
|
|
|
Daemon::$process->setState(Daemon::WSTATE_BUSY); |
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Called when the request starts sleep |
100
|
|
|
* @return void |
101
|
|
|
*/ |
102
|
|
|
public function onSleep() |
103
|
|
|
{ |
104
|
|
|
Daemon::$context = null; |
105
|
|
|
$this->running = false; |
106
|
|
|
unset($_SESSION, $_GET, $_POST, $_COOKIE); |
107
|
|
|
Daemon::$process->setState(Daemon::WSTATE_IDLE); |
|
|
|
|
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Called when the connection is handshaked. |
112
|
|
|
* @return void |
113
|
|
|
*/ |
114
|
|
|
public function onHandshake() |
115
|
|
|
{ |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Called when new frame is received |
120
|
|
|
* @param string $data Frame's contents |
121
|
|
|
* @param integer $type Frame's type |
122
|
|
|
* @return void |
123
|
|
|
*/ |
124
|
|
|
public function onFrame($data, $type) |
125
|
|
|
{ |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Uncaught exception handler |
130
|
|
|
* @param $e |
131
|
|
|
* @return boolean Handled? |
132
|
|
|
*/ |
133
|
|
|
public function handleException($e) |
134
|
|
|
{ |
135
|
|
|
return false; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Called when session finished. |
140
|
|
|
* @return void |
141
|
|
|
*/ |
142
|
|
|
public function onFinish() |
143
|
|
|
{ |
144
|
|
|
$this->client = null; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Called when the worker is going to shutdown. |
149
|
|
|
* @return boolean Ready to shutdown? |
150
|
|
|
*/ |
151
|
|
|
public function gracefulShutdown() |
152
|
|
|
{ |
153
|
|
|
return true; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Get cookie by name |
158
|
|
|
* @param string $name Name of cookie |
159
|
|
|
* @return string Contents |
160
|
|
|
*/ |
161
|
|
|
protected function getCookieStr($name) |
162
|
|
|
{ |
163
|
|
|
return \PHPDaemon\HTTPRequest\Generic::getString($this->attrs->cookie[$name]); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Set session state |
168
|
|
|
* @param mixed $var |
169
|
|
|
* @return void |
170
|
|
|
*/ |
171
|
|
|
protected function setSessionState($var) |
172
|
|
|
{ |
173
|
|
|
$this->attrs->session = $var; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Get session state |
178
|
|
|
* @return mixed |
179
|
|
|
*/ |
180
|
|
|
protected function getSessionState() |
181
|
|
|
{ |
182
|
|
|
return $this->attrs->session; |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.