Player: now can get pingMs

This commit is contained in:
Sunch 2025-03-21 12:11:05 +08:00
parent d63cbc1722
commit a17074ce20
2 changed files with 21 additions and 3 deletions

View file

@ -84,6 +84,8 @@ class Player{
* @var boolean $isSneaking * @var boolean $isSneaking
*/ */
public $isJumping, $isSneaking; public $isJumping, $isSneaking;
private $lastPing = -1;
/** /**
* @param integer $clientID * @param integer $clientID
@ -1503,6 +1505,17 @@ class Player{
} }
} }
public function sendPing() {
$pk = new PingPacket;
$pk->time = intdiv(hrtime(true), 1_000_000);
$this->directDataPacket($pk);
}
public function getPing() {
return $this->lastPing;
}
public function handleDataPacket(RakNetDataPacket $packet){ public function handleDataPacket(RakNetDataPacket $packet){
if($this->connected === false){ if($this->connected === false){
return; return;
@ -1511,18 +1524,20 @@ class Player{
if(EventHandler::callEvent(new DataPacketReceiveEvent($this, $packet)) === BaseEvent::DENY){ if(EventHandler::callEvent(new DataPacketReceiveEvent($this, $packet)) === BaseEvent::DENY){
return; return;
} }
switch($packet->pid()){ switch($packet->pid()){
case 0x01: case 0x01:
break; break;
case ProtocolInfo::PONG_PACKET: case ProtocolInfo::PONG_PACKET:
$currentTime = intdiv(hrtime(true), 1_000_000);
if($currentTime > $packet->ptime){
$this->lastPing = $currentTime - $packet->ptime;
}
break; break;
case ProtocolInfo::PING_PACKET: case ProtocolInfo::PING_PACKET:
$pk = new PongPacket; $pk = new PongPacket;
$pk->ptime = $packet->time; $pk->ptime = $packet->time;
$pk->time = abs(microtime(true) * 1000); $pk->time = intdiv(hrtime(true), 1_000_000);
$this->directDataPacket($pk); $this->directDataPacket($pk);
break; break;
case ProtocolInfo::DISCONNECT_PACKET: case ProtocolInfo::DISCONNECT_PACKET:

View file

@ -618,6 +618,9 @@ class PocketMinecraftServer{
foreach($this->clients as $client){ foreach($this->clients as $client){
$client->handlePacketQueues(); $client->handlePacketQueues();
if($this->ticks % 40 == 0){ //2s
$client->sendPing();
}
} }
foreach($this->api->level->levels as $l){ foreach($this->api->level->levels as $l){