new packet: ServerTotalPacket

This commit is contained in:
2025-07-16 09:35:17 +08:00
parent 21a1b2d826
commit e31a48c4cd
4 changed files with 42 additions and 518 deletions

View File

@ -257,7 +257,10 @@ func serverReadLoop(conn *ws.Conn, serverId string) {
// 使用 Load 读取,避免并发问题
serverVal, ok := servers.Load(serverId)
if !ok {
conn.Close()
err := conn.Close()
if err != nil {
return
}
return
}
server := serverVal.(*Server)
@ -323,9 +326,37 @@ func serverReadLoop(conn *ws.Conn, serverId string) {
if err != nil {
panic(err)
}
buf, err = proto.Marshal(&ServerPacket{
Typ: ServerPacketType_LIST,
Payload: buf,
})
if err != nil {
panic(err)
}
conn.WriteChan <- ws.Binary(buf)
return
}
case ServerPacketType_TOTAL:
total := 0
servers.Range(func(_, value any) bool {
total += len(value.(*Server).Players)
return true
})
reply := &ServerTotalPacket{
Total: int32(total),
}
buf, err := proto.Marshal(reply)
if err != nil {
panic(err)
}
buf, err = proto.Marshal(&ServerPacket{
Typ: ServerPacketType_TOTAL,
Payload: buf,
})
if err != nil {
panic(err)
}
conn.WriteChan <- ws.Binary(buf)
}
default:
if conn.IsClosed() {
@ -397,7 +428,10 @@ func init() {
return true // 继续遍历
})
if time.Since(serverProxyLastHB) > time.Second*10 && serverProxyConn != nil {
serverProxyConn.Close()
err := serverProxyConn.Close()
if err != nil {
continue
}
}
}
}()