Skip to content

Instantly share code, notes, and snippets.

@suntong
Last active December 23, 2017 07:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save suntong/384761bd58f7e5920d32cdb8fff2bf78 to your computer and use it in GitHub Desktop.
Save suntong/384761bd58f7e5920d32cdb8fff2bf78 to your computer and use it in GitHub Desktop.
////////////////////////////////////////////////////////////////////////////
// Porgram: wx-go.go
// Purpose: weixin robot
// Authors: Tong Sun (c) 2017, All rights reserved
////////////////////////////////////////////////////////////////////////////
package main
import (
//"fmt"
"time"
"github.com/songtianyi/rrframework/logs"
"github.com/songtianyi/wechat-go/wxweb"
"github.com/suntong/wx-go/plugins/gamer24"
)
func main() {
// create session
session, err := wxweb.CreateSession(nil, nil, wxweb.TERMINAL_MODE)
if err != nil {
logs.Error(err)
return
}
// load plugins for this session
gamer24.Register(session)
d0, _ := time.ParseDuration("3s")
for { // until logged in
err := session.LoginAndServe(false)
if err == nil {
break
}
logs.Error("session exit, %s", err)
}
OuterLoop:
for { // endless loop
for i, d := 0, d0; i < 16; i, d = i+1, d*2 {
//logs.Info(fmt.Sprintf("trying re-login with cache (%d)", i))
logs.Info("trying re-login with cache (%d)", i)
if err := session.LoginAndServe(true); err != nil {
logs.Error("re-login (%d) error, %s", i, err)
time.Sleep(d)
} else {
logs.Info("re-login with cache succeeded.")
continue OuterLoop
}
}
if session, err = wxweb.CreateSession(nil, session.HandlerRegister, wxweb.TERMINAL_MODE); err != nil {
logs.Error("create new sesion failed, %s", err)
break
} else {
logs.Info("recreate new session succeeded.")
}
}
}
@songtianyi
Copy link

use

time.Sleep(d * time.Second)

instead of d

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment