feat 稳定300人

This commit is contained in:
2025-12-17 09:54:24 +08:00
parent 4983638c19
commit 0274262591
16 changed files with 467 additions and 234 deletions

View File

@@ -24,13 +24,13 @@ type Client struct {
cancel context.CancelFunc // 取消上下文
heartBeat time.Time // 最后一次心跳
UID int
UID int32 // 用户ID
SceneSID int64 // 场景服ID
InstanceID int
UniqueNo int64
InstanceID int32 // 副本ID副本类型
UniqueNo int64 // 副本唯一编号
}
func NewClient(uid int, conn socket.ISocketConn) *Client {
func NewClient(uid int32, conn socket.ISocketConn) *Client {
client := &Client{
UID: uid,
conn: conn,
@@ -64,7 +64,7 @@ func (c *Client) Loop() {
}
case <-t.C:
_ = c.conn.Ping()
if time.Now().Sub(c.heartBeat) > 120*time.Second {
if time.Now().Sub(c.heartBeat) > 60*time.Second {
return
}
}
@@ -86,6 +86,9 @@ func (c *Client) OnEvent(event Event) {
// WriteMessage 向客户端发送消息
func (c *Client) WriteMessage(id sc_pb.MessageID, data proto.Message) {
if c.conn.IsClose() {
return
}
d, err := proto.Marshal(data)
if err != nil {
c.logger.Errorf("WriteMessage proto.Marshal err: %v", err)
@@ -106,6 +109,9 @@ func (c *Client) WriteMessage(id sc_pb.MessageID, data proto.Message) {
// WriteBytes 向客户端发送字节数据
func (c *Client) WriteBytes(id sc_pb.MessageID, data []byte) {
if c.conn.IsClose() {
return
}
m, err := proto.Marshal(&sc_pb.Message{
ID: id,
Payload: data,
@@ -137,5 +143,6 @@ func (c *Client) onClose() {
c.mailChan = nil
}
UserMgr.Delete(c.UID)
c.onLeave()
c.Done()
}

View File

@@ -18,7 +18,6 @@ func (c *Client) handle(event Event) {
c.cancel()
return
}
c.logger.Infof("收到客户端消息:%v", msg.ID)
switch msg.ID {
case sc_pb.MessageID_MESSAGE_ID_ENTER_INSTANCE:
m := &sc_pb.C2S_EnterInstance{}
@@ -49,7 +48,7 @@ func (c *Client) onEnter(msg *sc_pb.C2S_EnterInstance) {
return
}
resp, err := client.Enter(c.ctx, &grpc_pb.EnterReq{
UID: int32(c.UID),
UID: c.UID,
GatewaySID: GatewaySID,
InstanceID: msg.InstanceID,
})
@@ -59,14 +58,26 @@ func (c *Client) onEnter(msg *sc_pb.C2S_EnterInstance) {
}
c.SceneSID = resp.SceneSID
c.UniqueNo = resp.UniqueNo
c.InstanceID = int(msg.InstanceID)
c.WriteMessage(sc_pb.MessageID_MESSAGE_ID_POSITION, &sc_pb.S2C_Position{
Info: []*sc_pb.PositionInfo{{
UID: int32(c.UID),
X: 1,
Y: 1,
}},
c.InstanceID = msg.InstanceID
c.WriteBytes(sc_pb.MessageID(resp.MessageID), resp.Payload)
}
func (c *Client) onLeave() {
client, err := service.SceneNewClient(c.SceneSID)
if err != nil {
c.logger.Errorf("SceneNewClient err: %v", err)
return
}
_, err = client.Leave(c.ctx, &grpc_pb.LeaveReq{
UID: c.UID,
GatewaySID: GatewaySID,
InstanceID: c.InstanceID,
UniqueNo: c.UniqueNo,
})
if err != nil {
c.logger.Errorf("leave err: %v", err)
return
}
}
func (c *Client) onAction(msg *sc_pb.C2S_Action) {
@@ -75,9 +86,11 @@ func (c *Client) onAction(msg *sc_pb.C2S_Action) {
}
if err := stream_client.SendMessageToScene(c.SceneSID, stream_client.FunAction, &grpc_pb.ActionReq{
UniqueNo: c.UniqueNo,
UID: int32(c.UID),
UID: c.UID,
Action: int32(msg.Action),
Payload: msg.Payload,
DirX: msg.DirX,
DirY: msg.DirY,
SkillID: msg.SkillID,
}); err != nil {
c.logger.Errorf("send action err: %v", err)
}

View File

@@ -7,35 +7,35 @@ import (
var UserMgr *userManager
type userManager struct {
userMap map[int]*Client
userMap map[int32]*Client
sync.RWMutex
}
func init() {
UserMgr = &userManager{
userMap: make(map[int]*Client),
userMap: make(map[int32]*Client),
}
}
func (m *userManager) Add(uid int, client *Client) {
func (m *userManager) Add(uid int32, client *Client) {
m.Lock()
defer m.Unlock()
m.userMap[uid] = client
}
func (m *userManager) Delete(uid int) {
func (m *userManager) Delete(uid int32) {
m.Lock()
defer m.Unlock()
delete(m.userMap, uid)
}
func (m *userManager) GetAll() map[int]*Client {
func (m *userManager) GetAll() map[int32]*Client {
m.RLock()
defer m.RUnlock()
return m.userMap
}
func (m *userManager) GetByUID(uid int) *Client {
func (m *userManager) GetByUID(uid int32) *Client {
m.RLock()
defer m.RUnlock()
return m.userMap[uid]