Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| b296d73367 | |||
| 2200657ba7 | |||
| 07f7893a26 | |||
| 7f21b733ed | |||
| 9816426780 |
@@ -3,6 +3,7 @@ package internal
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
@@ -10,9 +11,10 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
writeWait = 10 * time.Second
|
||||
pongWait = 60 * time.Second
|
||||
pingPeriod = 25 * time.Second
|
||||
writeWait = 10 * time.Second
|
||||
pongWait = 60 * time.Second
|
||||
pingPeriod = (pongWait * 9) / 10
|
||||
maxMessageSize = 512
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
@@ -20,16 +22,16 @@ type Client struct {
|
||||
Conn *websocket.Conn
|
||||
Send chan []byte
|
||||
SubscribedPath string
|
||||
done chan struct{}
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
func NewClient(conn *websocket.Conn, subscribedPath string) *Client {
|
||||
return &Client{
|
||||
ID: uuid.NewString(),
|
||||
Conn: conn,
|
||||
Send: make(chan []byte, 1),
|
||||
Send: make(chan []byte, 256),
|
||||
SubscribedPath: subscribedPath,
|
||||
done: make(chan struct{}, 1),
|
||||
mu: sync.Mutex{},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,9 +44,9 @@ type Hub struct {
|
||||
|
||||
func NewHub() *Hub {
|
||||
return &Hub{
|
||||
Broadcast: make(chan []byte, 1),
|
||||
Register: make(chan *Client, 1),
|
||||
Unregister: make(chan *Client, 1),
|
||||
Broadcast: make(chan []byte, 256),
|
||||
Register: make(chan *Client, 10),
|
||||
Unregister: make(chan *Client, 10),
|
||||
Clients: make(map[*Client]bool),
|
||||
}
|
||||
}
|
||||
@@ -55,23 +57,22 @@ func (h *Hub) Run() {
|
||||
select {
|
||||
case client := <-h.Register:
|
||||
h.Clients[client] = true
|
||||
log.Println("Client registered")
|
||||
case c := <-h.Unregister:
|
||||
if v, ok := h.Clients[c]; ok {
|
||||
fmt.Println(v, c)
|
||||
delete(h.Clients, c)
|
||||
close(c.Send)
|
||||
log.Printf("Client registered %s\n", client.ID)
|
||||
case client := <-h.Unregister:
|
||||
if _, ok := h.Clients[client]; ok {
|
||||
delete(h.Clients, client)
|
||||
close(client.Send)
|
||||
}
|
||||
log.Println("Client Unregistered")
|
||||
log.Printf("Client Unregistered %s\n", client.ID)
|
||||
case message := <-h.Broadcast:
|
||||
for client := range h.Clients {
|
||||
client.Send <- message
|
||||
// select {
|
||||
// case client.Send <- message:
|
||||
// default:
|
||||
// close(client.Send)
|
||||
// delete(h.Clients, client)
|
||||
// }
|
||||
select {
|
||||
case client.Send <- message:
|
||||
default:
|
||||
close(client.Send)
|
||||
delete(h.Clients, client)
|
||||
log.Printf("Client %s removed (slow/disconnected)", client.ID)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -90,23 +91,36 @@ func WritePump(c *Client, h *Hub) {
|
||||
select {
|
||||
case message, ok := <-c.Send:
|
||||
c.Conn.SetWriteDeadline(time.Now().Add(writeWait))
|
||||
|
||||
if !ok {
|
||||
c.Conn.WriteMessage(websocket.CloseMessage, []byte{})
|
||||
fmt.Println(ok)
|
||||
return
|
||||
}
|
||||
|
||||
if err := c.Conn.WriteMessage(websocket.TextMessage, message); err != nil {
|
||||
fmt.Println(err)
|
||||
w, err := c.Conn.NextWriter(websocket.TextMessage)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
w.Write(message)
|
||||
|
||||
// Queue queued messages in the same buffer (optional optimization)
|
||||
n := len(c.Send)
|
||||
for i := 0; i < n; i++ {
|
||||
w.Write(<-c.Send)
|
||||
}
|
||||
|
||||
if err := w.Close(); err != nil {
|
||||
return
|
||||
}
|
||||
case <-pingTicker.C:
|
||||
c.Conn.SetWriteDeadline(time.Now().Add(writeWait))
|
||||
if err := c.Conn.WriteMessage(websocket.PingMessage, []byte{}); err != nil {
|
||||
|
||||
if err := c.Conn.WriteMessage(websocket.PingMessage, nil); err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -116,15 +130,16 @@ func ReadPump(c *Client, h *Hub) {
|
||||
c.Conn.Close()
|
||||
}()
|
||||
|
||||
c.Conn.SetReadLimit(1024)
|
||||
c.Conn.SetReadLimit(maxMessageSize)
|
||||
c.Conn.SetReadDeadline(time.Now().Add(pongWait))
|
||||
|
||||
c.Conn.SetPongHandler(func(string) error {
|
||||
c.Conn.SetReadDeadline(time.Now().Add(pongWait))
|
||||
return nil
|
||||
})
|
||||
|
||||
for {
|
||||
messageType, message, err := c.Conn.ReadMessage()
|
||||
_, message, err := c.Conn.ReadMessage()
|
||||
if err != nil {
|
||||
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure) {
|
||||
log.Printf("WebSocket error: %v", err)
|
||||
@@ -132,8 +147,6 @@ func ReadPump(c *Client, h *Hub) {
|
||||
break
|
||||
}
|
||||
|
||||
if messageType == websocket.TextMessage {
|
||||
log.Printf("Received: %s\n", message)
|
||||
}
|
||||
log.Printf("Received: %s\n", message)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -15,8 +16,9 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
pingPeriod = 10 * time.Second
|
||||
readDeadline = 30 * time.Second
|
||||
pingPeriod = 10 * time.Second
|
||||
readDeadline = 30 * time.Second
|
||||
writeDeadline = 10 * time.Second
|
||||
)
|
||||
|
||||
type MessageType uint
|
||||
@@ -36,6 +38,7 @@ type Message struct {
|
||||
type SafeWebsocketClientBuilder struct {
|
||||
baseHost *string `nil_checker:"required"`
|
||||
basePort *uint16 `nil_checker:"required"`
|
||||
headers *map[string]string
|
||||
path *string
|
||||
rawQuery *string
|
||||
isDrop *bool
|
||||
@@ -58,6 +61,11 @@ func (b *SafeWebsocketClientBuilder) BasePort(port uint16) *SafeWebsocketClientB
|
||||
return b
|
||||
}
|
||||
|
||||
func (b *SafeWebsocketClientBuilder) Headers(headers map[string]string) *SafeWebsocketClientBuilder {
|
||||
b.headers = &headers
|
||||
return b
|
||||
}
|
||||
|
||||
func (b *SafeWebsocketClientBuilder) UseTLS(useTLS bool) *SafeWebsocketClientBuilder {
|
||||
b.useTLS = &useTLS
|
||||
return b
|
||||
@@ -117,6 +125,7 @@ func (b *SafeWebsocketClientBuilder) Build(ctx context.Context) (*SafeWebsocketC
|
||||
wsClient := SafeWebsocketClient{
|
||||
baseHost: *b.baseHost,
|
||||
basePort: *b.basePort,
|
||||
headers: b.headers,
|
||||
useTLS: *b.useTLS,
|
||||
isDrop: *b.isDrop,
|
||||
path: b.path,
|
||||
@@ -142,6 +151,8 @@ func (b *SafeWebsocketClientBuilder) Build(ctx context.Context) (*SafeWebsocketC
|
||||
type SafeWebsocketClient struct {
|
||||
baseHost string
|
||||
basePort uint16
|
||||
headers *map[string]string
|
||||
|
||||
isDrop bool
|
||||
useTLS bool
|
||||
path *string
|
||||
@@ -159,7 +170,6 @@ type SafeWebsocketClient struct {
|
||||
doneMap *safemap.SafeMap[string, chan struct{}]
|
||||
|
||||
writeChan chan Message
|
||||
pongChan chan error
|
||||
}
|
||||
|
||||
func (wsClient *SafeWebsocketClient) connect() error {
|
||||
@@ -173,219 +183,88 @@ func (wsClient *SafeWebsocketClient) connect() error {
|
||||
Scheme: scheme,
|
||||
Host: fmt.Sprintf("%s:%d", wsClient.baseHost, wsClient.basePort),
|
||||
}
|
||||
|
||||
if wsClient.path != nil && strings.TrimSpace(*wsClient.path) != "" {
|
||||
newURL.Path = *wsClient.path
|
||||
}
|
||||
|
||||
if wsClient.rawQuery != nil && strings.TrimSpace(*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 {
|
||||
return fmt.Errorf("failed to connect to %s: %w", wsClient.baseHost, err)
|
||||
}
|
||||
|
||||
pingCtx, pingCancel := context.WithCancel(context.Background())
|
||||
pumpCtx, pumpCancel := context.WithCancel(context.Background())
|
||||
|
||||
wsClient.mu.WriteHandler(func() error {
|
||||
wsClient.cancelFuncs = append(wsClient.cancelFuncs, pingCancel)
|
||||
if wsClient.conn != nil {
|
||||
wsClient.conn.Close()
|
||||
}
|
||||
|
||||
wsClient.conn = conn
|
||||
|
||||
wsClient.cancelFuncs = append(wsClient.cancelFuncs, pingCancel, pumpCancel)
|
||||
return nil
|
||||
})
|
||||
|
||||
go wsClient.startPingTicker(pingCtx)
|
||||
go wsClient.writePump(pumpCtx, conn)
|
||||
go wsClient.readPump(pumpCtx, conn)
|
||||
|
||||
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
|
||||
})
|
||||
|
||||
if wsClient.conn != nil {
|
||||
wsClient.conn.Close()
|
||||
}
|
||||
wsClient.conn = conn
|
||||
wsClient.isConnected = true
|
||||
|
||||
go wsClient.writePump()
|
||||
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
|
||||
}
|
||||
|
||||
func (wsClient *SafeWebsocketClient) writePump() {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
wsClient.mu.WriteHandler(func() error {
|
||||
wsClient.cancelFuncs = append(wsClient.cancelFuncs, cancel)
|
||||
return nil
|
||||
})
|
||||
|
||||
var c *websocket.Conn
|
||||
wsClient.mu.ReadHandler(func() error {
|
||||
c = wsClient.conn
|
||||
return nil
|
||||
})
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
log.Println("Writer canceled by context")
|
||||
return
|
||||
case data := <-wsClient.writeChan:
|
||||
if c == nil {
|
||||
return
|
||||
}
|
||||
|
||||
if err := c.WriteMessage(int(data.MessageType), data.Data); err != nil {
|
||||
log.Printf("error on write message: %v\n", err)
|
||||
if data.MessageType == MessageTypePong {
|
||||
wsClient.pongChan <- err
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (wsClient *SafeWebsocketClient) readPump() {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
wsClient.mu.WriteHandler(func() error {
|
||||
wsClient.cancelFuncs = append(wsClient.cancelFuncs, cancel)
|
||||
return nil
|
||||
})
|
||||
|
||||
var c *websocket.Conn
|
||||
wsClient.mu.ReadHandler(func() error {
|
||||
c = wsClient.conn
|
||||
return nil
|
||||
})
|
||||
|
||||
if wsClient.isDrop {
|
||||
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
|
||||
default:
|
||||
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) {
|
||||
ticker := time.NewTicker(pingPeriod)
|
||||
defer ticker.Stop()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
log.Println("ping ticker canceled by context")
|
||||
return
|
||||
case <-ticker.C:
|
||||
wsClient.writeChan <- Message{
|
||||
MessageType: websocket.PingMessage,
|
||||
Data: []byte{},
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (wsClient *SafeWebsocketClient) triggerReconnect() {
|
||||
select {
|
||||
case wsClient.reconnectCh <- struct{}{}:
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
func (wsClient *SafeWebsocketClient) reconnectHandler() {
|
||||
backoff := 1 * time.Second
|
||||
maxBackoff := 30 * time.Second
|
||||
maxBackoff := 15 * time.Second
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-wsClient.reconnectCh:
|
||||
log.Println("Reconnect triggered")
|
||||
|
||||
wsClient.mu.ReadHandler(func() error {
|
||||
wsClient.mu.WriteHandler(func() error {
|
||||
if wsClient.cancelFuncs != nil {
|
||||
for _, cancel := range wsClient.cancelFuncs {
|
||||
cancel()
|
||||
}
|
||||
wsClient.cancelFuncs = nil
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
wsClient.isConnected = false
|
||||
|
||||
isInnerLoop := true
|
||||
for isInnerLoop {
|
||||
log.Printf("Attempting reconnect in %v...", backoff)
|
||||
@@ -410,7 +289,15 @@ func (wsClient *SafeWebsocketClient) reconnectHandler() {
|
||||
}
|
||||
if wsClient.reconnectChans != nil {
|
||||
for _, reconnectCh := range wsClient.reconnectChans {
|
||||
reconnectCh <- struct{}{}
|
||||
select {
|
||||
case reconnectCh <- struct{}{}:
|
||||
default: // prevent blocking if chan is full
|
||||
}
|
||||
}
|
||||
if len(wsClient.reconnectChans) > 1 {
|
||||
wsClient.reconnectChans = wsClient.reconnectChans[1:]
|
||||
} else {
|
||||
wsClient.reconnectChans = nil
|
||||
}
|
||||
}
|
||||
case <-wsClient.ctx.Done():
|
||||
@@ -421,6 +308,96 @@ func (wsClient *SafeWebsocketClient) reconnectHandler() {
|
||||
}
|
||||
}
|
||||
|
||||
func (wsClient *SafeWebsocketClient) writePump(ctx context.Context, c *websocket.Conn) {
|
||||
defer func() {
|
||||
c.Close()
|
||||
}()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
log.Println("Writer canceled by context")
|
||||
return
|
||||
case data := <-wsClient.writeChan:
|
||||
if err := c.SetWriteDeadline(time.Now().Add(writeDeadline)); err != nil {
|
||||
log.Printf("error setting write deadline: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
if err := c.WriteMessage(int(data.MessageType), data.Data); err != nil {
|
||||
log.Printf("error on write message: %v\n", err)
|
||||
wsClient.triggerReconnect() // Trigger reconnect on write failure
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (wsClient *SafeWebsocketClient) readPump(ctx context.Context, c *websocket.Conn) {
|
||||
defer func() {
|
||||
wsClient.triggerReconnect()
|
||||
c.Close()
|
||||
}()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
log.Println("Reader canceled by context")
|
||||
return
|
||||
default:
|
||||
// Set read deadline
|
||||
if err := c.SetReadDeadline(time.Now().Add(readDeadline)); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
messageType, data, err := c.ReadMessage()
|
||||
if err != nil {
|
||||
log.Printf("error on read message: %v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
if messageType != websocket.TextMessage {
|
||||
continue
|
||||
}
|
||||
|
||||
select {
|
||||
case wsClient.dataChannel <- data:
|
||||
case <-ctx.Done():
|
||||
return
|
||||
default:
|
||||
if wsClient.isDrop {
|
||||
log.Println("Data channel full, dropping message")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (wsClient *SafeWebsocketClient) startPingTicker(ctx context.Context) {
|
||||
ticker := time.NewTicker(pingPeriod)
|
||||
defer ticker.Stop()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
log.Println("ping ticker canceled by context")
|
||||
return
|
||||
case <-ticker.C:
|
||||
wsClient.writeChan <- Message{
|
||||
MessageType: websocket.PingMessage,
|
||||
Data: []byte{},
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (wsClient *SafeWebsocketClient) triggerReconnect() {
|
||||
select {
|
||||
case wsClient.reconnectCh <- struct{}{}:
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
func (wsClient *SafeWebsocketClient) ReconnectChannel() <-chan struct{} {
|
||||
reconnectCh := make(chan struct{}, 1)
|
||||
wsClient.mu.WriteHandler(func() error {
|
||||
|
||||
@@ -8,10 +8,18 @@ import (
|
||||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
"net/http"
|
||||
_ "net/http/pprof"
|
||||
|
||||
"git.neurocipta.com/rogerferdinan/safe-web-socket/v1/client"
|
||||
)
|
||||
|
||||
func main() {
|
||||
go func() {
|
||||
log.Println("Starting pprof server on :6060")
|
||||
log.Println(http.ListenAndServe(":6060", nil))
|
||||
}()
|
||||
|
||||
sigChan := make(chan os.Signal, 1)
|
||||
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
|
||||
|
||||
@@ -27,7 +35,9 @@ func main() {
|
||||
wsClient, err := client.NewSafeWebsocketClientBuilder().
|
||||
BaseHost("localhost").
|
||||
BasePort(8080).
|
||||
Path("/ws/test/data_1").
|
||||
Headers(map[string]string{
|
||||
"X-MBX-APIKEY": "abcd",
|
||||
}).Path("/ws/test/data_1").
|
||||
UseTLS(false).
|
||||
ChannelSize(1).
|
||||
Build(ctx)
|
||||
@@ -44,7 +54,7 @@ func main() {
|
||||
dataChannel := wsClient.DataChannel()
|
||||
|
||||
for data := range dataChannel {
|
||||
// _ = data
|
||||
fmt.Println(string(data))
|
||||
_ = data
|
||||
// fmt.Println(string(data))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,9 +17,9 @@ func main() {
|
||||
s, err := server.NewSafeWebsocketServerBuilder().
|
||||
BaseHost("localhost").
|
||||
BasePort(8080).
|
||||
ApiKey("").
|
||||
ApiKey("abcd").
|
||||
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 {
|
||||
jsonBytes, err := json.Marshal(ExampleData{
|
||||
Time: time.Now(),
|
||||
@@ -32,7 +32,7 @@ func main() {
|
||||
}
|
||||
}).
|
||||
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 {
|
||||
jsonBytes, err := json.Marshal(ExampleData{
|
||||
Time: time.Now(),
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"crypto/subtle"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
@@ -81,6 +82,7 @@ func (b *SafeWebsocketServerBuilder) HandleFuncWebsocket(pattern string, subscri
|
||||
}
|
||||
c := internal.NewClient(conn, subscribedPath)
|
||||
h.Register <- c
|
||||
|
||||
go internal.WritePump(c, h)
|
||||
go internal.ReadPump(c, h)
|
||||
go writeFunc(h.Broadcast)
|
||||
@@ -110,12 +112,17 @@ type SafeWebsocketServer struct {
|
||||
|
||||
func (s *SafeWebsocketServer) AuthMiddleware(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Header.Get("X-MBX-APIKEY") != s.apiKey {
|
||||
providedKey := r.Header.Get("X-MBX-APIKEY")
|
||||
expectedKey := s.apiKey
|
||||
|
||||
if subtle.ConstantTimeCompare([]byte(providedKey), []byte(expectedKey)) != 1 {
|
||||
internal.ErrorResponse(w, internal.NewStatusMessage().
|
||||
StatusCode(http.StatusForbidden).
|
||||
Message("X-MBX-APIKEY is missing").
|
||||
Build())
|
||||
return
|
||||
}
|
||||
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user