Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| f32d97eac4 | |||
| 4111dd0f25 | |||
| f91e24cc02 |
2
go.mod
2
go.mod
@@ -3,3 +3,5 @@ module git.neurocipta.com/rogerferdinan/safe-web-socket
|
|||||||
go 1.24.5
|
go 1.24.5
|
||||||
|
|
||||||
require github.com/gorilla/websocket v1.5.3
|
require github.com/gorilla/websocket v1.5.3
|
||||||
|
|
||||||
|
require git.neurocipta.com/rogerferdinan/custom-rwmutex v1.0.0 // indirect
|
||||||
|
|||||||
2
go.sum
2
go.sum
@@ -1,2 +1,4 @@
|
|||||||
|
git.neurocipta.com/rogerferdinan/custom-rwmutex v1.0.0 h1:KnNc40SrYsg0cksIIcQy/ca6bunkGADQOs1u7O/E+iY=
|
||||||
|
git.neurocipta.com/rogerferdinan/custom-rwmutex v1.0.0/go.mod h1:9DvvHc2UZhBwEs63NgO4IhiuHnBNtTuBkTJgiMnnCss=
|
||||||
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
|
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
|
||||||
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||||
|
|||||||
@@ -19,7 +19,6 @@ type Client struct {
|
|||||||
Send chan []byte
|
Send chan []byte
|
||||||
SubscribedPath string
|
SubscribedPath string
|
||||||
done chan struct{}
|
done chan struct{}
|
||||||
mu *CustomRwMutex
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewClient(conn *websocket.Conn, subscribedPath string) *Client {
|
func NewClient(conn *websocket.Conn, subscribedPath string) *Client {
|
||||||
@@ -28,7 +27,6 @@ func NewClient(conn *websocket.Conn, subscribedPath string) *Client {
|
|||||||
Send: make(chan []byte, 64),
|
Send: make(chan []byte, 64),
|
||||||
SubscribedPath: subscribedPath,
|
SubscribedPath: subscribedPath,
|
||||||
done: make(chan struct{}),
|
done: make(chan struct{}),
|
||||||
mu: NewCustomRwMutex(),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -37,8 +35,6 @@ type Hub struct {
|
|||||||
Broadcast chan []byte
|
Broadcast chan []byte
|
||||||
Register chan *Client
|
Register chan *Client
|
||||||
Unregister chan *Client
|
Unregister chan *Client
|
||||||
writeMu *CustomRwMutex
|
|
||||||
readMu *CustomRwMutex
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewHub() *Hub {
|
func NewHub() *Hub {
|
||||||
@@ -47,7 +43,6 @@ func NewHub() *Hub {
|
|||||||
Register: make(chan *Client),
|
Register: make(chan *Client),
|
||||||
Unregister: make(chan *Client),
|
Unregister: make(chan *Client),
|
||||||
Clients: make(map[*Client]bool),
|
Clients: make(map[*Client]bool),
|
||||||
writeMu: NewCustomRwMutex(),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,33 +0,0 @@
|
|||||||
package internal
|
|
||||||
|
|
||||||
import (
|
|
||||||
"sync"
|
|
||||||
)
|
|
||||||
|
|
||||||
type CustomRwMutex struct {
|
|
||||||
mu *sync.RWMutex
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewCustomRwMutex() *CustomRwMutex {
|
|
||||||
return &CustomRwMutex{
|
|
||||||
mu: &sync.RWMutex{},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (rwMu *CustomRwMutex) WriteHandler(fn func() error) error {
|
|
||||||
rwMu.mu.Lock()
|
|
||||||
defer rwMu.mu.Unlock()
|
|
||||||
if err := fn(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (rwMu *CustomRwMutex) ReadHandler(fn func() error) error {
|
|
||||||
rwMu.mu.RLock()
|
|
||||||
defer rwMu.mu.RUnlock()
|
|
||||||
if err := fn(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
@@ -5,8 +5,10 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
custom_rwmutex "git.neurocipta.com/rogerferdinan/custom-rwmutex"
|
||||||
"git.neurocipta.com/rogerferdinan/safe-web-socket/internal"
|
"git.neurocipta.com/rogerferdinan/safe-web-socket/internal"
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
)
|
)
|
||||||
@@ -18,7 +20,8 @@ const (
|
|||||||
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"`
|
||||||
path *string `nil_checkeer:"required"`
|
path *string
|
||||||
|
rawQuery *string
|
||||||
useTLS *bool
|
useTLS *bool
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -46,6 +49,11 @@ func (b *SafeWebsocketClientBuilder) Path(path string) *SafeWebsocketClientBuild
|
|||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *SafeWebsocketClientBuilder) RawQuery(rawQuery string) *SafeWebsocketClientBuilder {
|
||||||
|
b.rawQuery = &rawQuery
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
func (b *SafeWebsocketClientBuilder) Build() (*SafeWebsocketClient, error) {
|
func (b *SafeWebsocketClientBuilder) Build() (*SafeWebsocketClient, error) {
|
||||||
if err := internal.NilChecker(b); err != nil {
|
if err := internal.NilChecker(b); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -62,11 +70,12 @@ func (b *SafeWebsocketClientBuilder) Build() (*SafeWebsocketClient, error) {
|
|||||||
baseHost: *b.baseHost,
|
baseHost: *b.baseHost,
|
||||||
basePort: *b.basePort,
|
basePort: *b.basePort,
|
||||||
useTLS: useTLS,
|
useTLS: useTLS,
|
||||||
path: *b.path,
|
path: b.path,
|
||||||
|
rawQuery: b.rawQuery,
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
cancel: cancel,
|
cancel: cancel,
|
||||||
dataChannel: make(chan []byte, 1),
|
dataChannel: make(chan []byte, 1),
|
||||||
mu: internal.NewCustomRwMutex(),
|
mu: custom_rwmutex.NewCustomRwMutex(),
|
||||||
reconnectCh: make(chan struct{}, 1),
|
reconnectCh: make(chan struct{}, 1),
|
||||||
isConnected: false,
|
isConnected: false,
|
||||||
}
|
}
|
||||||
@@ -83,9 +92,10 @@ type SafeWebsocketClient struct {
|
|||||||
baseHost string
|
baseHost string
|
||||||
basePort uint16
|
basePort uint16
|
||||||
useTLS bool
|
useTLS bool
|
||||||
path string
|
path *string
|
||||||
|
rawQuery *string
|
||||||
dataChannel chan []byte
|
dataChannel chan []byte
|
||||||
mu *internal.CustomRwMutex
|
mu *custom_rwmutex.CustomRwMutex
|
||||||
conn *websocket.Conn
|
conn *websocket.Conn
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
cancel context.CancelFunc
|
cancel context.CancelFunc
|
||||||
@@ -103,8 +113,16 @@ func (wsClient *SafeWebsocketClient) connect() error {
|
|||||||
newURL := url.URL{
|
newURL := url.URL{
|
||||||
Scheme: scheme,
|
Scheme: scheme,
|
||||||
Host: fmt.Sprintf("%s:%d", wsClient.baseHost, wsClient.basePort),
|
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
|
||||||
|
}
|
||||||
|
|
||||||
conn, _, err := websocket.DefaultDialer.Dial(newURL.String(), nil)
|
conn, _, err := websocket.DefaultDialer.Dial(newURL.String(), nil)
|
||||||
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)
|
||||||
@@ -164,7 +182,7 @@ func (wsClient *SafeWebsocketClient) startReceiveHandler() {
|
|||||||
log.Println("Reconnect handler stopped")
|
log.Println("Reconnect handler stopped")
|
||||||
return
|
return
|
||||||
default:
|
default:
|
||||||
if err := wsClient.mu.WriteHandler(func() error {
|
if err := wsClient.mu.ReadHandler(func() error {
|
||||||
conn := wsClient.conn
|
conn := wsClient.conn
|
||||||
|
|
||||||
if conn == nil {
|
if conn == nil {
|
||||||
@@ -174,7 +192,6 @@ func (wsClient *SafeWebsocketClient) startReceiveHandler() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
fmt.Println(string(message))
|
|
||||||
wsClient.dataChannel <- message
|
wsClient.dataChannel <- message
|
||||||
return nil
|
return nil
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
|
|||||||
@@ -18,9 +18,6 @@ type SafeWebsocketServerBuilder struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewSafeWebsocketServerBuilder() *SafeWebsocketServerBuilder {
|
func NewSafeWebsocketServerBuilder() *SafeWebsocketServerBuilder {
|
||||||
h := internal.NewHub()
|
|
||||||
h.Run()
|
|
||||||
|
|
||||||
return &SafeWebsocketServerBuilder{
|
return &SafeWebsocketServerBuilder{
|
||||||
upgrader: &websocket.Upgrader{
|
upgrader: &websocket.Upgrader{
|
||||||
ReadBufferSize: 1024,
|
ReadBufferSize: 1024,
|
||||||
|
|||||||
Reference in New Issue
Block a user