10
10
use Kdt \Iron \Nova \Network \Client as NovaPingClient ;
11
11
use Kdt \Iron \Nova \Exception \NetworkException ;
12
12
use Zan \Framework \Utilities \Types \Time ;
13
+ use ZanPHP \Contracts \LoadBalance \Node ;
13
14
14
- class NovaClient extends Base implements Connection
15
+ class NovaClient extends Base implements Connection, Node
15
16
{
16
17
private $ clientCb ;
17
18
protected $ isAsync = true ;
@@ -34,15 +35,17 @@ protected function closeSocket()
34
35
}
35
36
}
36
37
37
- public function init () {
38
+ public function init ()
39
+ {
38
40
//set callback
39
41
$ this ->getSocket ()->on ('connect ' , [$ this , 'onConnect ' ]);
40
42
$ this ->getSocket ()->on ('receive ' , [$ this , 'onReceive ' ]);
41
43
$ this ->getSocket ()->on ('close ' , [$ this , 'onClose ' ]);
42
44
$ this ->getSocket ()->on ('error ' , [$ this , 'onError ' ]);
43
45
}
44
46
45
- public function onConnect (SwooleClient $ cli ) {
47
+ public function onConnect (SwooleClient $ cli )
48
+ {
46
49
//put conn to active_pool
47
50
Timer::clearAfterJob ($ this ->getConnectTimeoutJobId ());
48
51
Timer::clearAfterJob ($ this ->getHeartbeatingJobId ());
@@ -54,14 +57,16 @@ public function onConnect(SwooleClient $cli) {
54
57
$ this ->inspect ("connect to server " , $ cli );
55
58
}
56
59
57
- public function onClose (SwooleClient $ cli ){
60
+ public function onClose (SwooleClient $ cli )
61
+ {
58
62
Timer::clearAfterJob ($ this ->getConnectTimeoutJobId ());
59
63
Timer::clearAfterJob ($ this ->getHeartbeatingJobId ());
60
64
$ this ->close ();
61
65
$ this ->inspect ("close " , $ cli );
62
66
}
63
67
64
- public function onReceive (SwooleClient $ cli , $ data ) {
68
+ public function onReceive (SwooleClient $ cli , $ data )
69
+ {
65
70
try {
66
71
call_user_func ($ this ->clientCb , $ data );
67
72
} catch (\Throwable $ t ) {
@@ -71,17 +76,20 @@ public function onReceive(SwooleClient $cli, $data) {
71
76
}
72
77
}
73
78
74
- public function onError (SwooleClient $ cli ){
79
+ public function onError (SwooleClient $ cli )
80
+ {
75
81
Timer::clearAfterJob ($ this ->getConnectTimeoutJobId ());
76
82
Timer::clearAfterJob ($ this ->getHeartbeatingJobId ());
77
83
$ this ->close ();
78
84
79
85
$ this ->inspect ("error " , $ cli , true );
80
86
}
81
87
82
- public function setClientCb (callable $ cb ) {
88
+ public function setClientCb (callable $ cb )
89
+ {
83
90
$ this ->clientCb = $ cb ;
84
91
}
92
+
85
93
public function heartbeat ()
86
94
{
87
95
Timer::after ($ this ->config ['heartbeat-time ' ], [$ this , 'heartbeating ' ], $ this ->getHeartbeatingJobId ());
@@ -153,4 +161,13 @@ private function inspect($desc, SwooleClient $cli, $error = false)
153
161
154
162
sys_echo ("nova client $ desc [ " . implode (", " , $ buffer ) . "] " );
155
163
}
164
+
165
+ /**
166
+ * 0 ~ 100
167
+ * @return int|null
168
+ */
169
+ public function getWeight ()
170
+ {
171
+ return isset ($ this ->config ["weight " ]) ? $ this ->config ["weight " ] : 100 ;
172
+ }
156
173
}
0 commit comments