From 6c4b4e26bc0f270824b7f4698ee5af91394a192d Mon Sep 17 00:00:00 2001 From: Enoch Date: Sun, 10 Nov 2024 21:46:21 +0800 Subject: [PATCH] ~ --- src/Monitor.php | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/Monitor.php b/src/Monitor.php index 20534fc..68e1f15 100644 --- a/src/Monitor.php +++ b/src/Monitor.php @@ -6,13 +6,12 @@ use WebSocket\Client; class Detect { var $dest='ws://172.83.153.167:2345'; - var $nodeinfo; - var $ping; - var $delay; + public function node() { $nodeinfo = $this->callWebSocet('{"msg":"getnodelist"}'); $this->nodeinfo=$nodeinfo; + return $this; } public function getNode(){ if(!isset($this->nodeinfo)){ @@ -25,20 +24,27 @@ class Detect if(!isset($this->nodeinfo)){ $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){ $ping[$node->nodeid]['nodeinfo']=$node; } $alldelay = 0; - foreach($this->ping as $id=>$node){ + foreach($ping as $id=>$node){ if((!isset($node['nodeinfo'])) || (!isset($node['delay'])) || $node['delay']<=10){ - unset($this->ping[$id]); + unset($ping[$id]); }else{ $alldelay += $node['delay']; } } $this->ping=$ping; $this->delay=$alldelay/count($this->ping); + return $this; + } + public function getPing(){ + if(!isset($this->ping)){ + $this->ping(); + } + return $this->ping; } public function getDelay() { @@ -47,11 +53,12 @@ class Detect } return $this->delay; } - private function callWebSocet(){ + private function callWebSocet($msg){ $client = new Client($this->dest); // 发送消息 $client->send($msg); + $response = []; // 接收消息 while (true) {