clashapi: Remove traffic loop

This commit is contained in:
世界
2024-11-27 18:05:30 +08:00
parent fe9289aac2
commit 207d7a8d1b
3 changed files with 24 additions and 53 deletions

View File

@@ -34,7 +34,6 @@ func (s *CommandServer) readStatus() StatusMessage {
if clashServer := s.service.instance.Router().ClashServer(); clashServer != nil {
message.TrafficAvailable = true
trafficManager := clashServer.(*clashapi.Server).TrafficManager()
message.Uplink, message.Downlink = trafficManager.Now()
message.UplinkTotal, message.DownlinkTotal = trafficManager.Total()
message.ConnectionsIn = int32(trafficManager.ConnectionsLen())
}
@@ -52,8 +51,20 @@ func (s *CommandServer) handleStatusConn(conn net.Conn) error {
ticker := time.NewTicker(time.Duration(interval))
defer ticker.Stop()
ctx := connKeepAlive(conn)
var (
status StatusMessage
uploadTotal int64
downloadTotal int64
)
for {
err = binary.Write(conn, binary.BigEndian, s.readStatus())
status = s.readStatus()
upload := status.UplinkTotal - uploadTotal
download := status.DownlinkTotal - downloadTotal
uploadTotal = status.UplinkTotal
downloadTotal = status.DownlinkTotal
status.Uplink = upload
status.Downlink = download
err = binary.Write(conn, binary.BigEndian, status)
if err != nil {
return err
}