|
|
|
|
@@ -5,6 +5,7 @@ import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"log"
|
|
|
|
|
"net/url"
|
|
|
|
|
"strings"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"git.neurocipta.com/rogerferdinan/safe-web-socket/internal"
|
|
|
|
|
@@ -18,7 +19,8 @@ const (
|
|
|
|
|
type SafeWebsocketClientBuilder struct {
|
|
|
|
|
baseHost *string `nil_checker:"required"`
|
|
|
|
|
basePort *uint16 `nil_checker:"required"`
|
|
|
|
|
path *string `nil_checkeer:"required"`
|
|
|
|
|
path *string
|
|
|
|
|
rawQuery *string
|
|
|
|
|
useTLS *bool
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -46,6 +48,11 @@ func (b *SafeWebsocketClientBuilder) Path(path string) *SafeWebsocketClientBuild
|
|
|
|
|
return b
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (b *SafeWebsocketClientBuilder) RawQuery(rawQuery string) *SafeWebsocketClientBuilder {
|
|
|
|
|
b.rawQuery = &rawQuery
|
|
|
|
|
return b
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (b *SafeWebsocketClientBuilder) Build() (*SafeWebsocketClient, error) {
|
|
|
|
|
if err := internal.NilChecker(b); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
@@ -62,7 +69,8 @@ func (b *SafeWebsocketClientBuilder) Build() (*SafeWebsocketClient, error) {
|
|
|
|
|
baseHost: *b.baseHost,
|
|
|
|
|
basePort: *b.basePort,
|
|
|
|
|
useTLS: useTLS,
|
|
|
|
|
path: *b.path,
|
|
|
|
|
path: b.path,
|
|
|
|
|
rawQuery: b.rawQuery,
|
|
|
|
|
ctx: ctx,
|
|
|
|
|
cancel: cancel,
|
|
|
|
|
dataChannel: make(chan []byte, 1),
|
|
|
|
|
@@ -83,7 +91,8 @@ type SafeWebsocketClient struct {
|
|
|
|
|
baseHost string
|
|
|
|
|
basePort uint16
|
|
|
|
|
useTLS bool
|
|
|
|
|
path string
|
|
|
|
|
path *string
|
|
|
|
|
rawQuery *string
|
|
|
|
|
dataChannel chan []byte
|
|
|
|
|
mu *internal.CustomRwMutex
|
|
|
|
|
conn *websocket.Conn
|
|
|
|
|
@@ -103,8 +112,18 @@ func (wsClient *SafeWebsocketClient) connect() error {
|
|
|
|
|
newURL := url.URL{
|
|
|
|
|
Scheme: scheme,
|
|
|
|
|
Host: fmt.Sprintf("%s:%d", wsClient.baseHost, wsClient.basePort),
|
|
|
|
|
Path: wsClient.path,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if wsClient.path != nil && strings.TrimSpace(*wsClient.path) != "" {
|
|
|
|
|
newURL.Path = *wsClient.path
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if wsClient.rawQuery != nil && strings.TrimSpace(*wsClient.rawQuery) != "" {
|
|
|
|
|
newURL.RawQuery = *wsClient.rawQuery
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fmt.Println(newURL.String())
|
|
|
|
|
|
|
|
|
|
conn, _, err := websocket.DefaultDialer.Dial(newURL.String(), nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("failed to connect to %s: %w", wsClient.baseHost, err)
|
|
|
|
|
|