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

enhance io support

上级 562f2eaf
...@@ -26,7 +26,7 @@ It supports [linux](https://travis-ci.org/zixia/wechaty), [win32](https://ci.app ...@@ -26,7 +26,7 @@ It supports [linux](https://travis-ci.org/zixia/wechaty), [win32](https://ci.app
Wechaty is dead easy to use: 9 lines javascript for your 1st wechat bot. Wechaty is dead easy to use: 9 lines javascript for your 1st wechat bot.
## 1. Basic: 9 lines ## 1. Basic: 9 lines
The following 9 lines of code implement a bot log every message to console: The following 9 lines of code implement a bot log all message to console:
```javascript ```javascript
const Wechaty = require('wechaty') const Wechaty = require('wechaty')
......
...@@ -14,6 +14,7 @@ const WebSocket = require('ws') ...@@ -14,6 +14,7 @@ const WebSocket = require('ws')
const co = require('co') const co = require('co')
const log = require('./npmlog-env') const log = require('./npmlog-env')
const Contact = require('./contact')
class Io { class Io {
...@@ -41,7 +42,7 @@ class Io { ...@@ -41,7 +42,7 @@ class Io {
log.verbose('Io', 'init()') log.verbose('Io', 'init()')
return co.call(this, function* () { return co.call(this, function* () {
yield this.initWechaty() yield this.initEventHook()
yield this.initWebSocket() yield this.initWebSocket()
return this return this
...@@ -52,6 +53,7 @@ class Io { ...@@ -52,6 +53,7 @@ class Io {
} }
initWebSocket() { initWebSocket() {
log.verbose('Io', 'initWebSocket()')
// const auth = 'Basic ' + new Buffer(this.token + ':X').toString('base64') // const auth = 'Basic ' + new Buffer(this.token + ':X').toString('base64')
const auth = 'Token ' + this.token const auth = 'Token ' + this.token
const headers = { 'Authorization': auth } const headers = { 'Authorization': auth }
...@@ -72,19 +74,61 @@ class Io { ...@@ -72,19 +74,61 @@ class Io {
}.bind(this)) }.bind(this))
ws.on('message', (data, flags) => { ws.on('message', (data, flags) => {
log.verbose('Io', 'WebSocket got message') log.silly('Io', 'initWebSocket() ws.on(message): %s', data)
// flags.binary will be set if a binary data is received. // flags.binary will be set if a binary data is received.
// flags.masked will be set if the data was masked. // flags.masked will be set if the data was masked.
log.verbose('Io', 'onMessage: %s', data)
if (data.onMessage) { const ioEvent = {
const script = data.script name: 'raw'
, payload: data
}
try {
const obj = JSON.parse(data)
ioEvent.name = obj.name
ioEvent.payload = obj.payload
} catch (e) {
log.verbose('Io', 'on(message) recv a non IoEvent data[%s]', data)
}
switch (ioEvent.name) {
case 'botie':
const payload = ioEvent.payload
if (payload.onMessage) {
const script = payload.script
const fn = eval(script) const fn = eval(script)
if (typeof fn === 'function') { if (typeof fn === 'function') {
this.onMessage = fn this.onMessage = fn
} else { } else {
log.warn('Io', 'onMessage server push function invalid') log.warn('Io', 'server pushed function is invalid')
}
} }
break
case 'reset':
log.verbose('Io', 'on(reset): %s', ioEvent.payload)
this.wechaty.reset()
break
case 'update':
log.verbose('Io', 'on(report): %s', ioEvent.payload)
const user = this.wechaty.user()
if (user) {
const loginEvent = {
name: 'login'
, payload: user.obj
}
this.send(loginEvent)
}
break
case 'sys':
// do nothing
break
default:
log.warn('Io', 'UNKNOWN on(%s): %s', ioEvent.name, ioEvent.payload)
break
} }
}) })
...@@ -124,22 +168,22 @@ class Io { ...@@ -124,22 +168,22 @@ class Io {
} }
} }
initWechaty() { initEventHook() {
const wechaty = this.wechaty const wechaty = this.wechaty
wechaty.on('message', this.ioMessage) wechaty.on('message', this.ioMessage)
const ioEvents = [ const hookEvents = [
'scan' 'scan'
, 'login' , 'login'
, 'logout' , 'logout'
, 'error' , 'error'
, 'heartbeat' , 'heartbeat'
] ]
ioEvents.map(event => { hookEvents.map(event => {
wechaty.on(event, data => { wechaty.on(event, data => {
if (!this.connected()) { if (!this.connected()) {
log.verbose('Io', 'initWechaty() on event[%s] without a connected websocket', event) log.verbose('Io', 'initEventHook() on event[%s] without a connected websocket', event)
return return
} }
const ioEvent = { const ioEvent = {
...@@ -147,16 +191,32 @@ class Io { ...@@ -147,16 +191,32 @@ class Io {
, payload: data , payload: data
} }
log.verbose('Io', 'initWechaty() on(event) send ioEvent[%s:%s]', event, data) switch (event) {
case 'login':
case 'logout':
if (data instanceof Contact) {
ioEvent.payload = data.obj
}
break
default:
break
}
this.send(ioEvent)
})
})
return Promise.resolve()
}
send(ioEvent) {
log.silly('Io', 'send(%s: %s)', ioEvent.name, ioEvent.payload)
this.ws.send( this.ws.send(
JSON.stringify( JSON.stringify(
ioEvent ioEvent
) )
) )
})
})
return Promise.resolve()
} }
close() { close() {
...@@ -178,12 +238,3 @@ class Io { ...@@ -178,12 +238,3 @@ class Io {
* Expose `Wechaty`. * Expose `Wechaty`.
*/ */
module.exports = Io.default = Io.Io = Io module.exports = Io.default = Io.Io = Io
/*
www.wechaty.io
www
api
test
*/
...@@ -43,6 +43,17 @@ class Wechaty extends EventEmitter { ...@@ -43,6 +43,17 @@ class Wechaty extends EventEmitter {
version() { return this.npmVersion } version() { return this.npmVersion }
user() { return this.puppet && this.puppet.user }
reset(reason) {
log.verbose('Wechaty', 'reset() because %s', reason)
if (this.puppet && this.puppet.browser) {
this.puppet.browser.dead('restart required by wechaty reset()')
} else {
log.warn('Wechaty', 'reset() without browser')
}
}
init() { init() {
log.info('Wechaty', 'v%s initializing...', this.npmVersion) log.info('Wechaty', 'v%s initializing...', this.npmVersion)
log.verbose('Wechaty', 'puppet: %s' , this.type) log.verbose('Wechaty', 'puppet: %s' , this.type)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册