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

clean io code

上级 1578ace2
......@@ -67,9 +67,12 @@ export class IoClient {
this.state.on('pending')
try {
await this.initIo()
await this.startIo()
await this.hookWechaty(this.options.wechaty)
this.state.on(true)
} catch (e) {
log.error('IoClient', 'init() exception: %s', e.message)
this.state.off(true)
......@@ -96,11 +99,11 @@ export class IoClient {
return
}
private async initIo (): Promise<void> {
log.verbose('IoClient', 'initIo() with token %s', this.options.token)
private async startIo (): Promise<void> {
log.verbose('IoClient', 'startIo() with token %s', this.options.token)
if (this.state.off()) {
const e = new Error('initIo() state.off() is true, skipped')
const e = new Error('startIo() state.off() is true, skipped')
log.warn('IoClient', e.message)
throw e
}
......@@ -108,7 +111,7 @@ export class IoClient {
try {
await this.io.start()
} catch (e) {
log.verbose('IoClient', 'initIo() init fail: %s', e.message)
log.verbose('IoClient', 'startIo() init fail: %s', e.message)
throw e
}
......
......@@ -75,10 +75,10 @@ interface IoEventAny {
type IoEvent = IoEventScan | IoEventAny
export class Io {
private readonly cuid : string
private readonly id : string
private readonly protocol : string
private eventBuffer : IoEvent[] = []
private ws : undefined | WebSocket
private eventBuffer : IoEvent[] = []
private ws : undefined | WebSocket
private readonly state = new StateSwitch('Io', log)
......@@ -97,14 +97,14 @@ export class Io {
options.apihost = options.apihost || config.apihost
options.protocol = options.protocol || config.default.DEFAULT_PROTOCOL
this.cuid = options.wechaty.id
this.id = options.wechaty.id
this.protocol = options.protocol + '|' + options.wechaty.id
log.verbose('Io', 'instantiated with apihost[%s], token[%s], protocol[%s], cuid[%s]',
options.apihost,
options.token,
options.protocol,
this.cuid,
this.id,
)
}
......@@ -161,7 +161,7 @@ export class Io {
const wechaty = this.options.wechaty
wechaty.on('error' , error => this.send({ name: 'error', payload: error }))
wechaty.on('heartbeat', data => this.send({ name: 'heartbeat', payload: { cuid: this.cuid, data } }))
wechaty.on('heartbeat', data => this.send({ name: 'heartbeat', payload: { cuid: this.id, data } }))
wechaty.on('login', user => this.send({ name: 'login', payload: user }))
wechaty.on('logout' , user => this.send({ name: 'logout', payload: user }))
wechaty.on('message', message => this.ioMessage(message))
......@@ -170,56 +170,6 @@ export class Io {
// wechaty.on('scan', (url, code) => this.send({ name: 'scan', payload: { url, code } }))
wechaty.on('scan', (qrcode, status) => this.send({ name: 'scan', payload: { qrcode, status } } as IoEventScan ))
// const hookEvents: WechatyEventName[] = [
// 'scan'
// , 'login'
// , 'logout'
// , 'heartbeat'
// , 'error'
// ]
// hookEvents.map(event => {
// wechaty.on(event, (data) => {
// const ioEvent: IoEvent = {
// name: event
// , payload: data
// }
// switch (event) {
// case 'login':
// case 'logout':
// if (data instanceof Contact) {
// // ioEvent.payload = data.obj
// ioEvent.payload = data
// }
// break
// case 'error':
// ioEvent.payload = data.toString()
// break
// case 'heartbeat':
// ioEvent.payload = {
// cuid: this.cuid
// , data: data
// }
// break
// default:
// break
// }
// this.send(ioEvent)
// })
// })
// wechaty.on('message', m => {
// const text = (m.room() ? '[' + m.room().topic() + ']' : '')
// + '<' + m.from().name() + '>'
// + ':' + m.toStringDigest()
// this.send({ name: 'message', payload: text })
// })
return
}
......@@ -273,7 +223,7 @@ export class Io {
this.reconnectTimeout = undefined
const name = 'sys'
const payload = 'Wechaty version ' + this.options.wechaty.version() + ` with CUID: ${this.cuid}`
const payload = 'Wechaty version ' + this.options.wechaty.version() + ` with CUID: ${this.id}`
const initEvent: IoEvent = {
name,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册