try reconnect socket
@@ -866,13 +866,16 @@ func (b *OGame) login() error {
b.Planets = extractPlanets(pageHTML, b)
go func(pageHTML string, b *OGame) {
+ for {
m := regexp.MustCompile(`var nodeUrl="https:\\/\\/([^:]+):(\d+)\\/socket.io\\/socket.io.js";`).FindStringSubmatch(pageHTML)
resp, err := http.Get("https://" + m[1] + ":" + m[2] + "/socket.io/1/?t=" + strconv.FormatInt(time.Now().Unix(), 10))
if err != nil {
fmt.Println("failed to get socket.io token:", err)
+ time.Sleep(time.Second)
+ continue
}
- defer resp.Body.Close()
by, _ := ioutil.ReadAll(resp.Body)
+ resp.Body.Close()
token := strings.Split(string(by), ":")[0]
origin := "https://" + m[1] + ":" + m[2] + "/"
@@ -889,6 +892,7 @@ func (b *OGame) login() error {
if _, err = ws.Read(buf); err != nil {
if err == io.EOF {
fmt.Println("chat eof:", err)
+ ws.Close()
break
} else {
fmt.Println("chat unexpected error", err)
@@ -918,6 +922,8 @@ func (b *OGame) login() error {
}
}
}
+ time.Sleep(time.Second)
+ }
}(pageHTML, b)
return nil
