Reconnect websocket on error

This commit is contained in:
Nise Void 2018-10-25 13:32:00 +02:00
parent bd9e9136e6
commit 2e7870df5d
Signed by: NiseVoid
GPG Key ID: FBA14AC83EA602F3
2 changed files with 11 additions and 13 deletions

View File

@ -2,13 +2,11 @@ package fortoprt
import ( import (
"encoding/json" "encoding/json"
"fmt"
"strconv" "strconv"
"time" "time"
"git.fuyu.moe/5GPowerQuality/parser/data" "git.fuyu.moe/5GPowerQuality/parser/data"
"git.fuyu.moe/5GPowerQuality/parser/shared" "git.fuyu.moe/5GPowerQuality/parser/shared"
"git.fuyu.moe/Fuyu/flog"
"golang.org/x/net/websocket" "golang.org/x/net/websocket"
) )
@ -32,8 +30,6 @@ type measurement struct {
var host = `dcem5g.ddns.net` var host = `dcem5g.ddns.net`
func FetchData() { func FetchData() {
fmt.Println(`Connecting`)
conn, err := websocket.Dial(`ws://`+host+`/ValuesSocketServer/VMvalues`, ``, `http://localhost`) conn, err := websocket.Dial(`ws://`+host+`/ValuesSocketServer/VMvalues`, ``, `http://localhost`)
if err != nil { if err != nil {
panic(err) panic(err)
@ -49,13 +45,6 @@ func FetchData() {
} }
func readData(conn *websocket.Conn) { func readData(conn *websocket.Conn) {
defer func() {
v := recover()
if v != nil {
flog.Critical(v)
}
}()
b, n := make([]byte, 16384), 0 b, n := make([]byte, 16384), 0
msg := message{} msg := message{}

13
main.go
View File

@ -17,8 +17,8 @@ func main() {
data.InitDB() data.InitDB()
// go fetchFortopData() go fetchFortopData()
go fortoprt.FetchData() go fetchFortopRealtimeData()
envitron.ReceiveData() envitron.ReceiveData()
} }
@ -37,3 +37,12 @@ func fetchFortopData() {
}() }()
} }
} }
func fetchFortopRealtimeData() {
for {
func() {
defer recoverFunc()
fortoprt.FetchData()
}()
}
}