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 {