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

check more and hold token check before instanciating

上级 33b940cc
......@@ -64,7 +64,7 @@ export class Bridge extends PadchatRpc {
private loginTimer?: NodeJS.Timer
private selfId? : string
private selfName? : string
// private selfName? : string
// private password? : string
// private nickname? : string
......@@ -139,7 +139,16 @@ export class Bridge extends PadchatRpc {
this.cacheRoomRawPayload = new FlashStoreSync(path.join(baseDir, 'room-raw-payload'))
this.cacheRoomMemberRawPayload = new FlashStoreSync(path.join(baseDir, 'room-member-raw-payload'))
log.silly('PuppetPadchatBridge', 'initCache() workdir="%s"', baseDir)
await this.cacheContactRawPayload.ready()
await this.cacheRoomRawPayload.ready()
await this.cacheRoomMemberRawPayload.ready()
log.verbose('PuppetPadchatBridge', 'initCache() inited %d Contacts, %d Rooms, %d RoomMembers, cachedir="%s"',
this.cacheContactRawPayload.size,
this.cacheRoomRawPayload.size,
this.cacheRoomMemberRawPayload.size,
baseDir,
)
}
private releaseCache(): void {
......@@ -174,6 +183,17 @@ export class Bridge extends PadchatRpc {
// await this.padchatRpc.start()
await super.start()
this.on('padchat-logout', () => {
if (this.selfId) {
this.emit('logout')
} else {
/**
* PadchatRpc will receive 'logout' message multiple times when the server is logouted.
*/
log.warn('PuppetPadchatBridge', 'start() on(padchat-logout) received but selfId is undefined')
}
})
// this.padchatRpc.on('message', messageRawPayload => {
// log.silly('PuppetPadchatBridge', 'start() padchatRpc.on(message)')
// this.emit('message', messageRawPayload)
......@@ -218,9 +238,9 @@ export class Bridge extends PadchatRpc {
throw new Error('userId exist')
}
this.selfId = userId
if (userName) {
this.selfName = userName
}
// if (userName) {
// this.selfName = userName
// }
await this.stopCheckScan()
......@@ -492,7 +512,7 @@ export class Bridge extends PadchatRpc {
// const result = await this.padchatRpc.WXGetQRCode()
const result = await this.WXGetQRCode()
if (!result) {
if (!result || !result.qr_code) {
log.verbose('PuppetPadchatBridge', `emitLoginQrCode() result not found. Call WXInitialize() and try again ...`)
// await this.padchatRpc.WXInitialize()
await this.WXInitialize()
......
......@@ -4,22 +4,25 @@ import {
export const WECHATY_PUPPET_PADCHAT_ENDPOINT = process.env['WECHATY_PUPPET_PADCHAT_ENDPOINT'] || 'ws://54.223.36.77:9091/wx'
const WECHATY_PUPPET_PADCHAT_TOKEN = process.env['WECHATY_PUPPET_PADCHAT_TOKEN'] as string
if (!WECHATY_PUPPET_PADCHAT_TOKEN) {
log.error('PuppetPadchatConfig', `
function padchatToken() {
const token = process.env['WECHATY_PUPPET_PADCHAT_TOKEN'] as string
if (!token) {
log.error('PuppetPadchatConfig', `
WECHATY_PUPPET_PADCHAT_TOKEN environment variable not found.
WECHATY_PUPPET_PADCHAT_TOKEN environment variable not found.
PuppetPadchat need a token before it can be used,
Please set WECHATY_PUPPET_PADCHAT_TOKEN then retry again.
PuppetPadchat need a token before it can be used,
Please set WECHATY_PUPPET_PADCHAT_TOKEN then retry again.
Learn more about it at: https://github.com/Chatie/wechaty/issues/1296
Learn more about it at: https://github.com/Chatie/wechaty/issues/1296
`)
process.exit(1)
`)
throw new Error('set WECHATY_PUPPET_PADCHAT_TOKEN and try again')
}
return token
}
export {
WECHATY_PUPPET_PADCHAT_TOKEN,
padchatToken,
}
......@@ -205,7 +205,7 @@ export class PadchatRpc extends EventEmitter {
payload.type,
JSON.stringify(payload),
)
this.emit('logout', payload.msg)
this.emit('padchat-logout', payload.msg)
return
}
......@@ -259,11 +259,11 @@ export class PadchatRpc extends EventEmitter {
// )
// log.silly('PadchatRpc', 'onSocketPadchat(%s)', JSON.stringify(padchatPayload).substr(0, 500))
if (padchatPayload.type === PadchatPayloadType.Logout) {
// this.emit('logout', this.selfId())
console.log('onSocketPadchat: ', JSON.stringify(padchatPayload))
this.emit('logout')
}
// if (padchatPayload.type === PadchatPayloadType.Logout) {
// // this.emit('logout', this.selfId())
// console.log('onSocketPadchat: ', JSON.stringify(padchatPayload))
// this.emit('logout')
// }
let result: any
......
......@@ -53,7 +53,7 @@ import {
} from '../config'
import {
WECHATY_PUPPET_PADCHAT_TOKEN,
padchatToken,
WECHATY_PUPPET_PADCHAT_ENDPOINT,
} from './config'
......@@ -113,7 +113,7 @@ export class PuppetPadchat extends Puppet {
this.bridge = new Bridge({
memory : this.options.memory,
token : WECHATY_PUPPET_PADCHAT_TOKEN,
token : padchatToken(),
endpoint : WECHATY_PUPPET_PADCHAT_ENDPOINT,
})
}
......
......@@ -327,6 +327,8 @@ export class PadchatPureFunctionHelper {
}
public static async imageBase64ToQrcode(base64: string): Promise<string> {
// console.log('base64: ', typeof base64, String(base64).substr(0, 500))
const imageBuffer = Buffer.from(base64, 'base64')
const future = new Promise<string>((resolve, reject) => {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册