Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| b4e7238b0b | |||
| b092e36987 | |||
| 4f956b8fe9 | |||
| 967b8a98b3 |
@@ -1,7 +1,6 @@
|
|||||||
package internal
|
package internal
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"log"
|
"log"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -125,7 +124,7 @@ func ReadPump(c *Client, h *Hub) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if messageType == websocket.TextMessage {
|
if messageType == websocket.TextMessage {
|
||||||
fmt.Printf("Received: %s\n", message)
|
log.Printf("Received: %s\n", message)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package client
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"log"
|
"log"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -17,7 +16,7 @@ import (
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
pingPeriod = 10 * time.Second
|
pingPeriod = 10 * time.Second
|
||||||
readDeadline = 10 * time.Second
|
readDeadline = 30 * time.Second
|
||||||
)
|
)
|
||||||
|
|
||||||
type SafeMap[K comparable, V any] struct {
|
type SafeMap[K comparable, V any] struct {
|
||||||
@@ -235,12 +234,13 @@ func (wsClient *SafeWebsocketClient) connect() error {
|
|||||||
})
|
})
|
||||||
|
|
||||||
pingCtx, pingCancel := context.WithCancel(context.Background())
|
pingCtx, pingCancel := context.WithCancel(context.Background())
|
||||||
go wsClient.startPingTicker(pingCtx)
|
|
||||||
wsClient.mu.WriteHandler(func() error {
|
wsClient.mu.WriteHandler(func() error {
|
||||||
wsClient.cancelFuncs = append(wsClient.cancelFuncs, pingCancel)
|
wsClient.cancelFuncs = append(wsClient.cancelFuncs, pingCancel)
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
|
go wsClient.startPingTicker(pingCtx)
|
||||||
|
|
||||||
if wsClient.conn != nil {
|
if wsClient.conn != nil {
|
||||||
wsClient.conn.Close()
|
wsClient.conn.Close()
|
||||||
}
|
}
|
||||||
@@ -259,31 +259,21 @@ func (wsClient *SafeWebsocketClient) connect() error {
|
|||||||
wsClient.cancelFuncs = append(wsClient.cancelFuncs, cancel)
|
wsClient.cancelFuncs = append(wsClient.cancelFuncs, cancel)
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
fmt.Println("Writer stopped due to client shutdown")
|
log.Println("Writer stopped due to client shutdown")
|
||||||
return
|
return
|
||||||
case data := <-wsClient.writeChan:
|
case data := <-wsClient.writeChan:
|
||||||
if conn == nil {
|
if conn == nil {
|
||||||
wsClient.triggerReconnect()
|
wsClient.triggerReconnect()
|
||||||
return
|
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 := conn.WriteMessage(int(data.MessageType), data.Data); err != nil {
|
||||||
|
log.Printf("error on write message: %v\n", err)
|
||||||
|
wsClient.triggerReconnect()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -296,42 +286,41 @@ func (wsClient *SafeWebsocketClient) connect() error {
|
|||||||
wsClient.cancelFuncs = append(wsClient.cancelFuncs, cancel)
|
wsClient.cancelFuncs = append(wsClient.cancelFuncs, cancel)
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
fmt.Println("Reader stopped due to client shutdown")
|
log.Println("Reader stopped due to client shutdown")
|
||||||
return
|
return
|
||||||
default:
|
default:
|
||||||
if conn == nil {
|
if conn == nil {
|
||||||
|
wsClient.triggerReconnect()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err := conn.SetReadDeadline(time.Now().Add(readDeadline)); err != nil {
|
if err := conn.SetReadDeadline(time.Now().Add(readDeadline)); err != nil {
|
||||||
fmt.Printf("error on read deadline: %v\n", err)
|
log.Printf("error on read deadline: %v\n", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
_, reader, err := conn.NextReader()
|
|
||||||
|
messageType, data, err := conn.ReadMessage()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Next Reader Closed: %v\n", err)
|
log.Printf("error on read message: %v\n", err)
|
||||||
wsClient.triggerReconnect()
|
wsClient.triggerReconnect()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
readerBytes, err := io.ReadAll(reader)
|
if messageType != websocket.TextMessage {
|
||||||
if err != nil {
|
continue
|
||||||
fmt.Printf("io reader failed: %v\n", err)
|
|
||||||
wsClient.triggerReconnect()
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case wsClient.dataChannel <- readerBytes:
|
case wsClient.dataChannel <- data:
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
return
|
return
|
||||||
default:
|
default:
|
||||||
fmt.Println("Data channel full, dropping message")
|
log.Println("Data channel full, dropping message")
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
@@ -356,40 +345,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() {
|
func (wsClient *SafeWebsocketClient) triggerReconnect() {
|
||||||
select {
|
select {
|
||||||
case wsClient.reconnectCh <- struct{}{}:
|
case wsClient.reconnectCh <- struct{}{}:
|
||||||
@@ -493,7 +448,7 @@ func (wsClient *SafeWebsocketClient) Close() error {
|
|||||||
wsClient.conn.Close()
|
wsClient.conn.Close()
|
||||||
}
|
}
|
||||||
wsClient.isConnected = false
|
wsClient.isConnected = false
|
||||||
close(wsClient.dataChannel)
|
// close(wsClient.dataChannel)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ func main() {
|
|||||||
BasePort(8080).
|
BasePort(8080).
|
||||||
Path("/ws/test/data_1").
|
Path("/ws/test/data_1").
|
||||||
UseTLS(false).
|
UseTLS(false).
|
||||||
|
ChannelSize(30).
|
||||||
Build(ctx)
|
Build(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
|
|||||||
Reference in New Issue
Block a user