feat 初始化
This commit is contained in:
@@ -3,6 +3,8 @@ package websocket
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"git.hlsq.asia/alex/service-common/log"
|
||||
"git.hlsq.asia/alex/service-common/net/socket"
|
||||
"github.com/panjf2000/gnet/v2"
|
||||
"github.com/panjf2000/gnet/v2/pkg/logging"
|
||||
@@ -31,7 +33,7 @@ func NewWSServer(i socket.ISocketServer, logger logging.Logger, upgradeTimeout t
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
func (s *WSServer) Run(addr string, multicore bool, numEventLoop int, tcpNoDelay gnet.TCPSocketOpt, readBufferCap, writeBufferCap int, lockOSThread bool, reusePort, ticker bool) error {
|
||||
return gnet.Run(
|
||||
s,
|
||||
addr,
|
||||
@@ -43,7 +45,7 @@ func (s *WSServer) Run(addr string, multicore bool, numEventLoop int, tcpNoDelay
|
||||
gnet.WithLockOSThread(lockOSThread),
|
||||
gnet.WithReusePort(reusePort),
|
||||
gnet.WithTicker(ticker),
|
||||
gnet.WithLogger(logger),
|
||||
gnet.WithLogger(s.logger),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -65,7 +67,7 @@ func (s *WSServer) OnOpen(c gnet.Conn) ([]byte, gnet.Action) {
|
||||
isUpgrade: false,
|
||||
openTime: time.Now().Unix(),
|
||||
buf: bytes.Buffer{},
|
||||
logger: s.logger,
|
||||
logger: log.GetLogger().Named(fmt.Sprintf("CONN %v", c.RemoteAddr().String())),
|
||||
wsMessageBuf: wsMessageBuf{
|
||||
curHeader: nil,
|
||||
cachedBuf: bytes.Buffer{},
|
||||
@@ -86,7 +88,7 @@ func (s *WSServer) OnClose(c gnet.Conn, err error) (action gnet.Action) {
|
||||
ws, ok := c.Context().(*WSConn)
|
||||
if ok {
|
||||
ws.isClose = true
|
||||
ws.logger.Warnf("connection close, err: %v", err)
|
||||
ws.logger.Infof("Connection close, err: %v", err)
|
||||
return gnet.Action(s.i.OnClose(ws, err))
|
||||
}
|
||||
return
|
||||
@@ -121,14 +123,10 @@ func (s *WSServer) OnTraffic(c gnet.Conn) gnet.Action {
|
||||
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 {
|
||||
if socket.OpCode(m.OpCode) == socket.OpPing || socket.OpCode(m.OpCode) == socket.OpPong {
|
||||
continue
|
||||
}
|
||||
a := s.i.OnMessage(ws, m.Payload)
|
||||
@@ -176,9 +174,8 @@ func (s *WSServer) OnTick() (delay time.Duration, action gnet.Action) {
|
||||
v.logger.Errorf("upgrade ws time out close socket error: %v", err)
|
||||
}
|
||||
}
|
||||
d, a := s.i.OnTick()
|
||||
delay = d
|
||||
action = gnet.Action(a)
|
||||
delay = 5 * time.Second
|
||||
action = gnet.Action(s.i.OnTick())
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user