fallback in case http or https was returned from API

This commit is contained in:
Pavel 2024-04-04 16:53:29 +03:00
parent 646caf2cab
commit c62788dcc0

View File

@ -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)