From c62788dcc0efbc5929a5902d438a092923263761 Mon Sep 17 00:00:00 2001 From: Neur0toxine Date: Thu, 4 Apr 2024 16:53:29 +0300 Subject: [PATCH] fallback in case http or https was returned from API --- websocket.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/websocket.go b/websocket.go index cdf3838..a39f405 100644 --- a/websocket.go +++ b/websocket.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "log" + "strings" "github.com/gorilla/websocket" v1 "github.com/retailcrm/mg-bot-api-client-go/v1" @@ -48,6 +49,15 @@ func (l *WebsocketListener) Listen() error { return fmt.Errorf("cannot get meta for connection: %w", err) } + if strings.HasPrefix(data, "http:") { + log.Println("warning: found http: in the URL - replacing with ws:") + data = "ws:" + data[5:] + } + if strings.HasPrefix(data, "https:") { + log.Println("warning: found https: in the URL - replacing with wss:") + data = "wss:" + data[6:] + } + ws, _, err := websocket.DefaultDialer.Dial(data, header) if err != nil { return fmt.Errorf("cannot estabilish WebSocket connection to %s: %w", data, err)