完善服务器

This commit is contained in:
2025-07-01 00:08:27 +08:00
parent b45eb83fe4
commit 7c2c32a31a
37 changed files with 1307 additions and 160 deletions

View File

@@ -3,7 +3,6 @@ package ws_handler
import (
"common/log"
"common/net/socket"
"common/utils"
"context"
"fmt"
"go.uber.org/zap"
@@ -21,10 +20,11 @@ type Client struct {
cancel context.CancelFunc // 取消上下文
heartBeat time.Time // 最后一次心跳
UID int32
UID int
SceneSID int64 // 场景服ID
}
func NewClient(uid int32, conn socket.ISocketConn) *Client {
func NewClient(uid int, conn socket.ISocketConn) *Client {
client := &Client{
UID: uid,
conn: conn,
@@ -78,27 +78,6 @@ func (c *Client) OnEvent(event Event) {
}
}
func (c *Client) handle(event Event) {
switch e := event.(type) {
case *ClientEvent:
m, err := parseMsg(e.Msg)
if err != nil {
c.logger.Errorf("handle event json.Unmarshal err: %v", err)
c.cancel()
}
c.logger.Infof("收到客户端消息:%+v", *m)
switch m.Type {
case "init":
_ = c.conn.Write(wapMsg(&msg{
Type: "init",
Data: fmt.Sprintf("[%v,%v]", utils.RandInt(1, 100), utils.RandInt(1, 100)),
}))
}
case *PongEvent:
c.heartBeat = time.Now()
}
}
// CloseClient 关闭客户端同步会等待onClose执行完成
func (c *Client) CloseClient() {
if c.cancel != nil {