mirror of
https://github.com/make-42/hayai.git
synced 2025-01-18 10:37:11 +01:00
feat: program won't crash unless something goes very very wrong
This commit is contained in:
parent
3dd8d135ff
commit
67827e958c
@ -19,6 +19,7 @@ type ConfigS struct {
|
||||
IssueNotification bool
|
||||
OpenWebPages bool
|
||||
TestWarning bool
|
||||
RetryConnectionEveryXS float64
|
||||
}
|
||||
|
||||
var Config ConfigS
|
||||
@ -34,6 +35,7 @@ var DefaultConfig = ConfigS{
|
||||
IssueNotification: true,
|
||||
OpenWebPages: false,
|
||||
TestWarning: false,
|
||||
RetryConnectionEveryXS: 30,
|
||||
}
|
||||
|
||||
func Init() {
|
||||
|
@ -9,7 +9,7 @@ import (
|
||||
|
||||
func CheckError(err error) {
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
log.Print(err)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -81,6 +81,8 @@ type JMAEEW struct {
|
||||
Pond string
|
||||
}
|
||||
|
||||
var LastRetry time.Time
|
||||
|
||||
func Listen() {
|
||||
// Init sound
|
||||
alertSound, err := alertSoundFile.Open("assets/alert-sat.wav")
|
||||
@ -91,12 +93,15 @@ func Listen() {
|
||||
defer streamer.Close()
|
||||
// Listen
|
||||
u := url.URL{Scheme: "wss", Host: constants.WSHost, Path: "/all_eew"}
|
||||
for {
|
||||
log.Printf("connecting to %s", u.String())
|
||||
|
||||
c, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
|
||||
if err != nil {
|
||||
log.Fatal("dial:", err)
|
||||
log.Print("dial:", err)
|
||||
time.Sleep(time.Duration(config.Config.RetryConnectionEveryXS) * time.Second)
|
||||
continue
|
||||
}
|
||||
|
||||
defer c.Close()
|
||||
|
||||
done := make(chan struct{})
|
||||
@ -107,6 +112,14 @@ func Listen() {
|
||||
for {
|
||||
_, message, err := c.ReadMessage()
|
||||
utils.CheckError(err)
|
||||
if message == nil {
|
||||
log.Printf("connection lost.")
|
||||
if time.Since(LastRetry) < time.Duration(config.Config.RetryConnectionEveryXS)*time.Second {
|
||||
time.Sleep(time.Duration(config.Config.RetryConnectionEveryXS) * time.Second)
|
||||
LastRetry = time.Now()
|
||||
}
|
||||
break
|
||||
}
|
||||
var typeMessage TypeMessage
|
||||
if config.Config.TestWarning {
|
||||
message = constants.TestMessage
|
||||
@ -151,5 +164,5 @@ func Listen() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user