`` refactor(socket): 将ClientData结构及相关方法从wscodec.go移至SocketHandler_Tomee.go,优化代码组织结构``
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
|||||||
"blazing/common/socket/errorcode"
|
"blazing/common/socket/errorcode"
|
||||||
"blazing/common/utils/bytearray"
|
"blazing/common/utils/bytearray"
|
||||||
"blazing/cool"
|
"blazing/cool"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
@@ -13,6 +14,7 @@ import (
|
|||||||
|
|
||||||
"github.com/gogf/gf/v2/os/glog"
|
"github.com/gogf/gf/v2/os/glog"
|
||||||
"github.com/lunixbochs/struc"
|
"github.com/lunixbochs/struc"
|
||||||
|
"github.com/panjf2000/gnet/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TomeeHeader 结构体字段定义
|
// TomeeHeader 结构体字段定义
|
||||||
@@ -190,3 +192,54 @@ func (h *ClientData) Recv(data TomeeHeader) {
|
|||||||
t.SendPack(data.Pack(ret[0].Interface()))
|
t.SendPack(data.Pack(ret[0].Interface()))
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ClientData struct {
|
||||||
|
IsCrossDomain bool //是否跨域过
|
||||||
|
Player *Player //客户实体
|
||||||
|
Mu sync.Mutex
|
||||||
|
ERROR_CONNUT int
|
||||||
|
Wsmsg *WsCodec
|
||||||
|
Conn gnet.Conn
|
||||||
|
|
||||||
|
CloseChan chan struct{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewClientData(c gnet.Conn) *ClientData {
|
||||||
|
// 创建事件处理器
|
||||||
|
|
||||||
|
cd := ClientData{
|
||||||
|
IsCrossDomain: false,
|
||||||
|
Player: nil,
|
||||||
|
Conn: c,
|
||||||
|
Wsmsg: &WsCodec{},
|
||||||
|
}
|
||||||
|
return &cd
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *ClientData) OnEvent(v []byte) {
|
||||||
|
|
||||||
|
header := TomeeHeader{}
|
||||||
|
|
||||||
|
tempdata := bytearray.CreateByteArray(v)
|
||||||
|
header.Len, _ = tempdata.ReadUInt32()
|
||||||
|
header.Version, _ = tempdata.ReadByte()
|
||||||
|
header.CMD, _ = tempdata.ReadUInt32()
|
||||||
|
//header.CMD = cmd.EnumCommandID(_CMD)
|
||||||
|
header.UserID, _ = tempdata.ReadUInt32()
|
||||||
|
|
||||||
|
header.Result, _ = tempdata.ReadUInt32()
|
||||||
|
header.Data = tempdata.BytesAvailable()
|
||||||
|
if header.CMD > 1000 {
|
||||||
|
if h.Conn.Context().(*ClientData).Player == nil {
|
||||||
|
cool.Loger.Error(context.TODO(), "player is nil")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if h.Conn.Context().(*ClientData).Player.Info == nil {
|
||||||
|
cool.Loger.Error(context.TODO(), "player info is nil")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
h.Recv(header)
|
||||||
|
//fmt.Println("接收封包", header)
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,13 +1,9 @@
|
|||||||
package player
|
package player
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"blazing/common/utils/bytearray"
|
|
||||||
"blazing/cool"
|
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
|
||||||
"errors"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"sync"
|
|
||||||
|
|
||||||
"github.com/gobwas/ws"
|
"github.com/gobwas/ws"
|
||||||
"github.com/gobwas/ws/wsutil"
|
"github.com/gobwas/ws/wsutil"
|
||||||
@@ -193,54 +189,3 @@ func (w *WsCodec) readWsMessages() (messages []wsutil.Message, err error) {
|
|||||||
msgBuf.curHeader = nil
|
msgBuf.curHeader = nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type ClientData struct {
|
|
||||||
IsCrossDomain bool //是否跨域过
|
|
||||||
Player *Player //客户实体
|
|
||||||
Mu sync.Mutex
|
|
||||||
ERROR_CONNUT int
|
|
||||||
Wsmsg *WsCodec
|
|
||||||
Conn gnet.Conn
|
|
||||||
|
|
||||||
CloseChan chan struct{}
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewClientData(c gnet.Conn) *ClientData {
|
|
||||||
// 创建事件处理器
|
|
||||||
|
|
||||||
cd := ClientData{
|
|
||||||
IsCrossDomain: false,
|
|
||||||
Player: nil,
|
|
||||||
Conn: c,
|
|
||||||
Wsmsg: &WsCodec{},
|
|
||||||
}
|
|
||||||
return &cd
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *ClientData) OnEvent(v []byte) {
|
|
||||||
|
|
||||||
header := TomeeHeader{}
|
|
||||||
|
|
||||||
tempdata := bytearray.CreateByteArray(v)
|
|
||||||
header.Len, _ = tempdata.ReadUInt32()
|
|
||||||
header.Version, _ = tempdata.ReadByte()
|
|
||||||
header.CMD, _ = tempdata.ReadUInt32()
|
|
||||||
//header.CMD = cmd.EnumCommandID(_CMD)
|
|
||||||
header.UserID, _ = tempdata.ReadUInt32()
|
|
||||||
|
|
||||||
header.Result, _ = tempdata.ReadUInt32()
|
|
||||||
header.Data = tempdata.BytesAvailable()
|
|
||||||
if header.CMD > 1000 {
|
|
||||||
if h.Conn.Context().(*ClientData).Player == nil {
|
|
||||||
cool.Loger.Error(context.TODO(), "player is nil")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if h.Conn.Context().(*ClientData).Player.Info == nil {
|
|
||||||
cool.Loger.Error(context.TODO(), "player info is nil")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
h.Recv(header)
|
|
||||||
//fmt.Println("接收封包", header)
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user