完善服务器

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 {

View File

@@ -0,0 +1,71 @@
package ws_handler
import (
"common/net/grpc/service"
"common/proto/gen/grpc_pb"
"encoding/json"
"gateway/grpc_server/stream_client"
"time"
)
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 "enter":
c.onEnter(m)
case "action":
c.onAction(m)
}
case *PongEvent:
c.heartBeat = time.Now()
}
}
func (c *Client) onEnter(msg *tempMsg) {
//_ = c.conn.Write(wapMsg(&tempMsg{
// Type: "init",
// Data: fmt.Sprintf("[%v,%v]", utils.RandInt(1, 100), utils.RandInt(1, 100)),
//}))
client, err := service.SceneNewClient()
if err != nil {
c.logger.Errorf("SceneNewClient err: %v", err)
return
}
resp, err := client.Enter(c.ctx, &grpc_pb.EnterReq{
UID: int32(c.UID),
SID: 0,
InstanceID: 1,
})
if err != nil {
c.logger.Errorf("enter err: %v", err)
return
}
c.SceneSID = resp.SID
}
func (c *Client) onAction(msg *tempMsg) {
if c.SceneSID == 0 {
return
}
d := &tempAction{}
if err := json.Unmarshal([]byte(msg.Data), d); err != nil {
return
}
m := &tempActionMove{}
if err := json.Unmarshal([]byte(d.Data), m); err != nil {
return
}
stream := stream_client.FindSceneBySID(c.SceneSID, stream_client.FunAction)
if err := stream.SendMsg(&grpc_pb.ActionReq{
Action: int32(m.Move),
}); 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[int32]*Client
userMap map[int]*Client
sync.RWMutex
}
func init() {
UserMgr = &userManager{
userMap: make(map[int32]*Client),
userMap: make(map[int]*Client),
}
}
func (m *userManager) Add(uid int32, client *Client) {
func (m *userManager) Add(uid int, client *Client) {
m.Lock()
defer m.Unlock()
m.userMap[uid] = client
}
func (m *userManager) Delete(uid int32) {
func (m *userManager) Delete(uid int) {
m.Lock()
defer m.Unlock()
delete(m.userMap, uid)
}
func (m *userManager) GetAll() map[int32]*Client {
func (m *userManager) GetAll() map[int]*Client {
m.RLock()
defer m.RUnlock()
return m.userMap
}
func (m *userManager) GetByUID(uid int32) *Client {
func (m *userManager) GetByUID(uid int) *Client {
m.RLock()
defer m.RUnlock()
return m.userMap[uid]

View File

@@ -2,20 +2,29 @@ package ws_handler
import "encoding/json"
type msg struct {
type tempMsg struct {
Type string `json:"type"`
Data string `json:"data"`
}
func parseMsg(data []byte) (*msg, error) {
m := &msg{}
type tempAction struct {
Action int `json:"action"`
Data string `json:"data"`
}
type tempActionMove struct {
Move int `json:"move"`
}
func parseMsg(data []byte) (*tempMsg, error) {
m := &tempMsg{}
if err := json.Unmarshal(data, m); err != nil {
return nil, err
}
return m, nil
}
func wapMsg(m *msg) []byte {
func wapMsg(m *tempMsg) []byte {
data, _ := json.Marshal(m)
return data
}