1 | <?php |
||
2 | /** |
||
3 | * Created by PhpStorm. |
||
4 | * |
||
5 | * @author Donii Sergii <[email protected]> |
||
6 | * Date: 10/25/17 |
||
7 | * Time: 12:18 PM |
||
8 | */ |
||
9 | |||
10 | namespace sonrac\WAMP; |
||
11 | |||
12 | use Thruway\ClientSession; |
||
13 | use Thruway\Message\Message; |
||
14 | use Thruway\Peer\Client as PeerClient; |
||
15 | |||
16 | /** |
||
17 | * Class Client |
||
18 | * Client class. |
||
19 | * |
||
20 | * @author Donii Sergii <[email protected]> |
||
21 | */ |
||
22 | class Client extends PeerClient |
||
23 | { |
||
24 | /** |
||
25 | * Path to WAMP routes |
||
26 | * |
||
27 | * @var string|null |
||
28 | * |
||
29 | * @author Donii Sergii <[email protected]> |
||
30 | */ |
||
31 | protected $routePath = null; |
||
32 | |||
33 | /** |
||
34 | * Retry connection on close or no |
||
35 | * |
||
36 | * @var bool |
||
37 | * |
||
38 | * @author Donii Sergii <[email protected]> |
||
39 | */ |
||
40 | protected $connectionRetry = true; |
||
41 | |||
42 | /** |
||
43 | * Realm |
||
44 | * |
||
45 | * @var string |
||
46 | * |
||
47 | * @author Donii Sergii <[email protected]> |
||
48 | */ |
||
49 | protected $realm; |
||
50 | |||
51 | /** |
||
52 | * Session start event |
||
53 | * |
||
54 | * @param \Thruway\ClientSession $session |
||
55 | * @param \Thruway\Transport\TransportInterface $transport |
||
56 | * |
||
57 | * @author Donii Sergii <[email protected]> |
||
58 | */ |
||
59 | 2 | public function onSessionStart($session, $transport) |
|
60 | { |
||
61 | 2 | $this->includeRoutes($session, $transport); |
|
62 | 2 | } |
|
63 | |||
64 | /** |
||
65 | * {@inheritdoc} |
||
66 | */ |
||
67 | public function start($startLoop = true) |
||
68 | { |
||
69 | parent::start($startLoop); |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * {@inheritdoc} |
||
74 | */ |
||
75 | public function onClose($reason, $retry = true) |
||
76 | { |
||
77 | $this->connectionRetry = $retry; |
||
78 | |||
79 | parent::onClose($reason); |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * {@inheritdoc} |
||
84 | */ |
||
85 | public function retryConnection() |
||
86 | { |
||
87 | if (!$this->connectionRetry) { |
||
88 | return; |
||
89 | } |
||
90 | |||
91 | return parent::retryConnection(); |
||
92 | } |
||
93 | |||
94 | 2 | public function setRoutePath($path) |
|
95 | { |
||
96 | 2 | $this->routePath = $path; |
|
97 | 2 | } |
|
98 | |||
99 | /** |
||
100 | * Set attribute connectionRetry |
||
101 | * |
||
102 | * @return bool |
||
103 | * |
||
104 | * @author Donii Sergii <[email protected]> |
||
105 | */ |
||
106 | public function isConnectionRetry(): bool |
||
107 | { |
||
108 | return $this->connectionRetry; |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * Set connection retry |
||
113 | * |
||
114 | * @param bool $connectionRetry |
||
115 | * |
||
116 | * @author Donii Sergii <[email protected]> |
||
117 | */ |
||
118 | public function setConnectionRetry(bool $connectionRetry) |
||
119 | { |
||
120 | $this->connectionRetry = $connectionRetry; |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * Include routes |
||
125 | * |
||
126 | * @param \Thruway\ClientSession $session Client session |
||
127 | * @param \Thruway\Transport\TransportInterface $transport Transport provider |
||
128 | * |
||
129 | * @author Donii Sergii <[email protected]> |
||
130 | */ |
||
131 | 2 | protected function includeRoutes($session, $transport) |
|
132 | { |
||
133 | 2 | if (!is_dir($this->routePath) && !is_file($this->routePath)) { |
|
134 | return; |
||
135 | } |
||
136 | |||
137 | 2 | if ($session instanceof ClientSession) { |
|
0 ignored issues
–
show
introduced
by
![]() |
|||
138 | $this->session = $session; |
||
139 | } |
||
140 | 2 | $client = $this; |
|
141 | |||
142 | /* @scrutinizer ignore-call */ |
||
143 | 2 | app()->wampRouter->setClient($client); |
|
144 | |||
145 | 2 | if (is_file($this->routePath)) { |
|
146 | 1 | require $this->routePath; |
|
147 | 1 | app()->wampRouter->parseGroups(); |
|
148 | 1 | return; |
|
149 | } |
||
150 | |||
151 | 1 | if (is_dir($this->routePath)) { |
|
152 | 1 | $files = scandir($this->routePath); |
|
153 | |||
154 | 1 | foreach ($files as $file) { |
|
155 | 1 | if ($file === '.' || $file === '..') { |
|
156 | 1 | continue; |
|
157 | } |
||
158 | |||
159 | 1 | if (pathinfo($file, PATHINFO_EXTENSION) === 'php') { |
|
160 | 1 | require $this->routePath.DIRECTORY_SEPARATOR.$file; |
|
161 | } |
||
162 | } |
||
163 | } |
||
164 | 1 | app()->wampRouter->parseGroups(); |
|
165 | 1 | } |
|
166 | |||
167 | /** |
||
168 | * {@inheritdoc} |
||
169 | */ |
||
170 | public function processOther(ClientSession $session = null, Message $msg = null) |
||
171 | { |
||
172 | if (!$session) { |
||
173 | return; |
||
174 | } |
||
175 | |||
176 | parent::processOther($session, $msg); |
||
177 | } |
||
178 | } |
||
179 |