1 Star 0 Fork 0

jmesyan/impetus

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
ws_client.go 2.70 KB
一键复制 编辑 原始数据 按行查看 历史
jmesyan 提交于 2020-12-12 22:03 . init
package network
import (
"github.com/gorilla/websocket"
"gitee.com/jmesyan/impetus/log"
"sync"
"time"
)
type WSClient struct {
sync.Mutex
Addr string
ConnNum int
ConnectInterval time.Duration
PendingWriteNum int
MaxMsgLen uint32
HandshakeTimeout time.Duration
AutoReconnect bool
NewAgent func(*WSConn) Agent
dialer websocket.Dialer
conns WebsocketConnSet
wg sync.WaitGroup
closeFlag bool
}
func (client *WSClient) Start() {
client.init()
for i := 0; i < client.ConnNum; i++ {
client.wg.Add(1)
go client.connect()
}
}
func (client *WSClient) init() {
client.Lock()
defer client.Unlock()
if client.ConnNum <= 0 {
client.ConnNum = 1
log.Warning("invalid ConnNum, reset to %v", client.ConnNum)
}
if client.ConnectInterval <= 0 {
client.ConnectInterval = 3 * time.Second
log.Warning("invalid ConnectInterval, reset to %v", client.ConnectInterval)
}
if client.PendingWriteNum <= 0 {
client.PendingWriteNum = 100
log.Warning("invalid PendingWriteNum, reset to %v", client.PendingWriteNum)
}
if client.MaxMsgLen <= 0 {
client.MaxMsgLen = 4096
log.Warning("invalid MaxMsgLen, reset to %v", client.MaxMsgLen)
}
if client.HandshakeTimeout <= 0 {
client.HandshakeTimeout = 10 * time.Second
log.Warning("invalid HandshakeTimeout, reset to %v", client.HandshakeTimeout)
}
if client.NewAgent == nil {
log.Error("NewAgent must not be nil")
}
if client.conns != nil {
log.Error("client is running")
}
client.conns = make(WebsocketConnSet)
client.closeFlag = false
client.dialer = websocket.Dialer{
HandshakeTimeout: client.HandshakeTimeout,
}
}
func (client *WSClient) dial() *websocket.Conn {
for {
conn, _, err := client.dialer.Dial(client.Addr, nil)
if err == nil || client.closeFlag {
return conn
}
log.Warning("connect to %v error: %v", client.Addr, err)
time.Sleep(client.ConnectInterval)
continue
}
}
func (client *WSClient) connect() {
defer client.wg.Done()
reconnect:
conn := client.dial()
if conn == nil {
return
}
conn.SetReadLimit(int64(client.MaxMsgLen))
client.Lock()
if client.closeFlag {
client.Unlock()
conn.Close()
return
}
client.conns[conn] = struct{}{}
client.Unlock()
wsConn := newWSConn(conn, client.PendingWriteNum, client.MaxMsgLen)
agent := client.NewAgent(wsConn)
agent.Run()
// cleanup
wsConn.Close()
client.Lock()
delete(client.conns, conn)
client.Unlock()
agent.OnClose()
if client.AutoReconnect {
time.Sleep(client.ConnectInterval)
goto reconnect
}
}
func (client *WSClient) Close() {
client.Lock()
client.closeFlag = true
for conn := range client.conns {
conn.Close()
}
client.conns = nil
client.Unlock()
client.wg.Wait()
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/jmesyan/impetus.git
git@gitee.com:jmesyan/impetus.git
jmesyan
impetus
impetus
v1.0.2

搜索帮助