``feat(socket): 完善踢人流程日志记录,添加CloseChan确保数据保存完成``

This commit is contained in:
1
2025-10-16 18:59:38 +00:00
parent 52ea55ac39
commit 08fcc72b6d
6 changed files with 22 additions and 6 deletions

View File

@@ -29,7 +29,8 @@ type ServerHandler struct{}
// 实现踢人
func (h *ServerHandler) Kick(ctx context.Context, userid uint32) error {
cool.Loger.Info(context.TODO(),"服务器收到踢人" )
useid1, err := share.ShareManager.GetUserOnline(userid)
if err != nil {

View File

@@ -42,6 +42,7 @@ func (s *Server) Stop() error {
}
func (s *Server) OnClose(c gnet.Conn, _ error) (action gnet.Action) {
defer func() {
if r := recover(); r != nil {
fmt.Println("Recovered in f", r)
@@ -52,8 +53,13 @@ func (s *Server) OnClose(c gnet.Conn, _ error) (action gnet.Action) {
v, ok := c.Context().(*player.ClientData)
if ok && v.Player != nil {
cool.Loger.Info(context.TODO(), "准备保存", v.Player.Info.UserID)
v.Player.Save() //保存玩家数据
cool.Loger.Info(context.TODO(), "保存完成", v.Player.Info.UserID)
if v.CloseChan != nil {
close(v.CloseChan)
}
}
//}

View File

@@ -1,13 +1,14 @@
package socket
import (
"blazing/cool"
"blazing/logic/service/player"
"context"
"fmt"
)
func (h *Server) KickPerson(a int) error {
fmt.Println("检测到踢人请求", a)
cool.Loger.Info(context.TODO(), "检测到踢人请求", a)
return player.KickPlayer(uint32(a))
}

View File

@@ -2,6 +2,7 @@ package controller
import (
"blazing/common/data/share"
"blazing/cool"
"fmt"
"blazing/common/socket/errorcode"
@@ -35,11 +36,15 @@ func IsToday(t time.Time) bool {
func (h *Controller) Login(data *user.MAIN_LOGIN_IN, c gnet.Conn) (result *user.LoginMSInfo, err errorcode.ErrorCode) { //这个时候player应该是空的
if tt := data.CheakSession(); tt { //说明sid正确
cool.Loger.Info(context.TODO(), "准备踢人")
err := h.RPCClient.Kick(data.Head.UserID) //先踢人
if err != nil {
fmt.Println("踢人失败", err)
}
<-time.After(time.Millisecond * 3000)
//player.KickPlayer(data.Head.UserID)
cool.Loger.Info(context.TODO(), "踢人请求完成,继续登录流程")
// <-time.After(time.Millisecond * 3000)
share.ShareManager.SetUserOnline(data.Head.UserID, h.Port) //设置用户登录服务器
t := player.GetPlayer(c, data.Head.UserID)
t.Service = blservice.NewUserService(data.Head.UserID)

View File

@@ -44,6 +44,7 @@ type ClientData struct {
Mu sync.Mutex
ERROR_CONNUT int
Wsmsg *WsCodec
CloseChan chan struct{}
}
func NewClientData() *ClientData {

View File

@@ -41,11 +41,13 @@ func KickPlayer(userid uint32) error { //踢出玩家
//实际上这里有个问题,会造成重复保存问题
player1.SendPack(head.Pack(nil))
player1.MainConn.Context().(*ClientData).CloseChan = make(chan struct{})
player1.MainConn.Context().(*ClientData).Mu.Lock()
player1.MainConn.Close()
player1.MainConn.Context().(*ClientData).Mu.Unlock()
// clientdata.Player = player
<-player1.MainConn.Context().(*ClientData).CloseChan
}
}