Compare commits

..

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

2 changed files with 32 additions and 18 deletions

View File

@ -16,13 +16,21 @@ const (
func main() { func main() {
ws := websocket.NewWebSocketClient( ws := websocket.NewWebSocketClient(
// onOpen // onOpen
func(ws *websocket.WebSocketClient) { func(ws *websocket.WebSocketClient, isReconnecting bool) {
if isReconnecting {
log.Println("Reconnected")
} else {
log.Println("Connected") log.Println("Connected")
}
}, },
// onClose // onClose
func(ws *websocket.WebSocketClient) { func(ws *websocket.WebSocketClient, isReconnecting bool) {
if isReconnecting {
log.Println("Reconnecting...")
} else {
log.Println("Disconnected") log.Println("Disconnected")
}
}, },
// onMessage // onMessage
@ -32,7 +40,7 @@ func main() {
) )
// Connect to server // Connect to server
if err := ws.Connect(WEBSOCKET_URL, ATTEMPTS, INTERVAL); err != nil { if err := ws.Connect(WEBSOCKET_URL, ATTEMPTS, INTERVAL, false); err != nil {
log.Println("Failed to connect:", err) log.Println("Failed to connect:", err)
} }
@ -48,11 +56,11 @@ func main() {
time.Sleep(2 * time.Second) time.Sleep(2 * time.Second)
// Reconnecting // Reconnecting
ws.Connect(WEBSOCKET_URL, ATTEMPTS, INTERVAL) ws.Reconnect(WEBSOCKET_URL, ATTEMPTS, INTERVAL)
sendMessage() sendMessage()
time.Sleep(2 * time.Second) time.Sleep(2 * time.Second)
ws.Disconnect() ws.Disconnect(false)
log.Println("Done") log.Println("Done")
} }

View File

@ -17,14 +17,14 @@ type WebSocketClient struct {
wg sync.WaitGroup wg sync.WaitGroup
ctx context.Context ctx context.Context
cancel context.CancelFunc cancel context.CancelFunc
onOpen func(ws *WebSocketClient) onOpen func(ws *WebSocketClient, isReconnecting bool)
onClose func(ws *WebSocketClient) onClose func(ws *WebSocketClient, isReconnecting bool)
onMessage func(ws *WebSocketClient, payload []byte) onMessage func(ws *WebSocketClient, payload []byte)
} }
func NewWebSocketClient( func NewWebSocketClient(
onOpen func(ws *WebSocketClient), onOpen func(ws *WebSocketClient, isReconnecting bool),
onClose func(ws *WebSocketClient), onClose func(ws *WebSocketClient, isReconnecting bool),
onMessage func(ws *WebSocketClient, payload []byte), onMessage func(ws *WebSocketClient, payload []byte),
) *WebSocketClient { ) *WebSocketClient {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
@ -41,10 +41,8 @@ func (ws *WebSocketClient) Connect(
webSocketUrl string, webSocketUrl string,
attempts int, attempts int,
interval time.Duration, interval time.Duration,
isReconnecting bool,
) (err error) { ) (err error) {
ws.Disconnect()
ws.ctx, ws.cancel = context.WithCancel(context.Background())
ws.URL, err = url.Parse(webSocketUrl) ws.URL, err = url.Parse(webSocketUrl)
if err != nil { if err != nil {
return err return err
@ -64,7 +62,7 @@ func (ws *WebSocketClient) Connect(
} }
if ws.onOpen != nil { if ws.onOpen != nil {
ws.onOpen(ws) ws.onOpen(ws, isReconnecting)
} }
// Message Handler // Message Handler
@ -74,12 +72,14 @@ func (ws *WebSocketClient) Connect(
for { for {
select { select {
case <-ws.ctx.Done(): case <-ws.ctx.Done():
// log.Println("websocket receive cancel")
return return
default: default:
var payload []byte var payload []byte
if err := websocket.Message.Receive(ws.conn, &payload); err != nil { if err := websocket.Message.Receive(ws.conn, &payload); err != nil {
// log.Println("receive error", err)
if ws.onClose != nil { if ws.onClose != nil {
ws.onClose(ws) ws.onClose(ws, isReconnecting)
} }
return return
} }
@ -91,7 +91,7 @@ func (ws *WebSocketClient) Connect(
return nil return nil
} }
func (ws *WebSocketClient) Disconnect() { func (ws *WebSocketClient) Disconnect(isReconnecting bool) {
if ws.conn != nil { if ws.conn != nil {
ws.conn.Close() ws.conn.Close()
} }
@ -100,10 +100,16 @@ func (ws *WebSocketClient) Disconnect() {
ws.wg.Wait() ws.wg.Wait()
if ws.onClose != nil { if ws.onClose != nil {
ws.onClose(ws) ws.onClose(ws, isReconnecting)
} }
} }
func (ws *WebSocketClient) Reconnect(url string, attempts int, interval time.Duration) error {
ws.Disconnect(true)
ws.ctx, ws.cancel = context.WithCancel(context.Background())
return ws.Connect(url, attempts, interval, true)
}
func (ws *WebSocketClient) SendJSON(v any) error { func (ws *WebSocketClient) SendJSON(v any) error {
bytes, err := json.Marshal(v) bytes, err := json.Marshal(v)
if err != nil { if err != nil {