Compare commits

..

No commits in common. "main" and "v1.1.1" have entirely different histories.
main ... v1.1.1

2 changed files with 1 additions and 26 deletions

View File

@ -1,7 +1,6 @@
package main
import (
"context"
"log"
"time"
@ -16,9 +15,6 @@ const (
func main() {
ws := websocket.NewWebSocketClient(
// ctx
context.Background(),
// onOpen
func(ws *websocket.WebSocketClient) {
log.Println("Connected")

View File

@ -25,12 +25,11 @@ type WebSocketClient struct {
}
func NewWebSocketClient(
ctx context.Context,
onOpen func(ws *WebSocketClient),
onClose func(ws *WebSocketClient),
onMessage func(ws *WebSocketClient, payload []byte),
) *WebSocketClient {
ctx, cancel := context.WithCancel(ctx)
ctx, cancel := context.WithCancel(context.Background())
return &WebSocketClient{
ctx: ctx,
cancel: cancel,
@ -85,7 +84,6 @@ func (ws *WebSocketClient) Connect(
var payload []byte
_, payload, err := ws.conn.Read(ws.ctx)
if err != nil {
ws.cancel()
if ws.onClose != nil {
ws.onClose(ws)
}
@ -96,21 +94,6 @@ func (ws *WebSocketClient) Connect(
}
}()
// Ping Sender
ws.wg.Add(1)
go func() {
defer ws.wg.Done()
pingTicker := time.NewTicker(time.Second * 10)
for {
select {
case <-ws.ctx.Done():
return
case <-pingTicker.C:
ws.conn.Ping(ws.ctx)
}
}
}()
return nil
}
@ -130,7 +113,3 @@ func (ws *WebSocketClient) Disconnect() {
func (ws *WebSocketClient) SendJSON(v any) error {
return wsjson.Write(ws.ctx, ws.conn, v)
}
func (ws *WebSocketClient) SendPing() error {
return ws.conn.Ping(ws.ctx)
}