83 lines
2.1 KiB
Go
83 lines
2.1 KiB
Go
package client
|
|
|
|
// import (
|
|
// "context"
|
|
// "fmt"
|
|
// "time"
|
|
|
|
// "git.neurocipta.com/rogerferdinan/safe-web-socket/internal"
|
|
// "github.com/gorilla/websocket"
|
|
// )
|
|
|
|
// const (
|
|
// pingPeriod = 30 * time.Second
|
|
// )
|
|
|
|
// type SafeWebsocketClientBuilder struct {
|
|
// baseHost *string `nil_checker:"required"`
|
|
// basePort *uint16 `nil_checker:"required"`
|
|
// }
|
|
|
|
// func NewSafeWebsocketClientBuilder() *SafeWebsocketClientBuilder {
|
|
// return &SafeWebsocketClientBuilder{}
|
|
// }
|
|
|
|
// func (b *SafeWebsocketClientBuilder) BaseHost(host string) *SafeWebsocketClientBuilder {
|
|
// b.baseHost = &host
|
|
// return b
|
|
// }
|
|
|
|
// func (b *SafeWebsocketClientBuilder) BasePort(port uint16) *SafeWebsocketClientBuilder {
|
|
// b.basePort = &port
|
|
// return b
|
|
// }
|
|
|
|
// func (b *SafeWebsocketClientBuilder) Build() (*SafeWebsocketClient, error) {
|
|
// if err := internal.NilChecker(b); err != nil {
|
|
// return nil, err
|
|
// }
|
|
|
|
// ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
// wsClient := SafeWebsocketClient{
|
|
// baseHost: b.baseHost,
|
|
// basePort: b.basePort,
|
|
// ctx: ctx,
|
|
// cancel: cancel,
|
|
// reconnectCh: make(chan struct{}, 1),
|
|
// isConnected: false,
|
|
// }
|
|
|
|
// if err := wsClient.connect(); err != nil {
|
|
// cancel()
|
|
// return nil, fmt.Errorf("failed to establish initial connection: %v", err)
|
|
// }
|
|
|
|
// wsClient.startPingTicker()
|
|
// wsClient.startReceiveHandler()
|
|
|
|
// return &wsClient, nil
|
|
// }
|
|
|
|
// type SafeWebsocketClient struct {
|
|
// baseHost *string
|
|
// basePort *uint16
|
|
// mu *internal.CustomRwMutex
|
|
// ctx context.Context
|
|
// cancel context.CancelFunc
|
|
// reconnectCh chan struct{}
|
|
// isConnected bool
|
|
// }
|
|
|
|
// func (wsClient *SafeWebsocketClient) connect() error {
|
|
// url := fmt.Sprintf("%s:%d", *wsClient.baseHost, *wsClient.basePort)
|
|
// conn, _, err := websocket.DefaultDialer.Dial(url, nil)
|
|
// if err != nil {
|
|
// return fmt.Errorf("failed to connect to %s: %w", *wsClient.baseHost, err)
|
|
// }
|
|
|
|
// conn.SetPingHandler(func(pingData string) error {
|
|
// conn.WriteMessage(websocket.PongMessage, []byte(pingData))
|
|
// })
|
|
// }
|