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

support new events: login & logout

上级 5de04243
......@@ -39,3 +39,4 @@ t
socket.io.min.js
.*.swp
0
![Wechaty](https://raw.githubusercontent.com/zixia/wechaty/master/images/wechaty-logo-en.png)
# Wechaty [![Circle CI](https://circleci.com/gh/zixia/wechaty.svg?style=svg)](https://circleci.com/gh/zixia/wechaty) [![Build Status](https://travis-ci.org/zixia/wechaty.svg?branch=master)](https://travis-ci.org/zixia/wechaty)
Wechaty is Wechat for Bot.(Personal Account Robot, NOT Official Account)
Wechaty is a Bot-Enable Framework/Library for Personal Account of Wechat.
> Easy creating wechat robot code in 10 lines.
> Easy creating personal account wechat robot code in 10 lines.
**Connecting Bots**
[![Join the chat at https://gitter.im/zixia/wechaty](https://badges.gitter.im/zixia/wechaty.svg)](https://gitter.im/zixia/wechaty?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
[![node](https://img.shields.io/node/v/wechaty.svg?maxAge=2592000)](https://nodejs.org/)
......@@ -10,7 +12,7 @@ Wechaty is Wechat for Bot.(Personal Account Robot, NOT Official Account)
[![Downloads][downloads-image]][downloads-url]
# Why
My daily life/work depends on too much wechat.
My daily life/work depends on too much chat on wechat.
* I almost have 14,000 wechat friends till May 2014, before wechat restricts a total number of friends to 5,000.
* I almost have 400 wechat groups that most of them have more than 400 members.
......
此差异已折叠。
......@@ -63,5 +63,5 @@ function login() {
).catch(e => log.error('Bot', 'promise rejected'))
}
bot.on('login' , () => npm.info('Bot', 'logined'))
bot.on('logout' , () => npm.info('Bot', 'logouted'))
bot.on('login' , e => log.info('Bot', 'logined:' + JSON.stringify(e)))
bot.on('logout' , e => log.info('Bot', 'logouted:' + e))
......@@ -48,7 +48,7 @@
}
},
"engines": {
"node": ">= 6.1.0"
"node": ">= 6.0.0"
},
"devDependencies": {
"co": "^4.6.0",
......
......@@ -46,11 +46,12 @@ return (function(port) {
// glue funcs
, getLoginStatusCode: function() { return Wechaty.glue.loginScope.code }
, getLoginQrImgUrl: function() { return Wechaty.glue.loginScope.qrcodeUrl }
, isLogined: function() { return 200 === Wechaty.glue.loginScope.code /* MMCgi.isLogin ??? */}
, isLogin: function() { return !!(window.MMCgi && window.MMCgi.isLogin) }
, isReady: isReady
// variable
, socket: null
, socket: null
, eventsBuf: []
// funcs
, init: init
......@@ -59,6 +60,7 @@ return (function(port) {
, slog: slog // log throw Socket IO
, ding: ding
, quit: quit
, emit: emit
, getContact: getContact
}
......@@ -85,21 +87,29 @@ return (function(port) {
function glueAngular() {
var injector = angular.element(document).injector()
var rootScope = injector.get('$rootScope')
var http = injector.get('$http')
var http = injector.get('$http')
var accountFactory = injector.get('accountFactory')
var chatFactory = injector.get('chatFactory')
var confFactory = injector.get('confFactory')
var contactFactory = injector.get('contactFactory')
var loginScope = angular.element('.login_box').scope()
var rootScope = injector.get('$rootScope')
var appScope = angular.element('[ng-controller="appController"]').scope()
var loginScope = angular.element('[ng-controller="loginController"]').scope()
// get all we need from wx in browser(angularjs)
Wechaty.glue = {
injector: injector
, rootScope: rootScope
, http: http
, accountFactory: accountFactory
, chatFactory: chatFactory
, confFactory: confFactory
, contactFactory: contactFactory
, rootScope: rootScope
, appScope: appScope
, loginScope: loginScope
}
}
......@@ -111,35 +121,62 @@ return (function(port) {
}
clog('quit()')
}
function slog(data) { return Wechaty.socket && Wechaty.socket.emit('log', data) }
function slog(msg) { return Wechaty.socket && Wechaty.socket.emit('log', msg) }
function ding() { return 'dong' }
function send(ToUserName, Content) {
var chat = Wechaty.glue.chatFactory
var conf = Wechaty.glue.confFactory
var m = chat.createMessage({
ToUserName: ToUserName
, Content: Content
, MsgType: conf.MSGTYPE_TEXT
, MsgType: Wechaty.glue.confFactory.MSGTYPE_TEXT
})
chat.appendMessage(m)
return chat.sendMessage(m)
}
function getContact(id) { return Wechaty.glue.contactFactory.getContact(id) }
function hookMessage() {
var rootScope = Wechaty.glue.rootScope
rootScope.$on('message:add:success', function(event, data) {
if (Wechaty.socket) {
Wechaty.socket.emit('message', data)
} else {
clog('Wechaty.socket not ready')
}
Wechaty.glue.rootScope.$on('message:add:success', function(event, data) {
Wechaty.emit('message', data)
})
Wechaty.glue.appScope.$on("newLoginPage", function(event, data) {
Wechaty.emit('login', data)
})
Wechaty.glue.rootScope.$on('root:pageInit:success'), function (event, data) {
Wechaty.emit('login', data)
}
}
function hookUnload() {
window.addEventListener('unload', function(e) {
Wechaty.socket.emit('unload')
// XXX only 1 event can be emitted here???
Wechaty.emit('unload', e)
// Wechaty.slog('emit unload')
// Wechaty.emit('logout', e)
// Wechaty.slog('emit logout')
// Wechaty.slog('emit logout&unload over')
})
}
// Wechaty.emit, will save event & data when there's no socket io connection to prevent event lost
function emit(event, data) {
if (!Wechaty.socket) {
clog('Wechaty.socket not ready')
if (event) {
Wechaty.eventsBuf.push([event, data])
}
setTimeout(emit, 1000) // resent eventsBuf after 1000ms
return
}
if (Wechaty.eventsBuf.length) {
clog('Wechaty.eventsBuf has ' + Wechaty.eventsBuf.length + ' unsend events')
var eventData
while (eventData = Wechaty.eventsBuf.pop()) {
Wechaty.socket.emit(eventData[0], eventData[1])
}
clog('Wechaty.eventsBuf all sent')
}
if (event) {
Wechaty.socket.emit(event, data)
}
}
function connectSocket() {
clog('connectSocket()')
if (typeof io !== 'function') {
......@@ -178,6 +215,9 @@ return (function(port) {
window.Wechaty = Wechaty
if (Wechaty.isLogin()) {
Wechaty.emit('login', 'page refresh')
}
var callback = arguments[arguments.length - 1]
if (typeof callback === 'function') {
return callback('Wechaty')
......
......@@ -109,6 +109,7 @@ class Server extends EventEmitter {
'message'
, 'login'
, 'logout'
, 'log'
, 'unload'
].map(e => {
s.on(e, data => {
......
......@@ -61,13 +61,16 @@ class PuppetWeb extends Puppet {
* `unload` event is sent from js@browser to webserver via socketio
* after received `unload`, we re-inject the Wechaty js code into browser.
*/
this.server.on('unload', () => {
this.server.on('unload', data => {
log.verbose('PuppetWeb', 'server received unload event')
this.emit('logout', data)
this.browser.inject()
.then(() => log.verbose('PuppetWeb', 're-injected'))
.catch((e) => log.error('PuppetWeb', 'inject err: ' + e))
})
this.server.on('log', s => log.verbose('PuppetWeb', 'log event:' + s))
return this.server.init()
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册