feat 初始化
This commit is contained in:
@@ -1,9 +1,5 @@
|
||||
package socket
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type Action int
|
||||
|
||||
const (
|
||||
@@ -31,9 +27,8 @@ type ISocketServer interface {
|
||||
OnOpen(ISocketConn) ([]byte, Action) // 开启连接
|
||||
OnHandShake(ISocketConn) Action // 开始握手
|
||||
OnMessage(ISocketConn, []byte) Action // 收到消息
|
||||
OnPong(ISocketConn)
|
||||
OnClose(ISocketConn, error) Action // 关闭连接
|
||||
OnTick() (time.Duration, Action)
|
||||
OnClose(ISocketConn, error) Action // 关闭连接
|
||||
OnTick() Action // 定时调用(由网络层决定)
|
||||
}
|
||||
|
||||
// ISocketConn 由网络层实现
|
||||
@@ -41,7 +36,7 @@ type ISocketConn interface {
|
||||
GetParam(key string) interface{}
|
||||
SetParam(key string, values interface{})
|
||||
RemoteAddr() string
|
||||
Ping() error // 需要隔一段时间调用一下,建议20s
|
||||
Ping() error
|
||||
Write(data []byte) error
|
||||
Close() error
|
||||
IsClose() bool
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -4,12 +4,13 @@ import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"errors"
|
||||
"io"
|
||||
"net/url"
|
||||
|
||||
"github.com/gobwas/ws"
|
||||
"github.com/gobwas/ws/wsutil"
|
||||
"github.com/panjf2000/gnet/v2"
|
||||
"github.com/panjf2000/gnet/v2/pkg/logging"
|
||||
"io"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
// WSConn 实现ISocketConn接口
|
||||
@@ -22,7 +23,7 @@ type WSConn struct {
|
||||
isClose bool
|
||||
param map[string]interface{}
|
||||
openTime int64 // 开启连接的时间
|
||||
remoteAddr string // 远程ID地址
|
||||
remoteAddr string // 远程IP地址
|
||||
wsMessageBuf
|
||||
}
|
||||
|
||||
@@ -46,7 +47,7 @@ func (w *WSConn) readBytesBuf(c gnet.Conn) gnet.Action {
|
||||
read, err := c.Read(buf)
|
||||
if err != nil {
|
||||
if w.logger != nil {
|
||||
w.logger.Errorf("ws read bytes buf error", err)
|
||||
w.logger.Errorf("ws read bytes buf error: %v", err)
|
||||
}
|
||||
return gnet.Close
|
||||
}
|
||||
@@ -132,7 +133,7 @@ func (w *WSConn) readWsMessages() (messages []wsutil.Message, err error) {
|
||||
}
|
||||
} else { //数据不完整
|
||||
if w.logger != nil {
|
||||
w.logger.Debugf("ws read ws message incomplete data", in.Len(), dataLen)
|
||||
w.logger.Debugf("ws read ws message incomplete data ,%v,%v", in.Len(), dataLen)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user