|
|
|
|
@@ -17,7 +17,7 @@ import (
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
pingPeriod = 10 * time.Second
|
|
|
|
|
readDeadline = 10 * time.Second
|
|
|
|
|
readDeadline = 30 * time.Second
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type SafeMap[K comparable, V any] struct {
|
|
|
|
|
@@ -276,6 +276,7 @@ func (wsClient *SafeWebsocketClient) connect() error {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if _, err := writer.Write(data.Data); err != nil {
|
|
|
|
|
wsClient.triggerReconnect()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@@ -288,10 +289,7 @@ func (wsClient *SafeWebsocketClient) connect() error {
|
|
|
|
|
wsClient.cancelFuncs = append(wsClient.cancelFuncs, cancel)
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
_, reader, err := conn.NextReader()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for {
|
|
|
|
|
select {
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
|
@@ -302,10 +300,21 @@ func (wsClient *SafeWebsocketClient) connect() error {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if err := conn.SetReadDeadline(time.Now().Add(readDeadline)); err != nil {
|
|
|
|
|
wsClient.triggerReconnect()
|
|
|
|
|
fmt.Printf("error on read deadline: %v\n", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mt, reader, err := conn.NextReader()
|
|
|
|
|
if err != nil {
|
|
|
|
|
wsClient.triggerReconnect()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if mt != websocket.TextMessage {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
readerBytes, err := io.ReadAll(reader)
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Printf("io reader failed: %v\n", err)
|
|
|
|
|
@@ -346,40 +355,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{}{}:
|
|
|
|
|
|