Compare commits
2 Commits
v0.0.43
...
7f21b733ed
| Author | SHA1 | Date | |
|---|---|---|---|
| 7f21b733ed | |||
| 9816426780 |
@@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@@ -36,6 +37,7 @@ type Message struct {
|
|||||||
type SafeWebsocketClientBuilder struct {
|
type SafeWebsocketClientBuilder struct {
|
||||||
baseHost *string `nil_checker:"required"`
|
baseHost *string `nil_checker:"required"`
|
||||||
basePort *uint16 `nil_checker:"required"`
|
basePort *uint16 `nil_checker:"required"`
|
||||||
|
headers *map[string]string
|
||||||
path *string
|
path *string
|
||||||
rawQuery *string
|
rawQuery *string
|
||||||
isDrop *bool
|
isDrop *bool
|
||||||
@@ -58,6 +60,11 @@ func (b *SafeWebsocketClientBuilder) BasePort(port uint16) *SafeWebsocketClientB
|
|||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *SafeWebsocketClientBuilder) Headers(headers map[string]string) *SafeWebsocketClientBuilder {
|
||||||
|
b.headers = &headers
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
func (b *SafeWebsocketClientBuilder) UseTLS(useTLS bool) *SafeWebsocketClientBuilder {
|
func (b *SafeWebsocketClientBuilder) UseTLS(useTLS bool) *SafeWebsocketClientBuilder {
|
||||||
b.useTLS = &useTLS
|
b.useTLS = &useTLS
|
||||||
return b
|
return b
|
||||||
@@ -117,6 +124,7 @@ func (b *SafeWebsocketClientBuilder) Build(ctx context.Context) (*SafeWebsocketC
|
|||||||
wsClient := SafeWebsocketClient{
|
wsClient := SafeWebsocketClient{
|
||||||
baseHost: *b.baseHost,
|
baseHost: *b.baseHost,
|
||||||
basePort: *b.basePort,
|
basePort: *b.basePort,
|
||||||
|
headers: b.headers,
|
||||||
useTLS: *b.useTLS,
|
useTLS: *b.useTLS,
|
||||||
isDrop: *b.isDrop,
|
isDrop: *b.isDrop,
|
||||||
path: b.path,
|
path: b.path,
|
||||||
@@ -142,6 +150,8 @@ func (b *SafeWebsocketClientBuilder) Build(ctx context.Context) (*SafeWebsocketC
|
|||||||
type SafeWebsocketClient struct {
|
type SafeWebsocketClient struct {
|
||||||
baseHost string
|
baseHost string
|
||||||
basePort uint16
|
basePort uint16
|
||||||
|
headers *map[string]string
|
||||||
|
|
||||||
isDrop bool
|
isDrop bool
|
||||||
useTLS bool
|
useTLS bool
|
||||||
path *string
|
path *string
|
||||||
@@ -159,7 +169,6 @@ type SafeWebsocketClient struct {
|
|||||||
doneMap *safemap.SafeMap[string, chan struct{}]
|
doneMap *safemap.SafeMap[string, chan struct{}]
|
||||||
|
|
||||||
writeChan chan Message
|
writeChan chan Message
|
||||||
pongChan chan error
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (wsClient *SafeWebsocketClient) connect() error {
|
func (wsClient *SafeWebsocketClient) connect() error {
|
||||||
@@ -182,7 +191,15 @@ func (wsClient *SafeWebsocketClient) connect() error {
|
|||||||
newURL.RawQuery = *wsClient.rawQuery
|
newURL.RawQuery = *wsClient.rawQuery
|
||||||
}
|
}
|
||||||
|
|
||||||
conn, _, err := websocket.DefaultDialer.Dial(newURL.String(), nil)
|
header := make(http.Header)
|
||||||
|
|
||||||
|
if wsClient.headers != nil {
|
||||||
|
for k, v := range *wsClient.headers {
|
||||||
|
header.Set(k, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
conn, _, err := websocket.DefaultDialer.Dial(newURL.String(), header)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to connect to %s: %w", wsClient.baseHost, err)
|
return fmt.Errorf("failed to connect to %s: %w", wsClient.baseHost, err)
|
||||||
}
|
}
|
||||||
@@ -201,23 +218,30 @@ func (wsClient *SafeWebsocketClient) connect() error {
|
|||||||
wsClient.conn = conn
|
wsClient.conn = conn
|
||||||
wsClient.isConnected = true
|
wsClient.isConnected = true
|
||||||
|
|
||||||
|
conn.SetPingHandler(func(pingData string) error {
|
||||||
|
if err := conn.SetReadDeadline(time.Now().Add(readDeadline)); err != nil {
|
||||||
|
log.Printf("error on read deadline: %v\n", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
wsClient.writeChan <- Message{
|
||||||
|
MessageType: MessageTypePong,
|
||||||
|
Data: []byte(pingData),
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
|
conn.SetPongHandler(func(pingData string) error {
|
||||||
|
if err := conn.SetReadDeadline(time.Now().Add(readDeadline)); err != nil {
|
||||||
|
log.Printf("error on read deadline: %v\n", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
go wsClient.writePump()
|
go wsClient.writePump()
|
||||||
go wsClient.readPump()
|
go wsClient.readPump()
|
||||||
|
|
||||||
// conn.SetPingHandler(func(pingData string) error {
|
|
||||||
// wsClient.writeChan <- Message{
|
|
||||||
// MessageType: MessageTypePong,
|
|
||||||
// Data: []byte(pingData),
|
|
||||||
// }
|
|
||||||
|
|
||||||
// select {
|
|
||||||
// case err := <-wsClient.pongChan:
|
|
||||||
// return err
|
|
||||||
// default:
|
|
||||||
// }
|
|
||||||
// return nil
|
|
||||||
// })
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -246,9 +270,6 @@ func (wsClient *SafeWebsocketClient) writePump() {
|
|||||||
|
|
||||||
if err := c.WriteMessage(int(data.MessageType), data.Data); err != nil {
|
if err := c.WriteMessage(int(data.MessageType), data.Data); err != nil {
|
||||||
log.Printf("error on write message: %v\n", err)
|
log.Printf("error on write message: %v\n", err)
|
||||||
if data.MessageType == MessageTypePong {
|
|
||||||
wsClient.pongChan <- err
|
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -268,7 +289,6 @@ func (wsClient *SafeWebsocketClient) readPump() {
|
|||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
if wsClient.isDrop {
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
@@ -300,46 +320,12 @@ func (wsClient *SafeWebsocketClient) readPump() {
|
|||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
return
|
return
|
||||||
default:
|
default:
|
||||||
|
if wsClient.isDrop {
|
||||||
log.Println("Data channel full, dropping message")
|
log.Println("Data channel full, dropping message")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-ctx.Done():
|
|
||||||
log.Println("Reader canceled by context")
|
|
||||||
return
|
|
||||||
default:
|
|
||||||
if c == nil {
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := c.SetReadDeadline(time.Now().Add(readDeadline)); err != nil {
|
|
||||||
log.Printf("error on read deadline: %v\n", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
messageType, data, err := c.ReadMessage()
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("error on read message: %v\n", err)
|
|
||||||
wsClient.triggerReconnect()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if messageType != websocket.TextMessage {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
select {
|
|
||||||
case wsClient.dataChannel <- data:
|
|
||||||
case <-ctx.Done():
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (wsClient *SafeWebsocketClient) startPingTicker(ctx context.Context) {
|
func (wsClient *SafeWebsocketClient) startPingTicker(ctx context.Context) {
|
||||||
|
|||||||
@@ -8,10 +8,18 @@ import (
|
|||||||
"os/signal"
|
"os/signal"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
|
"net/http"
|
||||||
|
_ "net/http/pprof"
|
||||||
|
|
||||||
"git.neurocipta.com/rogerferdinan/safe-web-socket/v1/client"
|
"git.neurocipta.com/rogerferdinan/safe-web-socket/v1/client"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
go func() {
|
||||||
|
log.Println("Starting pprof server on :6060")
|
||||||
|
log.Println(http.ListenAndServe(":6060", nil))
|
||||||
|
}()
|
||||||
|
|
||||||
sigChan := make(chan os.Signal, 1)
|
sigChan := make(chan os.Signal, 1)
|
||||||
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
|
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
|
||||||
|
|
||||||
@@ -27,7 +35,9 @@ func main() {
|
|||||||
wsClient, err := client.NewSafeWebsocketClientBuilder().
|
wsClient, err := client.NewSafeWebsocketClientBuilder().
|
||||||
BaseHost("localhost").
|
BaseHost("localhost").
|
||||||
BasePort(8080).
|
BasePort(8080).
|
||||||
Path("/ws/test/data_1").
|
Headers(map[string]string{
|
||||||
|
"X-MBX-APIKEY": "abcd",
|
||||||
|
}).Path("/ws/test/data_1").
|
||||||
UseTLS(false).
|
UseTLS(false).
|
||||||
ChannelSize(1).
|
ChannelSize(1).
|
||||||
Build(ctx)
|
Build(ctx)
|
||||||
@@ -44,7 +54,7 @@ func main() {
|
|||||||
dataChannel := wsClient.DataChannel()
|
dataChannel := wsClient.DataChannel()
|
||||||
|
|
||||||
for data := range dataChannel {
|
for data := range dataChannel {
|
||||||
// _ = data
|
_ = data
|
||||||
fmt.Println(string(data))
|
// fmt.Println(string(data))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,9 +17,9 @@ func main() {
|
|||||||
s, err := server.NewSafeWebsocketServerBuilder().
|
s, err := server.NewSafeWebsocketServerBuilder().
|
||||||
BaseHost("localhost").
|
BaseHost("localhost").
|
||||||
BasePort(8080).
|
BasePort(8080).
|
||||||
ApiKey("").
|
ApiKey("abcd").
|
||||||
HandleFuncWebsocket("/ws/test/", "data_1", func(c chan []byte) {
|
HandleFuncWebsocket("/ws/test/", "data_1", func(c chan []byte) {
|
||||||
ticker := time.NewTicker(10 * time.Millisecond)
|
ticker := time.NewTicker(100 * time.Millisecond)
|
||||||
for range ticker.C {
|
for range ticker.C {
|
||||||
jsonBytes, err := json.Marshal(ExampleData{
|
jsonBytes, err := json.Marshal(ExampleData{
|
||||||
Time: time.Now(),
|
Time: time.Now(),
|
||||||
@@ -32,7 +32,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
}).
|
}).
|
||||||
HandleFuncWebsocket("/ws/test/", "data_2", func(c chan []byte) {
|
HandleFuncWebsocket("/ws/test/", "data_2", func(c chan []byte) {
|
||||||
ticker := time.NewTicker(10 * time.Millisecond)
|
ticker := time.NewTicker(100 * time.Millisecond)
|
||||||
for range ticker.C {
|
for range ticker.C {
|
||||||
jsonBytes, err := json.Marshal(ExampleData{
|
jsonBytes, err := json.Marshal(ExampleData{
|
||||||
Time: time.Now(),
|
Time: time.Now(),
|
||||||
|
|||||||
@@ -115,6 +115,7 @@ func (s *SafeWebsocketServer) AuthMiddleware(next http.Handler) http.Handler {
|
|||||||
StatusCode(http.StatusForbidden).
|
StatusCode(http.StatusForbidden).
|
||||||
Message("X-MBX-APIKEY is missing").
|
Message("X-MBX-APIKEY is missing").
|
||||||
Build())
|
Build())
|
||||||
|
return
|
||||||
}
|
}
|
||||||
next.ServeHTTP(w, r)
|
next.ServeHTTP(w, r)
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user