Skip to content

Commit

Permalink
Fallback to 100% white when server is not reachable on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
hlxid committed Aug 28, 2022
1 parent 2b683ab commit 12ba9c5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 deletions.
38 changes: 24 additions & 14 deletions go-client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
}
Expand Down
8 changes: 2 additions & 6 deletions go-client/ws.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 12ba9c5

Please sign in to comment.