Compare commits

...

2 Commits

Author SHA1 Message Date
ed52397ea9 增加server status和mega type 2025-08-11 11:55:19 +08:00
5ab67999d3 修复websocket 2025-08-11 11:54:58 +08:00
3 changed files with 78 additions and 16 deletions

View File

@ -13,6 +13,16 @@ enum ServerPacketType {
LEAVE = 6;
LIST = 7;
TOTAL = 8;
STATUS = 9;
}
enum ServerStatus {
FREE = 0;
WAITING = 1;
STARTING = 2;
PLAYING = 3;
ENDING = 4;
FULL = 5;
}
message ServerPacket {
@ -49,4 +59,8 @@ message ServerListPacket {
message ServerTotalPacket {
int32 total = 1;
}
message ServerStatusPacket {
ServerStatus status = 1;
}

View File

@ -22,6 +22,7 @@ const (
ServerTypeRoom ServerType = "ROOM"
ServerTypeLimbo ServerType = "LIMBO"
ServerTypeSystem ServerType = "SYSTEM"
ServerTypeMega ServerType = "MAGA" // make america great again
)
func (typ ServerType) Prefix() string {
@ -34,6 +35,8 @@ func (typ ServerType) Prefix() string {
return ""
case ServerTypeSystem:
return "S"
case ServerTypeMega:
return "mega"
case ServerTypeUnknown:
default:
return "U"
@ -41,6 +44,17 @@ func (typ ServerType) Prefix() string {
return "U"
}
type Status string
const (
ServerStatusFree Status = "FREE"
ServerStatusWaiting Status = "WAITING"
ServerStatusStarting Status = "STARTING"
ServerStatusFull Status = "FULL"
ServerStatusPlaying Status = "PLAYING"
ServerStatusEnding Status = "ENDING"
)
type Server struct {
Id string `json:"id"`
Name string `json:"name"`
@ -50,6 +64,7 @@ type Server struct {
Type ServerType `json:"type"`
Motd string `json:"motd"`
Players []string `json:"players"`
Status Status `json:"status"`
LastHB time.Time
}
@ -150,12 +165,14 @@ func ServerRegister(c *gin.Context) {
}
server := &Server{
Id: generateServerId(data.Type.Prefix()),
Host: data.Host,
Port: data.Port,
Type: data.Type,
Group: data.Group,
LastHB: time.Now(),
Id: generateServerId(data.Type.Prefix()),
Host: data.Host,
Port: data.Port,
Motd: data.Motd,
Type: data.Type,
Group: data.Group,
Players: make([]string, 0),
LastHB: time.Now(),
}
if data.Name == "AUTO" {
@ -336,6 +353,32 @@ func serverReadLoop(conn *ws.Conn, serverId string) {
conn.WriteChan <- ws.Binary(buf)
return
}
case ServerPacketType_STATUS:
var payload ServerStatusPacket
err := proto.Unmarshal(pkt.GetPayload(), &payload)
if err != nil {
panic(err)
}
switch payload.Status {
case ServerStatus_FREE:
server.Status = ServerStatusFull
break
case ServerStatus_WAITING:
server.Status = ServerStatusWaiting
break
case ServerStatus_STARTING:
server.Status = ServerStatusStarting
break
case ServerStatus_PLAYING:
server.Status = ServerStatusPlaying
break
case ServerStatus_ENDING:
server.Status = ServerStatusEnding
break
case ServerStatus_FULL:
server.Status = ServerStatusFull
break
}
case ServerPacketType_TOTAL:
total := 0
servers.Range(func(_, value any) bool {
@ -358,10 +401,8 @@ func serverReadLoop(conn *ws.Conn, serverId string) {
}
conn.WriteChan <- ws.Binary(buf)
}
default:
if conn.IsClosed() {
return
}
case <-conn.Context.Done():
return
}
}
}

View File

@ -3,6 +3,7 @@ package ws
import (
"context"
"github.com/gorilla/websocket"
"log"
)
type Packet struct {
@ -27,16 +28,21 @@ func Binary(buf []byte) *Packet {
type Conn struct {
WriteChan chan *Packet
ReadChan chan []byte
ctx context.Context
Context context.Context
cancel context.CancelFunc
conn *websocket.Conn
close bool
}
func Wrap(conn *websocket.Conn) *Conn {
ctx, cancel := context.WithCancel(context.Background())
return &Conn{
WriteChan: make(chan *Packet),
ReadChan: make(chan []byte),
conn: conn,
Context: ctx,
cancel: cancel,
close: false,
}
}
@ -44,6 +50,9 @@ func (conn *Conn) ReadLoop() {
for !conn.close {
_, message, err := conn.conn.ReadMessage()
if err != nil {
if !websocket.IsCloseError(err, websocket.CloseNormalClosure, websocket.CloseAbnormalClosure) {
log.Println("websocket read error:", err)
}
conn.Close()
break
}
@ -63,16 +72,14 @@ func (conn *Conn) WriteLoop() {
if err != nil {
break
}
default:
if conn.close {
return
}
case <-conn.Context.Done():
return
}
}
}
func (conn *Conn) Close() error {
conn.ReadChan <- nil
conn.cancel()
conn.close = true
return conn.conn.Close()
}