feat 优化网络库
This commit is contained in:
@@ -21,8 +21,8 @@ public class SocketManager : MonoBehaviour
|
||||
|
||||
public async void Connect()
|
||||
{
|
||||
_ws = new WebSocket($"wss://www.hlsq.asia/ws/?token={Random.Range(1, 1000)}");
|
||||
// _ws = new WebSocket($"ws://127.0.0.1:8501/?token={Random.Range(1, 1000)}");
|
||||
// _ws = new WebSocket($"wss://www.hlsq.asia/ws/?token={Random.Range(1, 1000)}");
|
||||
_ws = new WebSocket($"ws://127.0.0.1:8501/?token={Random.Range(1, 1000)}");
|
||||
|
||||
_ws.OnOpen += () =>
|
||||
{
|
||||
|
||||
@@ -9,7 +9,7 @@ require (
|
||||
github.com/golang-jwt/jwt/v5 v5.3.0
|
||||
github.com/grpc-ecosystem/grpc-gateway/v2 v2.26.3
|
||||
github.com/natefinch/lumberjack v2.0.0+incompatible
|
||||
github.com/panjf2000/gnet/v2 v2.9.1
|
||||
github.com/panjf2000/gnet/v2 v2.9.7
|
||||
github.com/redis/go-redis/v9 v9.10.0
|
||||
github.com/spf13/viper v1.21.0
|
||||
go.etcd.io/etcd/api/v3 v3.6.1
|
||||
|
||||
@@ -109,6 +109,8 @@ github.com/panjf2000/ants/v2 v2.11.3 h1:AfI0ngBoXJmYOpDh9m516vjqoUu2sLrIVgppI9TZ
|
||||
github.com/panjf2000/ants/v2 v2.11.3/go.mod h1:8u92CYMUc6gyvTIw8Ru7Mt7+/ESnJahz5EVtqfrilek=
|
||||
github.com/panjf2000/gnet/v2 v2.9.1 h1:bKewICy/0xnQ9PMzNaswpe/Ah14w1TrRk91LHTcbIlA=
|
||||
github.com/panjf2000/gnet/v2 v2.9.1/go.mod h1:WQTxDWYuQ/hz3eccH0FN32IVuvZ19HewEWx0l62fx7E=
|
||||
github.com/panjf2000/gnet/v2 v2.9.7 h1:6zW7Jl3oAfXwSuh1PxHLndoL2MQRWx0AJR6aaQjxUgA=
|
||||
github.com/panjf2000/gnet/v2 v2.9.7/go.mod h1:WQTxDWYuQ/hz3eccH0FN32IVuvZ19HewEWx0l62fx7E=
|
||||
github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4=
|
||||
github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
|
||||
@@ -28,9 +28,9 @@ const (
|
||||
|
||||
// ISocketServer 由应用层实现
|
||||
type ISocketServer interface {
|
||||
OnOpen(ISocketConn) ([]byte, Action) // 开启连接
|
||||
OnHandShake(ISocketConn) // 开始握手
|
||||
OnMessage(ISocketConn, []byte) Action // 收到消息
|
||||
OnOpen(ISocketConn) ([]byte, Action) // 开启连接
|
||||
OnHandShake(ISocketConn, []byte, func(ISocketConn, []byte)) Action // 开始握手
|
||||
OnMessage(ISocketConn, []byte) Action // 收到消息
|
||||
OnPong(ISocketConn)
|
||||
OnClose(ISocketConn, error) Action // 关闭连接
|
||||
OnTick() (time.Duration, Action)
|
||||
|
||||
@@ -12,7 +12,6 @@ import (
|
||||
|
||||
// WSServer 实现GNet库接口
|
||||
type WSServer struct {
|
||||
gnet.BuiltinEventEngine
|
||||
eng gnet.Engine
|
||||
i socket.ISocketServer
|
||||
logger logging.Logger // 日志
|
||||
@@ -20,29 +19,31 @@ type WSServer struct {
|
||||
unUpgradeConn sync.Map
|
||||
}
|
||||
|
||||
func NewWSServer(i socket.ISocketServer, logger logging.Logger, timeout time.Duration) *WSServer {
|
||||
func NewWSServer(i socket.ISocketServer, logger logging.Logger, upgradeTimeout time.Duration) *WSServer {
|
||||
if i == nil {
|
||||
return nil
|
||||
}
|
||||
return &WSServer{
|
||||
i: i,
|
||||
logger: logger,
|
||||
upgradeTimeout: timeout,
|
||||
upgradeTimeout: upgradeTimeout,
|
||||
unUpgradeConn: sync.Map{},
|
||||
}
|
||||
}
|
||||
|
||||
func (s *WSServer) Run(logger logging.Logger, addr string, multiCore, reusePort, tick, lockOSThread, reuseAddr bool, processNum int) error {
|
||||
func (s *WSServer) Run(addr string, multicore bool, numEventLoop int, tcpNoDelay gnet.TCPSocketOpt, readBufferCap, writeBufferCap int, lockOSThread bool, reusePort, ticker bool, logger logging.Logger) error {
|
||||
return gnet.Run(
|
||||
s,
|
||||
addr,
|
||||
gnet.WithMulticore(multiCore),
|
||||
gnet.WithNumEventLoop(processNum),
|
||||
gnet.WithReusePort(reusePort),
|
||||
gnet.WithTicker(tick),
|
||||
gnet.WithLogger(logger),
|
||||
gnet.WithMulticore(multicore),
|
||||
gnet.WithNumEventLoop(numEventLoop),
|
||||
gnet.WithTCPNoDelay(tcpNoDelay),
|
||||
gnet.WithReadBufferCap(readBufferCap),
|
||||
gnet.WithWriteBufferCap(writeBufferCap),
|
||||
gnet.WithLockOSThread(lockOSThread),
|
||||
gnet.WithReuseAddr(reuseAddr),
|
||||
gnet.WithReusePort(reusePort),
|
||||
gnet.WithTicker(ticker),
|
||||
gnet.WithLogger(logger),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -55,6 +56,9 @@ func (s *WSServer) OnBoot(eng gnet.Engine) gnet.Action {
|
||||
return gnet.None
|
||||
}
|
||||
|
||||
func (s *WSServer) OnShutdown(_ gnet.Engine) {
|
||||
}
|
||||
|
||||
func (s *WSServer) OnOpen(c gnet.Conn) ([]byte, gnet.Action) {
|
||||
ws := &WSConn{
|
||||
Conn: c,
|
||||
@@ -89,67 +93,43 @@ func (s *WSServer) OnClose(c gnet.Conn, err error) (action gnet.Action) {
|
||||
}
|
||||
|
||||
// OnTraffic fires when a local socket receives data from the peer.
|
||||
func (s *WSServer) OnTraffic(c gnet.Conn) (action gnet.Action) {
|
||||
tmp := c.Context()
|
||||
if tmp == nil {
|
||||
s.logger.Errorf("OnTraffic context nil: %v", c)
|
||||
action = gnet.Close
|
||||
return
|
||||
}
|
||||
|
||||
ws, ok := tmp.(*WSConn)
|
||||
if !ok {
|
||||
ws.logger.Errorf("OnTraffic convert ws error: %v", tmp)
|
||||
action = gnet.Close
|
||||
return
|
||||
}
|
||||
|
||||
action = ws.readBytesBuf(c)
|
||||
if action != gnet.None {
|
||||
return
|
||||
func (s *WSServer) OnTraffic(c gnet.Conn) gnet.Action {
|
||||
ws := c.Context().(*WSConn)
|
||||
if action := ws.readBytesBuf(c); action != gnet.None {
|
||||
return action
|
||||
}
|
||||
|
||||
if !ws.isUpgrade {
|
||||
var data []byte
|
||||
data, ok, action = ws.upgrade()
|
||||
if ok {
|
||||
data, action := ws.upgrade()
|
||||
if len(data) > 0 {
|
||||
s.unUpgradeConn.Delete(c.RemoteAddr().String())
|
||||
s.i.OnHandShake(ws)
|
||||
if data != nil {
|
||||
err := ws.Conn.AsyncWrite(data, nil)
|
||||
if err != nil {
|
||||
ws.logger.Errorf("update ws write upgrade protocol error", err)
|
||||
action = gnet.Close
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
msg, err := ws.readWsMessages()
|
||||
if err != nil {
|
||||
ws.logger.Errorf("read ws messages errors", err)
|
||||
return gnet.Close
|
||||
action = gnet.Action(s.i.OnHandShake(ws, data, s.OnHandShakeFinish))
|
||||
}
|
||||
return action
|
||||
}
|
||||
|
||||
if msg != nil {
|
||||
for _, m := range msg {
|
||||
if socket.OpCode(m.OpCode) == socket.OpPong {
|
||||
s.i.OnPong(ws)
|
||||
continue
|
||||
}
|
||||
if socket.OpCode(m.OpCode) == socket.OpClose {
|
||||
return gnet.Close
|
||||
}
|
||||
if socket.OpCode(m.OpCode) == socket.OpPing {
|
||||
continue
|
||||
}
|
||||
a := s.i.OnMessage(ws, m.Payload)
|
||||
if gnet.Action(a) != gnet.None {
|
||||
action = gnet.Action(a)
|
||||
}
|
||||
if msg, err := ws.readWsMessages(); err != nil {
|
||||
ws.logger.Errorf("read ws messages err: %v", err)
|
||||
return gnet.Close
|
||||
} else if msg != nil {
|
||||
for _, m := range msg {
|
||||
if socket.OpCode(m.OpCode) == socket.OpPong {
|
||||
s.i.OnPong(ws)
|
||||
continue
|
||||
}
|
||||
if socket.OpCode(m.OpCode) == socket.OpClose {
|
||||
return gnet.Close
|
||||
}
|
||||
if socket.OpCode(m.OpCode) == socket.OpPing {
|
||||
continue
|
||||
}
|
||||
a := s.i.OnMessage(ws, m.Payload)
|
||||
if gnet.Action(a) != gnet.None {
|
||||
return gnet.Action(a)
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
return gnet.None
|
||||
}
|
||||
|
||||
// OnTick fires immediately after the engine starts and will fire again
|
||||
@@ -194,3 +174,14 @@ func (s *WSServer) OnTick() (delay time.Duration, action gnet.Action) {
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// OnHandShakeFinish 握手完成
|
||||
func (s *WSServer) OnHandShakeFinish(conn socket.ISocketConn, hsResp []byte) {
|
||||
ws := conn.(*WSConn)
|
||||
if err := ws.Conn.AsyncWrite(hsResp, nil); err != nil {
|
||||
ws.logger.Errorf("OnHandShakeFinish err: %v", err)
|
||||
if err = ws.Close(); err != nil {
|
||||
ws.logger.Errorf("OnHandShakeFinish Close error: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,13 +15,14 @@ import (
|
||||
// WSConn 实现ISocketConn接口
|
||||
type WSConn struct {
|
||||
gnet.Conn
|
||||
buf bytes.Buffer
|
||||
logger logging.Logger
|
||||
isUpgrade bool
|
||||
isClose bool
|
||||
param map[string]interface{}
|
||||
openTime int64
|
||||
remoteAddr string
|
||||
buf bytes.Buffer
|
||||
logger logging.Logger
|
||||
isUpgrade bool // 是否已经升级
|
||||
upgradeResp []byte // 升级响应
|
||||
isClose bool
|
||||
param map[string]interface{}
|
||||
openTime int64 // 开启连接的时间
|
||||
remoteAddr string // 远程ID地址
|
||||
wsMessageBuf
|
||||
}
|
||||
|
||||
@@ -60,11 +61,7 @@ func (w *WSConn) readBytesBuf(c gnet.Conn) gnet.Action {
|
||||
return gnet.None
|
||||
}
|
||||
|
||||
func (w *WSConn) upgrade() (data []byte, ok bool, action gnet.Action) {
|
||||
if w.isUpgrade {
|
||||
ok = true
|
||||
return
|
||||
}
|
||||
func (w *WSConn) upgrade() (data []byte, action gnet.Action) {
|
||||
buf := &w.buf
|
||||
tmpReader := bytes.NewReader(buf.Bytes())
|
||||
oldLen := tmpReader.Len()
|
||||
@@ -83,27 +80,24 @@ func (w *WSConn) upgrade() (data []byte, ok bool, action gnet.Action) {
|
||||
}
|
||||
buf.Next(skipN)
|
||||
if w.logger != nil {
|
||||
w.logger.Errorf("ws upgrade error", err.Error())
|
||||
w.logger.Errorf("ws upgrade err: %v", err.Error())
|
||||
}
|
||||
|
||||
action = gnet.Close
|
||||
return
|
||||
}
|
||||
buf.Next(skipN)
|
||||
if w.logger != nil {
|
||||
w.logger.Infof("ws upgrade success conn upgrade websocket protocol!")
|
||||
}
|
||||
//if w.logger != nil {
|
||||
// w.logger.Infof("ws upgrade success conn upgrade websocket protocol!")
|
||||
//}
|
||||
_ = tempWriter.Flush()
|
||||
data = result.Bytes()
|
||||
ok = true
|
||||
w.isUpgrade = true
|
||||
return
|
||||
}
|
||||
|
||||
func (w *WSConn) readWsMessages() (messages []wsutil.Message, err error) {
|
||||
in := &w.buf
|
||||
//messages, err = wsutil.ReadClientMessage(in, messages)
|
||||
//return
|
||||
for {
|
||||
if w.curHeader == nil {
|
||||
if in.Len() < ws.MinHeaderSize { //头长度至少是2
|
||||
@@ -126,13 +120,8 @@ func (w *WSConn) readWsMessages() (messages []wsutil.Message, err error) {
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
//in.Next(skipN)
|
||||
|
||||
w.curHeader = &head
|
||||
//err = ws.WriteHeader(&msgBuf.cachedBuf, head)
|
||||
//if err != nil {
|
||||
// return nil, err
|
||||
//}
|
||||
}
|
||||
dataLen := (int)(w.curHeader.Length)
|
||||
if dataLen > 0 {
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
"gateway/config"
|
||||
"gateway/internal/net/ws_gateway"
|
||||
"github.com/panjf2000/gnet/v2"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
@@ -31,14 +32,16 @@ func (m *ModuleWebsocketServer) start() error {
|
||||
go func() {
|
||||
defer m.wg.Done()
|
||||
_ = m.server.Run(
|
||||
log.GetLogger().Named("GNET"),
|
||||
fmt.Sprintf("tcp4://0.0.0.0:%v", config.Get().Serve.Socket.Web.Port),
|
||||
true,
|
||||
0,
|
||||
gnet.TCPNoDelay,
|
||||
64*1024,
|
||||
64*1024,
|
||||
true,
|
||||
false,
|
||||
false,
|
||||
true,
|
||||
8,
|
||||
log.GetLogger().Named("GNET"),
|
||||
)
|
||||
}()
|
||||
return nil
|
||||
|
||||
@@ -53,7 +53,7 @@ require (
|
||||
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
|
||||
github.com/natefinch/lumberjack v2.0.0+incompatible // indirect
|
||||
github.com/panjf2000/ants/v2 v2.11.3 // indirect
|
||||
github.com/panjf2000/gnet/v2 v2.9.1 // indirect
|
||||
github.com/panjf2000/gnet/v2 v2.9.7 // indirect
|
||||
github.com/pelletier/go-toml/v2 v2.2.4 // indirect
|
||||
github.com/prometheus/client_model v0.6.1 // indirect
|
||||
github.com/prometheus/common v0.62.0 // indirect
|
||||
|
||||
@@ -116,6 +116,8 @@ github.com/panjf2000/ants/v2 v2.11.3 h1:AfI0ngBoXJmYOpDh9m516vjqoUu2sLrIVgppI9TZ
|
||||
github.com/panjf2000/ants/v2 v2.11.3/go.mod h1:8u92CYMUc6gyvTIw8Ru7Mt7+/ESnJahz5EVtqfrilek=
|
||||
github.com/panjf2000/gnet/v2 v2.9.1 h1:bKewICy/0xnQ9PMzNaswpe/Ah14w1TrRk91LHTcbIlA=
|
||||
github.com/panjf2000/gnet/v2 v2.9.1/go.mod h1:WQTxDWYuQ/hz3eccH0FN32IVuvZ19HewEWx0l62fx7E=
|
||||
github.com/panjf2000/gnet/v2 v2.9.7 h1:6zW7Jl3oAfXwSuh1PxHLndoL2MQRWx0AJR6aaQjxUgA=
|
||||
github.com/panjf2000/gnet/v2 v2.9.7/go.mod h1:WQTxDWYuQ/hz3eccH0FN32IVuvZ19HewEWx0l62fx7E=
|
||||
github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4=
|
||||
github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
|
||||
@@ -90,6 +90,7 @@ func (c *Client) CloseClient() {
|
||||
}
|
||||
}
|
||||
|
||||
// onClose 客户端关闭自动触发
|
||||
func (c *Client) onClose() {
|
||||
if c.conn != nil {
|
||||
_ = c.conn.Close()
|
||||
|
||||
@@ -3,8 +3,9 @@ package ws_gateway
|
||||
import (
|
||||
"common/log"
|
||||
"common/net/socket"
|
||||
"common/utils"
|
||||
"fmt"
|
||||
ws_handler2 "gateway/internal/handler/ws_handler"
|
||||
"gateway/internal/handler/ws_handler"
|
||||
"go.uber.org/zap"
|
||||
"strconv"
|
||||
"time"
|
||||
@@ -19,40 +20,49 @@ func (g *GatewayWsServer) OnOpen(conn socket.ISocketConn) ([]byte, socket.Action
|
||||
return nil, socket.None
|
||||
}
|
||||
|
||||
func (g *GatewayWsServer) OnHandShake(conn socket.ISocketConn) {
|
||||
func (g *GatewayWsServer) OnHandShake(conn socket.ISocketConn, bytes []byte, callback func(conn socket.ISocketConn, bytes []byte)) socket.Action {
|
||||
token, ok := conn.GetParam("token").(string)
|
||||
if !ok || token == "" {
|
||||
if !ok {
|
||||
g.logger.Warnf("token is not string")
|
||||
_ = conn.Close()
|
||||
return
|
||||
return socket.Close
|
||||
}
|
||||
t, err := strconv.Atoi(token)
|
||||
if err != nil {
|
||||
_ = conn.Close()
|
||||
//claims, err := utils.ParseToken(token, config.Get().Auth.Secret)
|
||||
//if err != nil {
|
||||
// g.logger.Warnf("token is invalid")
|
||||
// return socket.Close
|
||||
//}
|
||||
|
||||
t, _ := strconv.Atoi(token)
|
||||
claims := utils.Claims{
|
||||
USN: int64(t),
|
||||
}
|
||||
if oldClient := ws_handler2.UserMgr.GetByUSN(int64(t)); oldClient != nil {
|
||||
oldClient.CloseClient()
|
||||
}
|
||||
client := ws_handler2.NewClient(int64(t), conn)
|
||||
ws_handler2.UserMgr.Add(int64(t), client)
|
||||
conn.SetParam("client", client)
|
||||
go func(shResp []byte) {
|
||||
if oldClient := ws_handler.UserMgr.GetByUSN(claims.USN); oldClient != nil {
|
||||
oldClient.CloseClient()
|
||||
}
|
||||
client := ws_handler.NewClient(claims.USN, conn)
|
||||
ws_handler.UserMgr.Add(claims.USN, client)
|
||||
conn.SetParam("client", client)
|
||||
callback(conn, shResp)
|
||||
}(bytes)
|
||||
return socket.None
|
||||
}
|
||||
|
||||
func (g *GatewayWsServer) OnMessage(conn socket.ISocketConn, bytes []byte) socket.Action {
|
||||
client, ok := conn.GetParam("client").(*ws_handler2.Client)
|
||||
client, ok := conn.GetParam("client").(*ws_handler.Client)
|
||||
if !ok || client.USN == 0 {
|
||||
return socket.Close
|
||||
}
|
||||
client.OnEvent(&ws_handler2.ClientEvent{Msg: bytes})
|
||||
client.OnEvent(&ws_handler.ClientEvent{Msg: bytes})
|
||||
return socket.None
|
||||
}
|
||||
|
||||
func (g *GatewayWsServer) OnPong(conn socket.ISocketConn) {
|
||||
client, ok := conn.GetParam("client").(*ws_handler2.Client)
|
||||
client, ok := conn.GetParam("client").(*ws_handler.Client)
|
||||
if !ok || client.USN == 0 {
|
||||
return
|
||||
}
|
||||
client.OnEvent(&ws_handler2.PongEvent{})
|
||||
client.OnEvent(&ws_handler.PongEvent{})
|
||||
}
|
||||
|
||||
func (g *GatewayWsServer) OnClose(_ socket.ISocketConn, _ error) socket.Action {
|
||||
|
||||
@@ -20,6 +20,7 @@ type Module interface {
|
||||
func (p *Program) Init(_ svc.Environment) error {
|
||||
p.moduleList = append(p.moduleList, &ModuleBase{})
|
||||
p.moduleList = append(p.moduleList, &ModuleWebsocket{})
|
||||
p.moduleList = append(p.moduleList, &ModulePprof{})
|
||||
|
||||
for _, module := range p.moduleList {
|
||||
if err := module.init(); err != nil {
|
||||
|
||||
44
Server/robot/app/pprof.go
Normal file
44
Server/robot/app/pprof.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"common/log"
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
_ "net/http/pprof"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// ModulePprof pprof模块
|
||||
type ModulePprof struct {
|
||||
wg *sync.WaitGroup
|
||||
server *http.Server
|
||||
}
|
||||
|
||||
func (m *ModulePprof) init() error {
|
||||
m.wg = &sync.WaitGroup{}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *ModulePprof) start() error {
|
||||
m.wg.Add(1)
|
||||
go func() {
|
||||
defer m.wg.Done()
|
||||
m.server = &http.Server{
|
||||
Addr: "localhost:6060",
|
||||
}
|
||||
if err := m.server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
||||
log.Errorf("pprof server failed: %v", err.Error())
|
||||
}
|
||||
log.Infof("pprof server stop.")
|
||||
}()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *ModulePprof) stop() error {
|
||||
if err := m.server.Shutdown(context.Background()); err != nil {
|
||||
log.Errorf("stop pprof server failed: %v", err)
|
||||
}
|
||||
m.wg.Wait()
|
||||
return nil
|
||||
}
|
||||
@@ -9,9 +9,9 @@ log:
|
||||
maxAge: 7
|
||||
|
||||
client:
|
||||
count: 100
|
||||
count: 1100
|
||||
usn: [ 1,1000000 ]
|
||||
websocket:
|
||||
address: "ws://47.108.184.184/ws/"
|
||||
# address: "ws://127.0.0.1"
|
||||
port: 0
|
||||
# address: "ws://47.108.184.184/ws/"
|
||||
address: "ws://127.0.0.1"
|
||||
port: 8501
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"google.golang.org/protobuf/proto"
|
||||
"math"
|
||||
"math/rand"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -49,14 +50,21 @@ func (_ *EnterInstance) Handle(data []byte, client *Client) {
|
||||
}()
|
||||
}
|
||||
|
||||
var s2cPositionPool = sync.Pool{
|
||||
New: func() interface{} {
|
||||
return &sc_pb.Message{}
|
||||
},
|
||||
}
|
||||
|
||||
type Position struct {
|
||||
}
|
||||
|
||||
func (_ *Position) Handle(data []byte, client *Client) {
|
||||
msg := &sc_pb.S2C_Position{}
|
||||
if err := proto.Unmarshal(data, msg); err != nil {
|
||||
log.Errorf("handle msg error")
|
||||
client.Stop()
|
||||
return
|
||||
}
|
||||
//msg := s2cPositionPool.Get().(*sc_pb.Message)
|
||||
//if err := proto.Unmarshal(data, msg); err != nil {
|
||||
// log.Errorf("handle msg error")
|
||||
// client.Stop()
|
||||
// return
|
||||
//}
|
||||
//s2cPositionPool.Put(msg)
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ require (
|
||||
github.com/jinzhu/inflection v1.0.0 // indirect
|
||||
github.com/jinzhu/now v1.1.5 // indirect
|
||||
github.com/natefinch/lumberjack v2.0.0+incompatible // indirect
|
||||
github.com/panjf2000/gnet/v2 v2.9.1 // indirect
|
||||
github.com/panjf2000/gnet/v2 v2.9.7 // indirect
|
||||
github.com/pelletier/go-toml/v2 v2.2.4 // indirect
|
||||
github.com/redis/go-redis/v9 v9.10.0 // indirect
|
||||
github.com/sagikazarmark/locafero v0.11.0 // indirect
|
||||
|
||||
@@ -59,6 +59,8 @@ github.com/natefinch/lumberjack v2.0.0+incompatible h1:4QJd3OLAMgj7ph+yZTuX13Ld4
|
||||
github.com/natefinch/lumberjack v2.0.0+incompatible/go.mod h1:Wi9p2TTF5DG5oU+6YfsmYQpsTIOm0B1VNzQg9Mw6nPk=
|
||||
github.com/panjf2000/gnet/v2 v2.9.1 h1:bKewICy/0xnQ9PMzNaswpe/Ah14w1TrRk91LHTcbIlA=
|
||||
github.com/panjf2000/gnet/v2 v2.9.1/go.mod h1:WQTxDWYuQ/hz3eccH0FN32IVuvZ19HewEWx0l62fx7E=
|
||||
github.com/panjf2000/gnet/v2 v2.9.7 h1:6zW7Jl3oAfXwSuh1PxHLndoL2MQRWx0AJR6aaQjxUgA=
|
||||
github.com/panjf2000/gnet/v2 v2.9.7/go.mod h1:WQTxDWYuQ/hz3eccH0FN32IVuvZ19HewEWx0l62fx7E=
|
||||
github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4=
|
||||
github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
|
||||
@@ -47,7 +47,7 @@ require (
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
|
||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||
github.com/natefinch/lumberjack v2.0.0+incompatible // indirect
|
||||
github.com/panjf2000/gnet/v2 v2.9.1 // indirect
|
||||
github.com/panjf2000/gnet/v2 v2.9.7 // indirect
|
||||
github.com/pelletier/go-toml/v2 v2.2.4 // indirect
|
||||
github.com/quic-go/qpack v0.5.1 // indirect
|
||||
github.com/quic-go/quic-go v0.54.0 // indirect
|
||||
|
||||
@@ -113,6 +113,7 @@ github.com/natefinch/lumberjack v2.0.0+incompatible h1:4QJd3OLAMgj7ph+yZTuX13Ld4
|
||||
github.com/natefinch/lumberjack v2.0.0+incompatible/go.mod h1:Wi9p2TTF5DG5oU+6YfsmYQpsTIOm0B1VNzQg9Mw6nPk=
|
||||
github.com/panjf2000/gnet/v2 v2.9.1 h1:bKewICy/0xnQ9PMzNaswpe/Ah14w1TrRk91LHTcbIlA=
|
||||
github.com/panjf2000/gnet/v2 v2.9.1/go.mod h1:WQTxDWYuQ/hz3eccH0FN32IVuvZ19HewEWx0l62fx7E=
|
||||
github.com/panjf2000/gnet/v2 v2.9.7/go.mod h1:WQTxDWYuQ/hz3eccH0FN32IVuvZ19HewEWx0l62fx7E=
|
||||
github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4=
|
||||
github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
|
||||
Reference in New Issue
Block a user