Last active
December 23, 2017 07:26
-
-
Save suntong/384761bd58f7e5920d32cdb8fff2bf78 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//////////////////////////////////////////////////////////////////////////// | |
// 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.") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
use
instead of d