Compare commits
24 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 70d9a37a4e | |||
| 2ab2e07b9b | |||
| 9a9c65c24c | |||
| 8a11ce0103 | |||
| cf63683c9c | |||
| 92bd56aac0 | |||
| b4e7238b0b | |||
| b092e36987 | |||
| 4f956b8fe9 | |||
| 967b8a98b3 | |||
| d09d389011 | |||
| c550701dfa | |||
| 2225391fc3 | |||
| 9f8ee49b5c | |||
| 735f55858e | |||
| 5e5df9090f | |||
| 1537e58444 | |||
| 66b27082b9 | |||
| 9d74e72ede | |||
| dfbe2f2808 | |||
| 4792d638c1 | |||
| b96e574726 | |||
| 752804cc58 | |||
| e686e69a24 |
@@ -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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,7 +15,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
pingPeriod = 10 * time.Second
|
pingPeriod = 10 * time.Second
|
||||||
|
readDeadline = 120 * time.Second
|
||||||
)
|
)
|
||||||
|
|
||||||
type SafeMap[K comparable, V any] struct {
|
type SafeMap[K comparable, V any] struct {
|
||||||
@@ -64,12 +65,28 @@ func (sm *SafeMap[K, V]) Len() int {
|
|||||||
return count
|
return count
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type MessageType uint
|
||||||
|
|
||||||
|
const (
|
||||||
|
MessageTypeText MessageType = iota + 1
|
||||||
|
MessageTypePing MessageType = iota
|
||||||
|
MessageTypePong MessageType = iota
|
||||||
|
MessageTypeClose MessageType = iota
|
||||||
|
)
|
||||||
|
|
||||||
|
type Message struct {
|
||||||
|
MessageType MessageType
|
||||||
|
Data []byte
|
||||||
|
}
|
||||||
|
|
||||||
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
|
path *string
|
||||||
rawQuery *string
|
rawQuery *string
|
||||||
useTLS *bool
|
useTLS *bool
|
||||||
|
channelSize *int64
|
||||||
|
authenticateFn func(*SafeWebsocketClient) error
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSafeWebsocketClientBuilder() *SafeWebsocketClientBuilder {
|
func NewSafeWebsocketClientBuilder() *SafeWebsocketClientBuilder {
|
||||||
@@ -91,6 +108,11 @@ func (b *SafeWebsocketClientBuilder) UseTLS(useTLS bool) *SafeWebsocketClientBui
|
|||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *SafeWebsocketClientBuilder) AuthenticateFn(authenticateFn func(*SafeWebsocketClient) error) *SafeWebsocketClientBuilder {
|
||||||
|
b.authenticateFn = authenticateFn
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
func (b *SafeWebsocketClientBuilder) Path(path string) *SafeWebsocketClientBuilder {
|
func (b *SafeWebsocketClientBuilder) Path(path string) *SafeWebsocketClientBuilder {
|
||||||
b.path = &path
|
b.path = &path
|
||||||
return b
|
return b
|
||||||
@@ -101,7 +123,12 @@ func (b *SafeWebsocketClientBuilder) RawQuery(rawQuery string) *SafeWebsocketCli
|
|||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *SafeWebsocketClientBuilder) Build() (*SafeWebsocketClient, error) {
|
func (b *SafeWebsocketClientBuilder) ChannelSize(channelSize int64) *SafeWebsocketClientBuilder {
|
||||||
|
b.channelSize = &channelSize
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *SafeWebsocketClientBuilder) Build(ctx context.Context) (*SafeWebsocketClient, error) {
|
||||||
if err := internal.NilChecker(b); err != nil {
|
if err := internal.NilChecker(b); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -111,19 +138,32 @@ func (b *SafeWebsocketClientBuilder) Build() (*SafeWebsocketClient, error) {
|
|||||||
useTLS = *b.useTLS
|
useTLS = *b.useTLS
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if b.channelSize == nil {
|
||||||
|
channelSize := int64(1)
|
||||||
|
b.channelSize = &channelSize
|
||||||
|
}
|
||||||
|
|
||||||
wsClient := SafeWebsocketClient{
|
wsClient := SafeWebsocketClient{
|
||||||
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,
|
rawQuery: b.rawQuery,
|
||||||
dataChannel: make(chan []byte, 1),
|
dataChannel: make(chan []byte, *b.channelSize),
|
||||||
mu: custom_rwmutex.NewCustomRwMutex(),
|
mu: custom_rwmutex.NewCustomRwMutex(),
|
||||||
|
ctx: ctx,
|
||||||
reconnectCh: make(chan struct{}, 1),
|
reconnectCh: make(chan struct{}, 1),
|
||||||
isConnected: false,
|
isConnected: false,
|
||||||
doneMap: NewSafeMap[string, chan struct{}](),
|
doneMap: NewSafeMap[string, chan struct{}](),
|
||||||
|
writeChan: make(chan Message),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if b.authenticateFn != nil {
|
||||||
|
wsClient.authenticateFn = b.authenticateFn
|
||||||
|
}
|
||||||
|
|
||||||
|
go wsClient.reconnectHandler()
|
||||||
|
|
||||||
if err := wsClient.connect(); err != nil {
|
if err := wsClient.connect(); err != nil {
|
||||||
return nil, fmt.Errorf("failed to establish initial connection: %v", err)
|
return nil, fmt.Errorf("failed to establish initial connection: %v", err)
|
||||||
}
|
}
|
||||||
@@ -132,20 +172,28 @@ func (b *SafeWebsocketClientBuilder) Build() (*SafeWebsocketClient, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type SafeWebsocketClient struct {
|
type SafeWebsocketClient struct {
|
||||||
baseHost string
|
baseHost string
|
||||||
basePort uint16
|
basePort uint16
|
||||||
useTLS bool
|
useTLS bool
|
||||||
path *string
|
path *string
|
||||||
rawQuery *string
|
rawQuery *string
|
||||||
|
|
||||||
dataChannel chan []byte
|
dataChannel chan []byte
|
||||||
mu *custom_rwmutex.CustomRwMutex
|
mu *custom_rwmutex.CustomRwMutex
|
||||||
conn *websocket.Conn
|
conn *websocket.Conn
|
||||||
ctx context.Context
|
cancelFuncs []context.CancelFunc
|
||||||
cancel context.CancelFunc
|
|
||||||
reconnectCh chan struct{}
|
|
||||||
isConnected bool
|
|
||||||
|
|
||||||
doneMap *SafeMap[string, chan struct{}]
|
ctx context.Context
|
||||||
|
Cancel context.CancelFunc
|
||||||
|
|
||||||
|
reconnectCh chan struct{}
|
||||||
|
reconnectChans []chan struct{}
|
||||||
|
isConnected bool
|
||||||
|
doneMap *SafeMap[string, chan struct{}]
|
||||||
|
authenticateFn func(*SafeWebsocketClient) error
|
||||||
|
|
||||||
|
writeChan chan Message
|
||||||
|
pongChan chan error
|
||||||
}
|
}
|
||||||
|
|
||||||
func (wsClient *SafeWebsocketClient) connect() error {
|
func (wsClient *SafeWebsocketClient) connect() error {
|
||||||
@@ -173,86 +221,143 @@ func (wsClient *SafeWebsocketClient) connect() error {
|
|||||||
return fmt.Errorf("failed to connect to %s: %w", wsClient.baseHost, err)
|
return fmt.Errorf("failed to connect to %s: %w", wsClient.baseHost, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
conn.SetPingHandler(func(pingData string) error {
|
pingCtx, pingCancel := context.WithCancel(context.Background())
|
||||||
return wsClient.mu.WriteHandler(func() error {
|
|
||||||
if err := conn.WriteMessage(websocket.PongMessage, []byte(pingData)); err != nil {
|
|
||||||
if err == websocket.ErrCloseSent {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
if netErr, ok := err.(interface{ Timeout() bool }); ok && netErr.Timeout() {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
wsClient.mu.WriteHandler(func() error {
|
wsClient.mu.WriteHandler(func() error {
|
||||||
wsClient.conn.Close()
|
wsClient.cancelFuncs = append(wsClient.cancelFuncs, pingCancel)
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
|
||||||
wsClient.ctx = ctx
|
|
||||||
wsClient.cancel = cancel
|
|
||||||
wsClient.conn = conn
|
|
||||||
wsClient.isConnected = true
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
go wsClient.startPingTicker()
|
go wsClient.startPingTicker(pingCtx)
|
||||||
go wsClient.startReceiveHandler()
|
|
||||||
go wsClient.reconnectHandler()
|
if wsClient.conn != nil {
|
||||||
|
wsClient.conn.Close()
|
||||||
|
}
|
||||||
|
wsClient.conn = conn
|
||||||
|
wsClient.isConnected = true
|
||||||
|
|
||||||
|
if wsClient.authenticateFn != nil {
|
||||||
|
if err := wsClient.authenticateFn(wsClient); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
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 = conn
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
log.Println("Writer stopped due to client shutdown")
|
||||||
|
return
|
||||||
|
case data := <-wsClient.writeChan:
|
||||||
|
if c == nil {
|
||||||
|
wsClient.triggerReconnect()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := c.WriteMessage(int(data.MessageType), data.Data); err != nil {
|
||||||
|
log.Printf("error on write message: %v\n", err)
|
||||||
|
wsClient.triggerReconnect()
|
||||||
|
if data.MessageType == MessageTypePong {
|
||||||
|
wsClient.pongChan <- err
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
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 = conn
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
if c == nil {
|
||||||
|
wsClient.triggerReconnect()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
log.Println("Reader stopped due to client shutdown")
|
||||||
|
return
|
||||||
|
default:
|
||||||
|
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")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
func (wsClient *SafeWebsocketClient) startPingTicker() {
|
func (wsClient *SafeWebsocketClient) startPingTicker(ctx context.Context) {
|
||||||
ticker := time.NewTicker(pingPeriod)
|
ticker := time.NewTicker(pingPeriod)
|
||||||
defer ticker.Stop()
|
defer ticker.Stop()
|
||||||
|
|
||||||
doneKey := "startPingTicker"
|
|
||||||
wsClient.doneMap.Store(doneKey, make(chan struct{}))
|
|
||||||
done, _ := wsClient.doneMap.Load(doneKey)
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
log.Println("ping ticker stopped")
|
||||||
|
return
|
||||||
case <-ticker.C:
|
case <-ticker.C:
|
||||||
wsClient.mu.WriteHandler(func() error {
|
wsClient.writeChan <- Message{
|
||||||
if err := wsClient.conn.WriteMessage(websocket.PingMessage, []byte{}); err != nil {
|
MessageType: websocket.PingMessage,
|
||||||
log.Printf("Ping failed: %v. Will attempt reconnect.", err)
|
Data: []byte{},
|
||||||
wsClient.triggerReconnect()
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
case <-done:
|
|
||||||
log.Println("Ping ticker stopped")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (wsClient *SafeWebsocketClient) startReceiveHandler() {
|
|
||||||
doneKey := "startReceiveHandler"
|
|
||||||
wsClient.doneMap.Store(doneKey, make(chan struct{}))
|
|
||||||
done, _ := wsClient.doneMap.Load(doneKey)
|
|
||||||
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-done:
|
|
||||||
log.Println("Reconnect handler stopped")
|
|
||||||
return
|
|
||||||
default:
|
|
||||||
conn := wsClient.conn
|
|
||||||
|
|
||||||
if conn == nil {
|
|
||||||
wsClient.triggerReconnect()
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
_, message, err := conn.ReadMessage()
|
|
||||||
if err != nil {
|
|
||||||
wsClient.triggerReconnect()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
wsClient.dataChannel <- message
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -265,25 +370,102 @@ func (wsClient *SafeWebsocketClient) triggerReconnect() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (wsClient *SafeWebsocketClient) reconnectHandler() {
|
func (wsClient *SafeWebsocketClient) reconnectHandler() {
|
||||||
doneKey := "reconnectHandler"
|
backoff := 1 * time.Second
|
||||||
wsClient.doneMap.Store(doneKey, make(chan struct{}))
|
maxBackoff := 30 * time.Second
|
||||||
done, _ := wsClient.doneMap.Load(doneKey)
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-wsClient.reconnectCh:
|
case <-wsClient.reconnectCh:
|
||||||
wsClient.cancel()
|
log.Println("Reconnect triggered")
|
||||||
wsClient.connect()
|
|
||||||
|
|
||||||
wsClient.doneMap.Range(func(s string, c chan struct{}) bool {
|
wsClient.mu.ReadHandler(func() error {
|
||||||
c <- struct{}{}
|
if wsClient.cancelFuncs != nil {
|
||||||
return true
|
for _, cancel := range wsClient.cancelFuncs {
|
||||||
|
cancel()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
})
|
})
|
||||||
case <-done:
|
|
||||||
|
wsClient.isConnected = false
|
||||||
|
time.Sleep(100 * time.Millisecond)
|
||||||
|
|
||||||
|
isInnerLoop := true
|
||||||
|
for isInnerLoop {
|
||||||
|
log.Printf("Attempting reconnect in %v...", backoff)
|
||||||
|
select {
|
||||||
|
case <-time.After(backoff):
|
||||||
|
if err := wsClient.connect(); err != nil {
|
||||||
|
log.Printf("Reconnect failed: %v", err)
|
||||||
|
if backoff < maxBackoff {
|
||||||
|
backoff *= 2
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
log.Println("Reconnected successfully")
|
||||||
|
backoff = 1 * time.Second
|
||||||
|
isInnerLoop = false
|
||||||
|
continue
|
||||||
|
case <-wsClient.ctx.Done():
|
||||||
|
log.Println("reconnect handler stopped due to client shutdown")
|
||||||
|
wsClient.Close()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if wsClient.reconnectChans != nil {
|
||||||
|
for _, reconnectCh := range wsClient.reconnectChans {
|
||||||
|
reconnectCh <- struct{}{}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case <-wsClient.ctx.Done():
|
||||||
|
log.Println("reconnect handler stopped due to client shutdown")
|
||||||
|
wsClient.Close()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (wsClient *SafeWebsocketClient) ReconnectChannel() <-chan struct{} {
|
||||||
|
reconnectCh := make(chan struct{})
|
||||||
|
wsClient.mu.WriteHandler(func() error {
|
||||||
|
wsClient.reconnectChans = append(wsClient.reconnectChans, reconnectCh)
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
|
return reconnectCh
|
||||||
|
}
|
||||||
|
|
||||||
func (wsClient *SafeWebsocketClient) DataChannel() <-chan []byte {
|
func (wsClient *SafeWebsocketClient) DataChannel() <-chan []byte {
|
||||||
return wsClient.dataChannel
|
return wsClient.dataChannel
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (wsClient *SafeWebsocketClient) Write(data []byte) error {
|
||||||
|
wsClient.writeChan <- Message{
|
||||||
|
MessageType: MessageTypeText,
|
||||||
|
Data: data,
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (wsClient *SafeWebsocketClient) Close() error {
|
||||||
|
wsClient.mu.ReadHandler(func() error {
|
||||||
|
if wsClient.cancelFuncs != nil {
|
||||||
|
for _, cancel := range wsClient.cancelFuncs {
|
||||||
|
cancel()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
|
if wsClient.reconnectChans != nil {
|
||||||
|
for _, reconnectChan := range wsClient.reconnectChans {
|
||||||
|
close(reconnectChan)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if wsClient.conn != nil {
|
||||||
|
wsClient.conn.Close()
|
||||||
|
}
|
||||||
|
wsClient.isConnected = false
|
||||||
|
// close(wsClient.dataChannel)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,25 +1,50 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"syscall"
|
||||||
|
|
||||||
"git.neurocipta.com/rogerferdinan/safe-web-socket/v1/client"
|
"git.neurocipta.com/rogerferdinan/safe-web-socket/v1/client"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
sigChan := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
|
||||||
|
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
<-sigChan
|
||||||
|
fmt.Println("\nReceived interrupt signal. Shutting down gracefully...")
|
||||||
|
cancel()
|
||||||
|
}()
|
||||||
|
|
||||||
wsClient, err := client.NewSafeWebsocketClientBuilder().
|
wsClient, err := client.NewSafeWebsocketClientBuilder().
|
||||||
BaseHost("localhost").
|
BaseHost("localhost").
|
||||||
BasePort(8080).
|
BasePort(8080).
|
||||||
Path("/ws/test/data_1").
|
Path("/ws/test/data_1").
|
||||||
UseTLS(false).
|
UseTLS(false).
|
||||||
Build()
|
ChannelSize(30).
|
||||||
|
Build(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
for range wsClient.ReconnectChannel() {
|
||||||
|
fmt.Println("Reconnection Success")
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
dataChannel := wsClient.DataChannel()
|
dataChannel := wsClient.DataChannel()
|
||||||
|
|
||||||
for data := range dataChannel {
|
for data := range dataChannel {
|
||||||
|
// _ = data
|
||||||
fmt.Println(string(data))
|
fmt.Println(string(data))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user