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

Enable Hostie for Wechaty

上级 8ccba353
......@@ -5,10 +5,8 @@
"main": "dist/src/index.js",
"typings": "dist/src/index.d.ts",
"wechaty": {
"DEFAULT_HEAD": 0,
"DEFAULT_PORT": 8080,
"DEFAULT_PROTOCOL": "io|0.0.1",
"DEFAULT_TOKEN": "WECHATY_IO_TOKEN",
"DEFAULT_APIHOST": "api.chatie.io"
},
"scripts": {
......@@ -52,6 +50,9 @@
},
"keywords": [
"wechat",
"rpa",
"conversational",
"ai",
"微信",
"weixin",
"personal",
......@@ -73,7 +74,7 @@
"bugs": {
"url": "https://github.com/wechaty/wechaty/issues"
},
"homepage": "https://chatie.io/wechaty/",
"homepage": "https://github.com/wechaty/",
"engines": {
"node": ">= 10"
},
......@@ -92,12 +93,12 @@
"pkg-dir": "^4.0.0",
"portfinder": "^1.0.17",
"promise-retry": "^1.1.1",
"qr-image": "^3.2.0",
"raven": "^2.6.2",
"read-pkg-up": "^7.0.0",
"state-switch": "^0.6.2",
"watchdog": "^0.8.1",
"wechaty-puppet": "^0.19.6",
"wechaty-puppet-hostie": "^0.3.8",
"ws": "^7.0.0"
},
"devDependencies": {
......@@ -117,7 +118,6 @@
"@types/node": "^12.12.3",
"@types/open-graph": "^0.2.0",
"@types/promise-retry": "^1.1.3",
"@types/qr-image": "^3.2.1",
"@types/raven": "^2.1.0",
"@types/request": "^2.47.1",
"@types/retry": "^0.12.0",
......
......@@ -22,6 +22,9 @@
*/
import { StateSwitch } from 'state-switch'
import { PuppetHostieServer } from 'wechaty-puppet-hostie'
import { PuppetHostieGrpcServerOptions } from 'wechaty-puppet-hostie/dist/src/grpc/puppet-server'
import { Message } from './user'
import {
......@@ -37,7 +40,12 @@ export interface IoClientOptions {
export class IoClient {
private io: Io // XXX keep io `null-able` or not? 20161026
/**
* Huan(20161026): keep io `null-able` or not?
* Huan(202002): make it optional.
*/
private io?: Io
private hostieServer?: PuppetHostieServer
private state: StateSwitch
......@@ -47,12 +55,33 @@ export class IoClient {
log.verbose('IoClient', 'constructor(%s)', JSON.stringify(options))
this.state = new StateSwitch('IoClient', log)
}
this.io = new Io({
token : this.options.token,
wechaty : this.options.wechaty,
})
private async startHostie () {
log.verbose('IoClient', 'startHostie()')
if (this.hostieServer) {
throw new Error('hostie server exists')
}
const options: PuppetHostieGrpcServerOptions = {
endpoint : '0.0.0.0:8788',
puppet : this.options.wechaty.puppet,
token : this.options.token,
}
this.hostieServer = new PuppetHostieServer(options)
await this.hostieServer.start()
}
private async stopHostie () {
log.verbose('IoClient', 'stopHostie()')
if (!this.hostieServer) {
throw new Error('hostie server does not exist')
}
await this.hostieServer.stop()
this.hostieServer = undefined
}
public async start (): Promise<void> {
......@@ -67,9 +96,10 @@ export class IoClient {
this.state.on('pending')
try {
await this.hookWechaty(this.options.wechaty)
await this.startIo()
await this.hookWechaty(this.options.wechaty)
await this.startHostie()
this.state.on(true)
......@@ -105,6 +135,15 @@ export class IoClient {
throw e
}
if (this.io) {
throw new Error('io exists')
}
this.io = new Io({
token : this.options.token,
wechaty : this.options.wechaty,
})
try {
await this.io.start()
} catch (e) {
......@@ -113,6 +152,18 @@ export class IoClient {
}
}
private async stopIo () {
log.verbose('IoClient', 'stopIo()')
if (!this.io) {
log.warn('IoClient', 'stopIo() io does not exist')
return
}
await this.io.stop()
this.io = undefined
}
private async onMessage (msg: Message) {
log.verbose('IoClient', 'onMessage(%s)', msg)
......@@ -138,14 +189,9 @@ export class IoClient {
this.state.off('pending')
// XXX
if (!this.io) {
log.warn('IoClient', 'stop() without this.io')
this.state.off(true)
return
}
await this.stopIo()
await this.stopHostie()
await this.io.stop()
this.state.off(true)
// XXX 20161026
......
......@@ -14,6 +14,7 @@ export const PUPPET_DEPENDENCIES = {
// 'wechaty-puppet-hostie' : '^0.0.1',
// 'wechaty-puppet-dll' : '^0.3.1', // https://www.npmjs.com/package/wechaty-puppet-dll
'wechaty-puppet-hostie' : '^0.3.8', // https://www.npmjs.com/package/wechaty-puppet-hostie
// 'wechaty-puppet-ioscat' : '^0.5.22', // https://www.npmjs.com/package/wechaty-puppet-ioscat
'wechaty-puppet-mock' : '^0.16.1', // https://www.npmjs.com/package/wechaty-puppet-mock
// 'wechaty-puppet-padchat' : '^0.19.3', // https://www.npmjs.com/package/wechaty-puppet-padchat
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册