This commit is contained in:
Enoch 2024-11-10 21:46:21 +08:00
parent 6d56f09106
commit 6c4b4e26bc

View File

@ -6,13 +6,12 @@ use WebSocket\Client;
class Detect class Detect
{ {
var $dest='ws://172.83.153.167:2345'; var $dest='ws://172.83.153.167:2345';
var $nodeinfo;
var $ping;
var $delay;
public function node() public function node()
{ {
$nodeinfo = $this->callWebSocet('{"msg":"getnodelist"}'); $nodeinfo = $this->callWebSocet('{"msg":"getnodelist"}');
$this->nodeinfo=$nodeinfo; $this->nodeinfo=$nodeinfo;
return $this;
} }
public function getNode(){ public function getNode(){
if(!isset($this->nodeinfo)){ if(!isset($this->nodeinfo)){
@ -25,20 +24,27 @@ class Detect
if(!isset($this->nodeinfo)){ if(!isset($this->nodeinfo)){
$this->node(); $this->node();
} }
$ping = $this->callWebSocet('{"msg":"ping","target:'.$host.'","type":"ICMP","count":'.$count.'}'); $ping = $this->callWebSocet('{"msg":"ping","target":"'.$host.'","type":"ICMP","count":'.$count.'}');
foreach($this->nodeinfo as $node){ foreach($this->nodeinfo as $node){
$ping[$node->nodeid]['nodeinfo']=$node; $ping[$node->nodeid]['nodeinfo']=$node;
} }
$alldelay = 0; $alldelay = 0;
foreach($this->ping as $id=>$node){ foreach($ping as $id=>$node){
if((!isset($node['nodeinfo'])) || (!isset($node['delay'])) || $node['delay']<=10){ if((!isset($node['nodeinfo'])) || (!isset($node['delay'])) || $node['delay']<=10){
unset($this->ping[$id]); unset($ping[$id]);
}else{ }else{
$alldelay += $node['delay']; $alldelay += $node['delay'];
} }
} }
$this->ping=$ping; $this->ping=$ping;
$this->delay=$alldelay/count($this->ping); $this->delay=$alldelay/count($this->ping);
return $this;
}
public function getPing(){
if(!isset($this->ping)){
$this->ping();
}
return $this->ping;
} }
public function getDelay() public function getDelay()
{ {
@ -47,11 +53,12 @@ class Detect
} }
return $this->delay; return $this->delay;
} }
private function callWebSocet(){ private function callWebSocet($msg){
$client = new Client($this->dest); $client = new Client($this->dest);
// 发送消息 // 发送消息
$client->send($msg); $client->send($msg);
$response = [];
// 接收消息 // 接收消息
while (true) { while (true) {