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\Peer\Client as PeerClient; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class Client |
16
|
|
|
* Client class. |
17
|
|
|
* |
18
|
|
|
* @author Donii Sergii <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class Client extends PeerClient |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Path to WAMP routes |
24
|
|
|
* |
25
|
|
|
* @var string|null |
26
|
|
|
* |
27
|
|
|
* @author Donii Sergii <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
protected $routePath = null; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Retry connection on close or no |
33
|
|
|
* |
34
|
|
|
* @var bool |
35
|
|
|
* |
36
|
|
|
* @author Donii Sergii <[email protected]> |
37
|
|
|
*/ |
38
|
|
|
protected $connectionRetry = true; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Session start event |
42
|
|
|
* |
43
|
|
|
* @param \Thruway\ClientSession $session |
44
|
|
|
* @param \Thruway\Transport\TransportInterface $transport |
45
|
|
|
* |
46
|
|
|
* @author Donii Sergii <[email protected]> |
47
|
|
|
*/ |
48
|
3 |
|
public function onSessionStart($session, $transport) |
49
|
|
|
{ |
50
|
3 |
|
$this->includeRoutes($session, $transport); |
51
|
3 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
1 |
|
public function start($startLoop = true) |
57
|
|
|
{ |
58
|
1 |
|
app()->wampRouter->parseGroups(); |
|
|
|
|
59
|
1 |
|
parent::start($startLoop); |
60
|
1 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
|
|
public function onClose($reason, $retry = true) |
66
|
|
|
{ |
67
|
|
|
$this->connectionRetry = $retry; |
68
|
|
|
|
69
|
|
|
parent::onClose($reason); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
|
|
public function retryConnection() |
76
|
|
|
{ |
77
|
|
|
if (!$this->connectionRetry) { |
78
|
|
|
return; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return parent::retryConnection(); |
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
2 |
|
public function setRoutePath($path) |
85
|
|
|
{ |
86
|
2 |
|
$this->routePath = $path; |
87
|
2 |
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Include routes |
91
|
|
|
* |
92
|
|
|
* @param \Thruway\ClientSession $session Client session |
93
|
|
|
* @param \Thruway\Transport\TransportInterface $transport Transport provider |
94
|
|
|
* |
95
|
|
|
* @author Donii Sergii <[email protected]> |
96
|
|
|
*/ |
97
|
3 |
|
protected function includeRoutes($session, $transport) |
98
|
|
|
{ |
99
|
3 |
|
if (!is_dir($this->routePath) && !is_file($this->routePath)) { |
100
|
1 |
|
return; |
101
|
|
|
} |
102
|
|
|
|
103
|
2 |
|
$session = $this->session = $session; |
104
|
2 |
|
$client = $this; |
105
|
|
|
|
106
|
|
|
/* @scrutinizer ignore-call */ |
107
|
2 |
|
app()->wampRouter->setClient($client); |
108
|
|
|
|
109
|
2 |
|
if (is_file($this->routePath)) { |
110
|
1 |
|
require $this->routePath; |
111
|
|
|
|
112
|
1 |
|
return; |
113
|
|
|
} |
114
|
|
|
|
115
|
1 |
|
if (is_dir($this->routePath)) { |
116
|
1 |
|
$files = scandir($this->routePath); |
117
|
|
|
|
118
|
1 |
|
foreach ($files as $file) { |
119
|
1 |
|
if ($file === '.' || $file === '..') { |
120
|
1 |
|
continue; |
121
|
|
|
} |
122
|
|
|
|
123
|
1 |
|
if (pathinfo($file, PATHINFO_EXTENSION) === 'php') { |
124
|
1 |
|
require $this->routePath.DIRECTORY_SEPARATOR.$file; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
} |
128
|
1 |
|
} |
129
|
|
|
} |
130
|
|
|
|