提交 12e9096c 编写于 作者: Huan (李卓桓)'s avatar Huan (李卓桓)

bugs fix for saveSession & login event & this binding for event handler etc

上级 ab04383c
......@@ -64,19 +64,19 @@ class Bridge {
const injectio = this.getInjectio()
let retObj = yield this.execute(injectio, this.port)
if (retObj && /^2/.test(retObj.code)) {
log.verbose('PuppetWebBridge', 'inject() injectio injected, with code[%d] message[%s] port[%d]'
if (retObj && /^2|3/.test(retObj.code)) { // HTTP Code 2XX & 3XX
log.verbose('PuppetWebBridge', 'inject() eval(Wechaty) return code[%d] message[%s] port[%d]'
, retObj.code, retObj.message, retObj.port)
} else {
throw new Error('execute injectio error: ' + retObj.message)
} else { // HTTP Code 4XX & 5XX
throw new Error('execute injectio error: ' + retObj.code + ', ' + retObj.message)
}
retObj = yield this.proxyWechaty('init')
if (retObj && /^2/.test(retObj.code)) {
log.verbose('PuppetWebBridge', 'inject() injectio inited, with code[%d] message[%s] port[%d]'
if (retObj && /^2|3/.test(retObj.code)) { // HTTP Code 2XX & 3XX
log.verbose('PuppetWebBridge', 'inject() Wechaty.init() return code[%d] message[%s] port[%d]'
, retObj.code, retObj.message, retObj.port)
} else {
throw new Error('execute proxyWechaty(init) error: ' + retObj.message)
} else { // HTTP Code 4XX & 5XX
throw new Error('execute proxyWechaty(init) error: ' + retObj.code + ', ' + retObj.message)
}
// const r = yield this.proxyWechaty('initClog')
......
......@@ -161,6 +161,7 @@ function onServerDisconnect(data) {
return
})
}
/**
* `unload` event is sent from js@browser to webserver via socketio
* after received `unload`, we should fix bridge by re-inject the Wechaty js code into browser.
......@@ -172,7 +173,7 @@ function onServerDisconnect(data) {
*/
function onServerUnload(data) {
log.warn('PuppetWebEvent', 'onServerUnload(%s)', typeof data)
// this.onServerLogout(data) // XXX: should emit event[logout] from browser
// onServerLogout.call(this, data) // XXX: should emit event[logout] from browser
if (!this.browser) {
log.warn('PuppetWebEvent', 'onServerUnload() found browser gone, should be quiting now')
......@@ -203,7 +204,7 @@ function onServerLogin(data) {
const userName = yield this.bridge.getUserName()
if (!userName) {
log.verbose('PuppetWebEvent', 'onServerLogin: browser not full loaded, retry later.')
setTimeout(this.onServerLogin.bind(this), 500)
setTimeout(onServerLogin.bind(this), 500)
return
}
log.verbose('PuppetWebEvent', 'bridge.getUserName: %s', userName)
......@@ -221,6 +222,7 @@ function onServerLogin(data) {
log.error('PuppetWebEvent', 'onServerLogin() exception: %s', e.message)
})
}
function onServerLogout(data) {
if (this.user) {
this.emit('logout', this.user)
......
......@@ -320,7 +320,8 @@ return (function(port) {
function checkScan() {
clog('checkScan()')
if (isLogin()) {
log('checkScan() - already login, no more check')
log('checkScan() - already login, no more check, but I will emit a login event')
login('checkScan found already login')
return
}
if (!Wechaty.glue.loginScope) {
......@@ -391,9 +392,10 @@ return (function(port) {
rootScope.$on('root:pageInit:success'), function (event, data) {
login('by event[root:pageInit:success]')
}
appScope.$on("newLoginPage", function(event, data) {
login('by event[newLoginPage]')
})
// newLoginPage seems not stand for a user login action
// appScope.$on("newLoginPage", function(event, data) {
// login('by event[newLoginPage]')
// })
window.addEventListener('unload', function(e) {
// XXX only 1 event can be emitted here???
Wechaty.emit('unload', e)
......
......@@ -187,6 +187,7 @@ class PuppetWeb extends Puppet {
log.silly('PuppetWeb', 'watchDog(%s)', data)
options = options || {}
const TIMEOUT = options.timeout || 60000 // 60s default. can be override in options
const EVENT = options.event || 'food' // just a name
if (this.watchDogTimer) {
clearTimeout(this.watchDogTimer)
......@@ -194,17 +195,17 @@ class PuppetWeb extends Puppet {
this.watchDogTimer = setTimeout(() => {
const err = new Error('watchdog timeout after ' + Math.floor(TIMEOUT/1000) + ' seconds')
// this.emit('error', err)
this.onBrowserDead(err)
Event.onBrowserDead.call(this, err)
}, TIMEOUT)
this.watchDogTimer.unref() // dont block quit
const SAVE_SESSION_INTERVAL = 5 * 60 * 1000 // 5 mins
if (this.session) {
if (!this.watchDogLastSaveSession || Date.now() - this.watchDogLastSaveSession > SAVE_SESSION_INTERVAL) {
if (this.watchDogLastSaveSession || Date.now() - this.watchDogLastSaveSession > SAVE_SESSION_INTERVAL) {
log.verbose('PuppetWeb', 'watchDog() saveSession(%s) after %d minutes', this.session, Math.floor(SAVE_SESSION_INTERVAL/1000/60))
this.browser.saveSession(this.session)
this.watchDogLastSaveSession = Date.now()
}
this.watchDogLastSaveSession = Date.now()
}
// if web browser stay at login qrcode page long time,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册