From 12ba9c5ec9ca4d2e784e35fd360eaae0e127df22 Mon Sep 17 00:00:00 2001 From: Daniel Huber <30466471+daniel0611@users.noreply.github.com> Date: Sun, 28 Aug 2022 17:22:15 +0200 Subject: [PATCH] Fallback to 100% white when server is not reachable on startup --- go-client/main.go | 38 ++++++++++++++++++++++++-------------- go-client/ws.go | 8 ++------ 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/go-client/main.go b/go-client/main.go index 9949359..4242fcf 100644 --- a/go-client/main.go +++ b/go-client/main.go @@ -24,20 +24,30 @@ func main() { ctx, cancel := context.WithTimeout(context.Background(), time.Minute) defer cancel() - conn := connectWS(server, ctx) - defer conn.Close() - log.Println("Connected to websocket server.") - - wsChan := buildWsRecvChan(conn) - sigChan := make(chan os.Signal, 1) - signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM) - - for { - select { - case msg := <-wsChan: - handleWSMessage(msg, command) - case <-sigChan: - return + conn, err := connectWS(server, ctx) + + if err != nil { + log.Printf("Failed to connect to ws server at %s: %v\n", server, err) + log.Println("No server, defaulting to 100% white"); + + setColor(Color{255, 255, 255}, command) + // Do nothing else. + for {} + } else { + defer conn.Close() + log.Println("Connected to websocket server.") + + wsChan := buildWsRecvChan(conn) + sigChan := make(chan os.Signal, 1) + signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM) + + for { + select { + case msg := <-wsChan: + handleWSMessage(msg, command) + case <-sigChan: + return + } } } } diff --git a/go-client/ws.go b/go-client/ws.go index 717554d..d57a4f7 100644 --- a/go-client/ws.go +++ b/go-client/ws.go @@ -18,13 +18,9 @@ func (msg *wsMessage) StringData() string { return string(msg.Data) } -func connectWS(server string, ctx context.Context) *websocket.Conn { +func connectWS(server string, ctx context.Context) (*websocket.Conn, error) { conn, _, err := websocket.DefaultDialer.DialContext(ctx, server, nil) - if err != nil { - log.Fatalln("cannot connect to websocket server:", err) - } - - return conn + return conn, err } func buildWsRecvChan(conn *websocket.Conn) <-chan wsMessage {