|
|
|
|
@@ -3,7 +3,6 @@ package client
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
|
|
|
|
"io"
|
|
|
|
|
"log"
|
|
|
|
|
"net/url"
|
|
|
|
|
"strings"
|
|
|
|
|
@@ -17,7 +16,7 @@ import (
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
pingPeriod = 10 * time.Second
|
|
|
|
|
readDeadline = 10 * time.Second
|
|
|
|
|
readDeadline = 120 * time.Second
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type SafeMap[K comparable, V any] struct {
|
|
|
|
|
@@ -156,6 +155,7 @@ func (b *SafeWebsocketClientBuilder) Build(ctx context.Context) (*SafeWebsocketC
|
|
|
|
|
reconnectCh: make(chan struct{}, 1),
|
|
|
|
|
isConnected: false,
|
|
|
|
|
doneMap: NewSafeMap[string, chan struct{}](),
|
|
|
|
|
writeChan: make(chan Message),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if b.authenticateFn != nil {
|
|
|
|
|
@@ -220,22 +220,13 @@ func (wsClient *SafeWebsocketClient) connect() error {
|
|
|
|
|
return fmt.Errorf("failed to connect to %s: %w", wsClient.baseHost, err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
conn.SetPingHandler(func(pingData string) error {
|
|
|
|
|
if err := conn.WriteMessage(websocket.PongMessage, []byte(pingData)); err != nil {
|
|
|
|
|
if err == websocket.ErrCloseSent {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
if netErr, ok := err.(interface{ Timeout() bool }); ok && netErr.Timeout() {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
pingCtx, pingCancel := context.WithCancel(context.Background())
|
|
|
|
|
wsClient.mu.WriteHandler(func() error {
|
|
|
|
|
wsClient.cancelFuncs = append(wsClient.cancelFuncs, pingCancel)
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
pingCtx, pingCancel := context.WithCancel(context.Background())
|
|
|
|
|
go wsClient.startPingTicker(pingCtx)
|
|
|
|
|
wsClient.cancelFuncs = append(wsClient.cancelFuncs, pingCancel)
|
|
|
|
|
|
|
|
|
|
if wsClient.conn != nil {
|
|
|
|
|
wsClient.conn.Close()
|
|
|
|
|
@@ -251,32 +242,31 @@ func (wsClient *SafeWebsocketClient) connect() error {
|
|
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
wsClient.cancelFuncs = append(wsClient.cancelFuncs, cancel)
|
|
|
|
|
wsClient.mu.WriteHandler(func() error {
|
|
|
|
|
wsClient.cancelFuncs = append(wsClient.cancelFuncs, cancel)
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
var c *websocket.Conn
|
|
|
|
|
wsClient.mu.ReadHandler(func() error {
|
|
|
|
|
c = conn
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
for {
|
|
|
|
|
select {
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
|
fmt.Println("Writer stopped due to client shutdown")
|
|
|
|
|
log.Println("Writer stopped due to client shutdown")
|
|
|
|
|
return
|
|
|
|
|
case data := <-wsClient.writeChan:
|
|
|
|
|
if conn == nil {
|
|
|
|
|
if c == nil {
|
|
|
|
|
wsClient.triggerReconnect()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
messageType := websocket.TextMessage
|
|
|
|
|
switch data.MessageType {
|
|
|
|
|
case MessageTypePing:
|
|
|
|
|
messageType = websocket.PingMessage
|
|
|
|
|
case MessageTypePong:
|
|
|
|
|
messageType = websocket.PongMessage
|
|
|
|
|
case MessageTypeClose:
|
|
|
|
|
messageType = websocket.CloseMessage
|
|
|
|
|
}
|
|
|
|
|
writer, err := conn.NextWriter(messageType)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if _, err := writer.Write(data.Data); err != nil {
|
|
|
|
|
if err := c.WriteMessage(int(data.MessageType), data.Data); err != nil {
|
|
|
|
|
log.Printf("error on write message: %v\n", err)
|
|
|
|
|
wsClient.triggerReconnect()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@@ -285,46 +275,71 @@ func (wsClient *SafeWebsocketClient) connect() error {
|
|
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
wsClient.cancelFuncs = append(wsClient.cancelFuncs, cancel)
|
|
|
|
|
wsClient.mu.WriteHandler(func() error {
|
|
|
|
|
wsClient.cancelFuncs = append(wsClient.cancelFuncs, cancel)
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
var c *websocket.Conn
|
|
|
|
|
|
|
|
|
|
wsClient.mu.ReadHandler(func() error {
|
|
|
|
|
c = conn
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
if c == nil {
|
|
|
|
|
wsClient.triggerReconnect()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
for {
|
|
|
|
|
select {
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
|
fmt.Println("Reader stopped due to client shutdown")
|
|
|
|
|
log.Println("Reader stopped due to client shutdown")
|
|
|
|
|
return
|
|
|
|
|
default:
|
|
|
|
|
if conn == nil {
|
|
|
|
|
if err := c.SetReadDeadline(time.Now().Add(readDeadline)); err != nil {
|
|
|
|
|
log.Printf("error on read deadline: %v\n", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if err := conn.SetReadDeadline(time.Now().Add(readDeadline)); err != nil {
|
|
|
|
|
fmt.Printf("error on read deadline: %v\n", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
_, reader, err := conn.NextReader()
|
|
|
|
|
|
|
|
|
|
messageType, data, err := c.ReadMessage()
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Printf("Next Reader Closed: %v\n", err)
|
|
|
|
|
log.Printf("error on read message: %v\n", err)
|
|
|
|
|
wsClient.triggerReconnect()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
readerBytes, err := io.ReadAll(reader)
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Printf("io reader failed: %v\n", err)
|
|
|
|
|
wsClient.triggerReconnect()
|
|
|
|
|
return
|
|
|
|
|
if messageType != websocket.TextMessage {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
select {
|
|
|
|
|
case wsClient.dataChannel <- readerBytes:
|
|
|
|
|
case wsClient.dataChannel <- data:
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
|
return
|
|
|
|
|
default:
|
|
|
|
|
fmt.Println("Data channel full, dropping message")
|
|
|
|
|
log.Println("Data channel full, dropping message")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
conn.SetPingHandler(func(pingData string) error {
|
|
|
|
|
wsClient.writeChan <- Message{
|
|
|
|
|
MessageType: MessageTypePong,
|
|
|
|
|
Data: []byte(pingData),
|
|
|
|
|
}
|
|
|
|
|
// if err := conn.WriteMessage(websocket.PongMessage, []byte(pingData)); err != nil {
|
|
|
|
|
// if err == websocket.ErrCloseSent {
|
|
|
|
|
// return nil
|
|
|
|
|
// }
|
|
|
|
|
// if netErr, ok := err.(interface{ Timeout() bool }); ok && netErr.Timeout() {
|
|
|
|
|
// return nil
|
|
|
|
|
// }
|
|
|
|
|
// return err
|
|
|
|
|
// }
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -346,40 +361,6 @@ func (wsClient *SafeWebsocketClient) startPingTicker(ctx context.Context) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// func (wsClient *SafeWebsocketClient) startReceiveHandler(ctx context.Context) {
|
|
|
|
|
// for {
|
|
|
|
|
// select {
|
|
|
|
|
// case <-ctx.Done():
|
|
|
|
|
// log.Println("receive handler stopped")
|
|
|
|
|
// return
|
|
|
|
|
// default:
|
|
|
|
|
// if err := wsClient.mu.ReadHandler(func() error {
|
|
|
|
|
// conn := wsClient.conn
|
|
|
|
|
|
|
|
|
|
// if conn == nil {
|
|
|
|
|
// wsClient.triggerReconnect()
|
|
|
|
|
// return fmt.Errorf("connection closed")
|
|
|
|
|
// }
|
|
|
|
|
// _, message, err := conn.ReadMessage()
|
|
|
|
|
// if err != nil {
|
|
|
|
|
// wsClient.triggerReconnect()
|
|
|
|
|
// return fmt.Errorf("failed to read message: %v", err)
|
|
|
|
|
// }
|
|
|
|
|
// select {
|
|
|
|
|
// case wsClient.dataChannel <- message:
|
|
|
|
|
// case <-ctx.Done():
|
|
|
|
|
// log.Println("Reconnect handler stopped")
|
|
|
|
|
// default:
|
|
|
|
|
// log.Println("")
|
|
|
|
|
// }
|
|
|
|
|
// return nil
|
|
|
|
|
// }); err != nil {
|
|
|
|
|
// return
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
func (wsClient *SafeWebsocketClient) triggerReconnect() {
|
|
|
|
|
select {
|
|
|
|
|
case wsClient.reconnectCh <- struct{}{}:
|
|
|
|
|
@@ -395,11 +376,15 @@ func (wsClient *SafeWebsocketClient) reconnectHandler() {
|
|
|
|
|
case <-wsClient.reconnectCh:
|
|
|
|
|
log.Println("Reconnect triggered")
|
|
|
|
|
|
|
|
|
|
if wsClient.cancelFuncs != nil {
|
|
|
|
|
for _, cancel := range wsClient.cancelFuncs {
|
|
|
|
|
cancel()
|
|
|
|
|
wsClient.mu.ReadHandler(func() error {
|
|
|
|
|
if wsClient.cancelFuncs != nil {
|
|
|
|
|
for _, cancel := range wsClient.cancelFuncs {
|
|
|
|
|
cancel()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
wsClient.isConnected = false
|
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
|
|
|
|
|
|
@@ -461,11 +446,14 @@ func (wsClient *SafeWebsocketClient) Write(data []byte) error {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (wsClient *SafeWebsocketClient) Close() error {
|
|
|
|
|
if wsClient.cancelFuncs != nil {
|
|
|
|
|
for _, cancel := range wsClient.cancelFuncs {
|
|
|
|
|
cancel()
|
|
|
|
|
wsClient.mu.ReadHandler(func() error {
|
|
|
|
|
if wsClient.cancelFuncs != nil {
|
|
|
|
|
for _, cancel := range wsClient.cancelFuncs {
|
|
|
|
|
cancel()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if wsClient.reconnectChans != nil {
|
|
|
|
|
for _, reconnectChan := range wsClient.reconnectChans {
|
|
|
|
|
@@ -476,7 +464,7 @@ func (wsClient *SafeWebsocketClient) Close() error {
|
|
|
|
|
wsClient.conn.Close()
|
|
|
|
|
}
|
|
|
|
|
wsClient.isConnected = false
|
|
|
|
|
close(wsClient.dataChannel)
|
|
|
|
|
// close(wsClient.dataChannel)
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|