refactor(socket): 重构 ClientData 结构体并优化相关逻辑
- 简化 ClientData 结构体,移除不必要的方法 - 优化 Player 结构体,调整 Conn 类型 - 更新 wscodec.go 中的 Conn 结构体 - 删除未使用的 XML 相关文件和代码 - 调整 ServerEvent 和 controller 中的相关逻辑
This commit is contained in:
@@ -1,9 +1,5 @@
|
||||
package socket
|
||||
|
||||
import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
func ConutPlayer() int {
|
||||
|
||||
count := 0
|
||||
@@ -15,46 +11,19 @@ func ConutPlayer() int {
|
||||
}
|
||||
|
||||
type ClientData struct {
|
||||
isCrossDomain bool //是否跨域过
|
||||
player *Player //客户实体
|
||||
IsCrossDomain bool //是否跨域过
|
||||
Player *Player //客户实体
|
||||
//UserID uint32
|
||||
m sync.Mutex
|
||||
|
||||
wsmsg WsCodec
|
||||
}
|
||||
|
||||
func (cd *ClientData) SetPlayer(player *Player) {
|
||||
cd.m.Lock()
|
||||
defer cd.m.Unlock()
|
||||
cd.player = player
|
||||
}
|
||||
func (cd *ClientData) GetPlayer() *Player {
|
||||
cd.m.Lock()
|
||||
defer cd.m.Unlock()
|
||||
return cd.player
|
||||
}
|
||||
func (cd *ClientData) Getwsmsg() *WsCodec {
|
||||
cd.m.Lock()
|
||||
defer cd.m.Unlock()
|
||||
return &cd.wsmsg
|
||||
}
|
||||
func (cd *ClientData) SetCrossDomain(isCrossDomain bool) {
|
||||
cd.m.Lock()
|
||||
defer cd.m.Unlock()
|
||||
cd.isCrossDomain = isCrossDomain
|
||||
}
|
||||
func (cd *ClientData) GetIsCrossDomain() bool {
|
||||
cd.m.Lock()
|
||||
defer cd.m.Unlock()
|
||||
return cd.isCrossDomain
|
||||
Wsmsg *WsCodec
|
||||
}
|
||||
|
||||
func NewClientData() *ClientData {
|
||||
cd := ClientData{
|
||||
isCrossDomain: false,
|
||||
player: nil,
|
||||
m: sync.Mutex{},
|
||||
wsmsg: WsCodec{},
|
||||
IsCrossDomain: false,
|
||||
Player: nil,
|
||||
|
||||
Wsmsg: &WsCodec{},
|
||||
}
|
||||
return &cd
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ var Mainplayer = &utils.SyncMap[uint32, *Player]{} //玩家数据
|
||||
|
||||
func (c *Conn) SendPack(bytes []byte) error {
|
||||
if t, ok := c.MainConn.Context().(*ClientData); ok {
|
||||
if t.Getwsmsg().Upgraded {
|
||||
if t.Wsmsg.Upgraded {
|
||||
// This is the echo server
|
||||
err := wsutil.WriteServerMessage(c.MainConn, ws.OpBinary, bytes)
|
||||
if err != nil {
|
||||
@@ -41,7 +41,7 @@ func (c *Conn) SendPack(bytes []byte) error {
|
||||
}
|
||||
|
||||
type Player struct {
|
||||
MainConn Conn
|
||||
MainConn *Conn
|
||||
|
||||
IsLogin bool //是否登录
|
||||
mu sync.Mutex
|
||||
@@ -58,7 +58,7 @@ type Player struct {
|
||||
// PlayerOption 定义配置 Player 的函数类型
|
||||
type PlayerOption func(*Player)
|
||||
|
||||
func WithConn(c Conn) PlayerOption {
|
||||
func WithConn(c *Conn) PlayerOption {
|
||||
return func(p *Player) {
|
||||
p.MainConn = c
|
||||
}
|
||||
@@ -76,7 +76,7 @@ func NewPlayer(opts ...PlayerOption) *Player {
|
||||
}
|
||||
|
||||
func (p *Player) SendPack(b []byte) error {
|
||||
fmt.Println("发送数据包", len(b))
|
||||
// fmt.Println("发送数据包", len(b))
|
||||
err := p.MainConn.SendPack(b)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"io"
|
||||
"sync"
|
||||
|
||||
"github.com/gobwas/ws"
|
||||
"github.com/gobwas/ws/wsutil"
|
||||
@@ -13,6 +14,7 @@ import (
|
||||
|
||||
type Conn struct {
|
||||
MainConn gnet.Conn `struc:"[0]pad"` //TODO 不序列化,,序列化下面的作为blob存数据库
|
||||
Mu sync.Mutex
|
||||
}
|
||||
|
||||
func NewConn(c gnet.Conn) *Conn {
|
||||
|
||||
Reference in New Issue
Block a user