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

make io-client more flexable

上级 7c0cde54
[![Wechaty](https://raw.githubusercontent.com/wechaty/wechaty/master/image/wechaty-logo-en.png)](https://github.com/wechaty/wechaty)
# Wechaty [![Linux Circle CI](https://circleci.com/gh/wechaty/wechaty.svg?style=svg)](https://circleci.com/gh/wechaty/wechaty) [![Linux/Mac Build Status](https://img.shields.io/travis/wechaty/wechaty.svg?label=Linux/Mac)](https://travis-ci.org/wechaty/wechaty) [![Win32 Build status](https://img.shields.io/appveyor/ci/zixia/wechaty/master.svg?label=Windows)](https://ci.appveyor.com/project/zixia/wechaty) [![Coverage Status](https://coveralls.io/repos/github/wechaty/wechaty/badge.svg?branch=master)](https://coveralls.io/github/wechaty/wechaty?branch=master)
Connecting ChatBots, Chat as a Service.
Connecting ChatBots.
Wechaty is a Bot Framework for Wechat **Personal** Account, It can help you easy creating personal wechat bot in 7 lines of javascript code, with cross platform support to [linux](https://travis-ci.org/wechaty/wechaty), [win32](https://ci.appveyor.com/project/zixia/wechaty) and [darwin(OSX/Mac)](https://travis-ci.org/wechaty/wechaty).
......
......@@ -23,7 +23,7 @@ const brolog = require('./brolog-env')
class IoClient {
constructor({
token = Config.token || Config.DEFAULT_TOKEN
initToken = Config.token || Config.DEFAULT_TOKEN
, endpoint = Config.endpoint
, log = brolog
} = {}) {
......@@ -39,15 +39,18 @@ class IoClient {
this.log.error('IoClient', e.message)
throw e
}
this.token = token
this.token(initToken)
}
init() {
this.log.verbose('IoClient', 'init()')
const wechaty = this.wechaty = new Wechaty({
profile: Config.DEFAULT_PROFILE
})
return co.call(this, function* () {
this.wechaty = yield this.initWechaty()
this.io = yield this.initIo(this.wechaty)
this.wechaty = yield this.initWechaty(this.wechaty)
return this
}).catch(e => {
this.log.error('IoClient', 'init() exception: %s', e.message)
......@@ -55,13 +58,9 @@ class IoClient {
})
}
initWechaty() {
initWechaty(wechaty) {
this.log.verbose('IoClient', 'initWechaty()')
const wechaty = new Wechaty({
profile: Config.DEFAULT_PROFILE
})
wechaty
.on('login' , user => this.log.info('IoClient', `${user.name()} logined`))
.on('logout' , user => this.log.info('IoClient', `${user.name()} logouted`))
......@@ -85,7 +84,7 @@ class IoClient {
}
initIo(wechaty) {
this.log.verbose('IoClient', 'initIo() with token %s', this.token)
this.log.verbose('IoClient', 'initIo() with token %s', this.token())
if (!wechaty) {
throw new Error('initIo() need a wechaty instance')
......@@ -93,7 +92,7 @@ class IoClient {
const io = new Io({
wechaty
, token: this.token
, token: this.token()
, endpoint: this.endpoint
})
......@@ -140,6 +139,67 @@ class IoClient {
.then(_ => this.log.info('Bot', 'REPLIED to magic word "wechaty"'))
}
}
start() {
this.log.verbose('IoClient', 'start()')
return this.initIo(this.wechaty)
.then(io => this.io = io)
}
stop() {
this.log.verbose('IoClient', 'stop()')
if (!this.io) {
this.log.warn('IoClient', 'stop() without this.io')
return Promise.resolve()
}
const p = this.io.quit()
this.io = null
return p
}
restart() {
this.log.verbose('IoClient', 'restart()')
return co.call(this, function* () {
yield this.stop()
yield this.start()
return this
}).catch(e => {
this.log.error('IoClient', 'restart() exception %s', e.message)
throw e
})
}
token(newToken) {
if (newToken) {
this.log.verbose('IoClient', 'token(%s)', newToken)
this._token = newToken
}
return this._token
}
quit() {
this.log.verbose('IoClient', 'quit()')
return co.call(this, function* () {
if (this.wechaty) {
yield this.wechaty.quit()
// this.wechaty = null
} else { this.log.warn('IoClient', 'quit() no this.wechaty') }
if (this.io) {
yield this.io.quit()
// this.io = null
} else { this.log.warn('IoClient', 'quit() no this.io') }
}).catch(e => {
this.log.error('IoClient', 'exception: %s', e.message)
throw e
})
}
}
module.exports = IoClient.default = IoClient.IoClient = IoClient
......@@ -36,6 +36,15 @@ class Io {
this.protocol = protocol + '|' + wechaty.uuid
log.verbose('Io', 'instantiated with endpoint[%s], token[%s], protocol[%s], uuid[%s]', endpoint, token, protocol, this.uuid)
this.purpose('offline')
}
purpose(newPurpose) {
if (newPurpose) {
this._purpose = newPurpose
}
return this._purpose
}
toString() { return 'Class Io(' + this.token + ')'}
......@@ -45,6 +54,8 @@ class Io {
init() {
log.verbose('Io', 'init()')
this.purpose('online')
return co.call(this, function* () {
yield this.initEventHook()
yield this.initWebSocket()
......@@ -174,10 +185,17 @@ class Io {
this.reconnect()
})
return Promise.resolve()
return Promise.resolve(ws)
}
reconnect() {
log.verbose('Io', 'reconnect()')
if (this.purpose() === 'offline') {
log.verbose('Io', 'reconnect() with purpose() === offline')
return
}
if (this.connected()) {
log.warn('Io', 'reconnect() on a already connected io')
return
......@@ -278,8 +296,20 @@ class Io {
close() {
this.ws.close()
// TODO: remove listener for this.wechaty.on(message )
return Promise.resolve()
}
quit() {
this.purpose('offline')
if (this.reconnectTimer) {
clearTimeout(this.reconnectTimer)
this.recnnectTimer = null
}
this.close()
return Promise.resolve()
}
/**
*
* Prepare to be overwriten by server setting
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册