33 lines
641 B
GDScript
33 lines
641 B
GDScript
extends PacketHandler
|
|
|
|
signal changed(ping: float)
|
|
|
|
const HISTORY_SIZE = 8
|
|
|
|
var count := 0
|
|
var history: Array[float] = []
|
|
|
|
func _init():
|
|
history.resize(HISTORY_SIZE)
|
|
|
|
func _ready():
|
|
%Client.set_handler(Packet.PING, self)
|
|
|
|
func send_ping():
|
|
var buffer := StreamPeerBuffer.new()
|
|
buffer.put_8(Packet.PING)
|
|
buffer.put_8(count)
|
|
%Client.socket.put_packet(buffer.data_array)
|
|
|
|
history[count] = Time.get_unix_time_from_system()
|
|
count += 1
|
|
|
|
if count >= HISTORY_SIZE:
|
|
count = 0
|
|
|
|
func handle_packet(data: PackedByteArray, _peer: PacketPeer):
|
|
var id := data[0]
|
|
var ping := Time.get_unix_time_from_system() - history[id]
|
|
changed.emit(ping)
|
|
|