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
|
IssueNotification bool
|
||||||
OpenWebPages bool
|
OpenWebPages bool
|
||||||
TestWarning bool
|
TestWarning bool
|
||||||
|
RetryConnectionEveryXS float64
|
||||||
}
|
}
|
||||||
|
|
||||||
var Config ConfigS
|
var Config ConfigS
|
||||||
@ -34,6 +35,7 @@ var DefaultConfig = ConfigS{
|
|||||||
IssueNotification: true,
|
IssueNotification: true,
|
||||||
OpenWebPages: false,
|
OpenWebPages: false,
|
||||||
TestWarning: false,
|
TestWarning: false,
|
||||||
|
RetryConnectionEveryXS: 30,
|
||||||
}
|
}
|
||||||
|
|
||||||
func Init() {
|
func Init() {
|
||||||
|
@ -9,7 +9,7 @@ import (
|
|||||||
|
|
||||||
func CheckError(err error) {
|
func CheckError(err error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Print(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -81,6 +81,8 @@ type JMAEEW struct {
|
|||||||
Pond string
|
Pond string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var LastRetry time.Time
|
||||||
|
|
||||||
func Listen() {
|
func Listen() {
|
||||||
// Init sound
|
// Init sound
|
||||||
alertSound, err := alertSoundFile.Open("assets/alert-sat.wav")
|
alertSound, err := alertSoundFile.Open("assets/alert-sat.wav")
|
||||||
@ -91,12 +93,15 @@ func Listen() {
|
|||||||
defer streamer.Close()
|
defer streamer.Close()
|
||||||
// Listen
|
// Listen
|
||||||
u := url.URL{Scheme: "wss", Host: constants.WSHost, Path: "/all_eew"}
|
u := url.URL{Scheme: "wss", Host: constants.WSHost, Path: "/all_eew"}
|
||||||
|
for {
|
||||||
log.Printf("connecting to %s", u.String())
|
log.Printf("connecting to %s", u.String())
|
||||||
|
|
||||||
c, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
|
c, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
|
||||||
if err != 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()
|
defer c.Close()
|
||||||
|
|
||||||
done := make(chan struct{})
|
done := make(chan struct{})
|
||||||
@ -107,6 +112,14 @@ func Listen() {
|
|||||||
for {
|
for {
|
||||||
_, message, err := c.ReadMessage()
|
_, message, err := c.ReadMessage()
|
||||||
utils.CheckError(err)
|
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
|
var typeMessage TypeMessage
|
||||||
if config.Config.TestWarning {
|
if config.Config.TestWarning {
|
||||||
message = constants.TestMessage
|
message = constants.TestMessage
|
||||||
@ -151,5 +164,5 @@ func Listen() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user