Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
.Veneno.
wechaty
提交
47d81b64
W
wechaty
项目概览
.Veneno.
/
wechaty
与 Fork 源项目一致
Fork自
wechaty / wechaty
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
W
wechaty
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
47d81b64
编写于
6月 15, 2016
作者:
Huan (李卓桓)
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix clog init exception and death loop when inject and init it
上级
ac70326b
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
199 addition
and
160 deletion
+199
-160
src/puppet-web-bridge.js
src/puppet-web-bridge.js
+41
-38
src/puppet-web-injectio.js
src/puppet-web-injectio.js
+158
-122
未找到文件。
src/puppet-web-bridge.js
浏览文件 @
47d81b64
...
...
@@ -60,6 +60,47 @@ class Bridge {
})
}
inject
()
{
log
.
verbose
(
'
PuppetWebBridge
'
,
'
inject()
'
)
return
co
.
call
(
this
,
function
*
()
{
const
injectio
=
this
.
getInjectio
()
let
retObj
=
yield
this
.
execute
(
injectio
,
this
.
options
.
port
)
if
(
retObj
&&
/^2/
.
test
(
retObj
.
code
))
{
log
.
verbose
(
'
PuppetWebBridge
'
,
'
inject() injectio injected, with code[%d] message[%s] port[%d]
'
,
retObj
.
code
,
retObj
.
message
,
retObj
.
port
)
}
else
{
throw
new
Error
(
'
execute injectio error:
'
+
retObj
.
message
)
}
retObj
=
yield
this
.
proxyWechaty
(
'
init
'
)
if
(
retObj
&&
/^2/
.
test
(
retObj
.
code
))
{
log
.
verbose
(
'
PuppetWebBridge
'
,
'
inject() injectio inited, with code[%d] message[%s] port[%d]
'
,
retObj
.
code
,
retObj
.
message
,
retObj
.
port
)
}
else
{
throw
new
Error
(
'
execute proxyWechaty(init) error:
'
+
retObj
.
message
)
}
// const r = yield this.proxyWechaty('initClog')
// log.warn('PuppetWebBridge', 'initClog(): %s', r)
return
true
})
.
catch
(
e
=>
{
log
.
warn
(
'
PuppetWebBridge
'
,
'
inject() exception: %s
'
,
e
.
message
)
throw
e
})
}
getInjectio
()
{
const
fs
=
require
(
'
fs
'
)
const
path
=
require
(
'
path
'
)
return
fs
.
readFileSync
(
path
.
join
(
path
.
dirname
(
__filename
),
'
puppet-web-injectio.js
'
)
,
'
utf8
'
)
}
logout
()
{
log
.
verbose
(
'
PuppetWebBridge
'
,
'
quit()
'
)
return
this
.
proxyWechaty
(
'
logout
'
)
...
...
@@ -143,44 +184,6 @@ class Bridge {
/////////////////////////////////
}
getInjectio
()
{
const
fs
=
require
(
'
fs
'
)
const
path
=
require
(
'
path
'
)
return
fs
.
readFileSync
(
path
.
join
(
path
.
dirname
(
__filename
),
'
puppet-web-injectio.js
'
)
,
'
utf8
'
)
}
inject
()
{
log
.
verbose
(
'
PuppetWebBridge
'
,
'
inject()
'
)
return
co
.
call
(
this
,
function
*
()
{
const
injectio
=
this
.
getInjectio
()
let
retObj
=
yield
this
.
execute
(
injectio
,
this
.
options
.
port
)
if
(
retObj
&&
/^2/
.
test
(
retObj
.
code
))
{
log
.
verbose
(
'
PuppetWebBridge
'
,
'
inject() injectio injected, with code[%d] message[%s] port[%d]
'
,
retObj
.
code
,
retObj
.
message
,
retObj
.
port
)
}
else
{
throw
new
Error
(
'
execute injectio error:
'
+
retObj
.
message
)
}
retObj
=
yield
this
.
proxyWechaty
(
'
init
'
)
if
(
retObj
&&
/^2/
.
test
(
retObj
.
code
))
{
log
.
verbose
(
'
PuppetWebBridge
'
,
'
inject() injectio inited, with code[%d] message[%s] port[%d]
'
,
retObj
.
code
,
retObj
.
message
,
retObj
.
port
)
}
else
{
throw
new
Error
(
'
execute proxyWechaty(init) error:
'
+
retObj
.
message
)
}
return
true
})
.
catch
(
e
=>
{
log
.
warn
(
'
PuppetWebBridge
'
,
'
inject() exception: %s
'
,
e
.
message
)
throw
e
})
}
/**
* Proxy Call to Wechaty in Bridge
*/
...
...
src/puppet-web-injectio.js
浏览文件 @
47d81b64
...
...
@@ -35,13 +35,13 @@ return (function(port) {
var
Wechaty
=
{
glue
:
{
// will be initialized by glueAngular() function
// will be initialized by glue
To
Angular() function
}
// glue funcs
,
getLoginStatusCode
:
function
()
{
return
Wechaty
.
glue
.
loginScope
.
code
}
,
getLoginQrImgUrl
:
function
()
{
return
Wechaty
.
glue
.
loginScope
.
qrcodeUrl
}
,
isReady
:
i
sReady
,
angularIsReady
:
angularI
sReady
// variable
,
vars
:
{
...
...
@@ -70,10 +70,7 @@ return (function(port) {
// test purpose
,
isLogin
:
isLogin
}
if
(
isWxLogin
())
{
login
(
'
page refresh
'
)
,
initClog
:
initClog
}
this
.
Wechaty
=
Wechaty
...
...
@@ -99,20 +96,13 @@ return (function(port) {
/////////////////////////////////////////////////////////////////////////////
/**
*
* Functions that Glued with AngularJS
*
*/
function
isWxLogin
()
{
return
!!
(
window
.
MMCgi
&&
window
.
MMCgi
.
isLogin
)
}
function
isReady
()
{
return
!!
(
(
typeof
angular
)
!==
'
undefined
'
&&
angular
.
element
&&
angular
.
element
(
'
body
'
)
)
}
function
init
()
{
if
(
!
initClog
())
{
// make console.log work (wxapp disabled the console.log)
retObj
.
code
=
501
retObj
.
message
=
'
initClog fail
'
return
retObj
}
if
(
Wechaty
.
vars
.
inited
===
true
)
{
log
(
'
Wechaty.init() called twice: already inited
'
)
retObj
.
code
=
201
...
...
@@ -120,7 +110,7 @@ return (function(port) {
return
retObj
}
if
(
!
i
sReady
())
{
if
(
!
angularI
sReady
())
{
clog
(
'
angular not ready. wait 500ms...
'
)
setTimeout
(
init
,
1000
)
retObj
.
code
=
202
...
...
@@ -128,14 +118,13 @@ return (function(port) {
return
retObj
}
if
(
!
initClog
())
{
// make console.log work (wxapp disabled the console.log
)
retObj
.
code
=
501
retObj
.
message
=
'
initClog fail
'
return
retObj
clog
(
'
init on port:
'
+
port
)
if
(
MMCgiLogined
())
{
login
(
'
page refresh
'
)
}
clog
(
'
init on port:
'
+
port
)
glueAngular
()
glueToAngular
()
connectSocket
()
hookEvents
()
...
...
@@ -151,6 +140,61 @@ return (function(port) {
return
retObj
}
/**
* Log to console
* http://stackoverflow.com/a/7089553/1123955
*/
function
initClog
()
{
if
(
Wechaty
.
vars
.
iframe
)
{
log
(
'
initClog() again? there is already a iframe
'
)
return
true
}
var
i
=
document
.
createElement
(
'
iframe
'
)
if
(
i
)
{
// slog('initClog got iframe element')
i
.
style
.
display
=
'
none
'
document
.
body
.
appendChild
(
i
)
Wechaty
.
vars
.
iframe
=
i
// if (!Wechaty.vars.iframe) {
// throw new Error('iframe gone after appendChild, WTF???')
// }
// slog('initClog done')
return
true
}
// slog('initClog got null iframe element')
return
false
}
function
clog
(
s
)
{
var
d
=
new
Date
()
s
=
d
.
getHours
()
+
'
:
'
+
d
.
getMinutes
()
+
'
:
'
+
d
.
getSeconds
()
+
'
<Wechaty>
'
+
s
if
(
Wechaty
.
vars
.
iframe
)
{
Wechaty
.
vars
.
iframe
.
contentWindow
.
console
.
log
(
s
)
}
else
{
throw
new
Error
(
'
clog() iframe not found when be invocked
'
)
}
}
function
slog
(
msg
)
{
Wechaty
.
emit
(
'
log
'
,
msg
)
}
function
log
(
s
)
{
clog
(
s
);
slog
(
s
)
}
/**
*
* Functions that Glued with AngularJS
*
*/
function
MMCgiLogined
()
{
return
!!
(
window
.
MMCgi
&&
window
.
MMCgi
.
isLogin
)
}
function
angularIsReady
()
{
return
!!
(
(
typeof
angular
)
!==
'
undefined
'
&&
angular
.
element
&&
angular
.
element
(
'
body
'
)
)
}
function
heartBeat
(
firstTime
)
{
var
TIMEOUT
=
15000
// 15s
if
(
firstTime
&&
Wechaty
.
vars
.
heartBeatTimmer
)
{
...
...
@@ -162,7 +206,7 @@ return (function(port) {
return
TIMEOUT
}
function
glueAngular
()
{
function
glue
To
Angular
()
{
var
injector
=
angular
.
element
(
document
).
injector
()
var
http
=
injector
.
get
(
'
$http
'
)
...
...
@@ -242,76 +286,45 @@ return (function(port) {
function
isLogin
()
{
return
!!
Wechaty
.
vars
.
logined
}
function
login
(
data
)
{
c
log
(
'
login(
'
+
data
+
'
)
'
)
log
(
'
login(
'
+
data
+
'
)
'
)
Wechaty
.
vars
.
logined
=
true
Wechaty
.
emit
(
'
login
'
,
data
)
}
function
logout
(
data
)
{
c
log
(
'
logout(
'
+
data
+
'
)
'
)
log
(
'
logout(
'
+
data
+
'
)
'
)
Wechaty
.
vars
.
logined
=
false
Wechaty
.
emit
(
'
logout
'
,
data
)
checkScan
()
}
function
quit
()
{
c
log
(
'
quit()
'
)
logout
(
'
quit
'
)
log
(
'
quit()
'
)
logout
(
'
quit
()
'
)
if
(
Wechaty
.
vars
.
socket
)
{
Wechaty
.
vars
.
socket
.
close
()
Wechaty
.
vars
.
socket
=
null
}
}
function
log
(
s
)
{
clog
(
s
);
slog
(
s
)
}
function
slog
(
msg
)
{
// keep this emit directly to use socket.emit instead of Wechaty.emit
// to prevent lost log msg if there has any bug in Wechaty.emit
if
(
!
Wechaty
.
vars
.
socket
)
{
clog
(
'
Wechaty.slog() not usable now coz no Wechaty.vars.socket. use clog instead
'
)
clog
(
msg
)
return
}
else
{
Wechaty
.
vars
.
socket
.
emit
(
'
log
'
,
msg
)
}
}
function
ding
()
{
log
(
'
recv ding
'
);
return
'
dong
'
}
function
send
(
ToUserName
,
Content
)
{
var
chat
=
Wechaty
.
glue
.
chatFactory
var
m
=
chat
.
createMessage
({
ToUserName
:
ToUserName
,
Content
:
Content
,
MsgType
:
Wechaty
.
glue
.
confFactory
.
MSGTYPE_TEXT
})
chat
.
appendMessage
(
m
)
return
chat
.
sendMessage
(
m
)
}
function
getContact
(
id
)
{
if
(
Wechaty
.
glue
.
contactFactory
)
{
var
c
=
Wechaty
.
glue
.
contactFactory
.
getContact
(
id
)
if
(
c
&&
c
.
isContact
)
{
c
.
stranger
=
!
(
c
.
isContact
())
}
return
c
}
log
(
'
contactFactory not inited
'
)
return
null
}
function
getUserName
()
{
return
Wechaty
.
glue
.
accountFactory
?
Wechaty
.
glue
.
accountFactory
.
getUserName
()
:
null
}
function
hookEvents
()
{
Wechaty
.
glue
.
rootScope
.
$on
(
'
message:add:success
'
,
function
(
event
,
data
)
{
var
rootScope
=
Wechaty
.
glue
.
rootScope
var
appScope
=
Wechaty
.
glue
.
appScope
if
(
!
rootScope
||
!
appScope
)
{
log
(
'
hookEvents() no rootScope
'
)
return
false
}
rootScope
.
$on
(
'
message:add:success
'
,
function
(
event
,
data
)
{
if
(
!
isLogin
())
{
// in case of we missed the pageInit event
login
(
'
by event[message:add:success]
'
)
}
Wechaty
.
emit
(
'
message
'
,
data
)
})
Wechaty
.
glue
.
appScope
.
$on
(
"
newLoginPage
"
,
function
(
event
,
data
)
{
login
(
'
by event[newLoginPage]
'
)
})
Wechaty
.
glue
.
rootScope
.
$on
(
'
root:pageInit:success
'
),
function
(
event
,
data
)
{
rootScope
.
$on
(
'
root:pageInit:success
'
),
function
(
event
,
data
)
{
login
(
'
by event[root:pageInit:success]
'
)
}
appScope
.
$on
(
"
newLoginPage
"
,
function
(
event
,
data
)
{
login
(
'
by event[newLoginPage]
'
)
})
window
.
addEventListener
(
'
unload
'
,
function
(
e
)
{
// XXX only 1 event can be emitted here???
Wechaty
.
emit
(
'
unload
'
,
e
)
...
...
@@ -320,41 +333,50 @@ return (function(port) {
// Wechaty.slog('emit logout')
// Wechaty.slog('emit logout&unload over')
})
return
true
}
// Wechaty.emit, will save event & data when there's no socket io connection to prevent event lost
/**
* Wechaty.emit, will save event & data when there's no socket io connection to prevent event lost
* NOTICE: only clog available here, because slog & log will call emit, death loop
*/
function
emit
(
event
,
data
)
{
var
eventsBuf
=
Wechaty
.
vars
.
eventsBuf
if
(
!
eventsBuf
.
map
)
{
throw
new
Error
(
'
Wechaty.vars.eventsBuf must be a Array
'
)
}
if
(
event
)
{
Wechaty
.
vars
.
eventsBuf
.
push
([
event
,
data
])
eventsBuf
.
push
([
event
,
data
])
}
if
(
!
Wechaty
.
vars
.
socket
)
{
var
socket
=
Wechaty
.
vars
.
socket
if
(
!
socket
)
{
clog
(
'
Wechaty.vars.socket not ready
'
)
return
setTimeout
(
emit
,
1000
)
// resent eventsBuf after 1000ms
}
var
bufLen
=
Wechaty
.
vars
.
eventsBuf
.
length
var
bufLen
=
eventsBuf
.
length
if
(
bufLen
)
{
if
(
bufLen
>
1
)
{
clog
(
'
Wechaty.vars.eventsBuf has
'
+
bufLen
+
'
unsend events
'
)
}
while
(
Wechaty
.
vars
.
eventsBuf
.
length
)
{
var
eventData
=
Wechaty
.
vars
.
eventsBuf
.
pop
()
while
(
eventsBuf
.
length
)
{
var
eventData
=
eventsBuf
.
pop
()
if
(
eventData
&&
eventData
.
map
&&
eventData
.
length
===
2
)
{
clog
(
'
emiting
'
+
eventData
[
0
])
Wechaty
.
vars
.
socket
.
emit
(
eventData
[
0
],
eventData
[
1
])
socket
.
emit
(
eventData
[
0
],
eventData
[
1
])
}
else
{
clog
(
'
Wechaty.emit() got invalid eventData:
'
+
eventData
[
0
]
+
'
,
'
+
eventData
[
1
]
+
'
, length:
'
+
eventData
.
length
)
}
}
if
(
bufLen
>
1
)
{
clog
(
'
Wechaty.vars.eventsBuf all sent
'
)
}
if
(
bufLen
>
1
)
{
clog
(
'
Wechaty.vars.eventsBuf
[
'
+
bufLen
+
'
]
all sent
'
)
}
}
}
function
connectSocket
()
{
c
log
(
'
connectSocket()
'
)
log
(
'
connectSocket()
'
)
if
(
typeof
io
!==
'
function
'
)
{
c
log
(
'
connectSocket: io not found. loading lib...
'
)
log
(
'
connectSocket: io not found. loading lib...
'
)
// http://stackoverflow.com/a/3248500/1123955
var
script
=
document
.
createElement
(
'
script
'
)
script
.
onload
=
function
()
{
c
log
(
'
socket io lib loaded.
'
)
setTimeout
(
connectSocket
,
50
)
log
(
'
socket io lib loaded.
'
)
connectSocket
(
)
}
script
.
src
=
'
https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.4.5/socket.io.min.js
'
document
.
getElementsByTagName
(
'
head
'
)[
0
].
appendChild
(
script
)
...
...
@@ -366,49 +388,63 @@ return (function(port) {
// ding -> dong. for test & live check purpose
// ping/pong are reserved by socket.io https://github.com/socketio/socket.io/issues/2414
socket
.
on
(
'
ding
'
,
function
(
e
)
{
clog
(
'
received socket io event: ding
. emit dong...
'
)
socket
.
emit
(
'
dong
'
,
'
dong
'
)
socket
.
on
(
'
ding
'
,
function
(
data
)
{
log
(
'
received socket io event: ding(
'
+
data
+
'
)
. emit dong...
'
)
socket
.
emit
(
'
dong
'
,
data
)
})
socket
.
on
(
'
connect
'
,
function
(
e
)
{
clog
(
'
connected to server:
'
+
e
)
})
socket
.
on
(
'
disconnect
'
,
function
(
e
)
{
clog
(
'
socket disconnect:
'
+
e
)
})
}
/**
* Log to console
* http://stackoverflow.com/a/7089553/1123955
*/
function
initClog
()
{
var
i
=
document
.
createElement
(
'
iframe
'
)
if
(
!
i
)
{
return
false
}
i
.
style
.
display
=
'
none
'
document
.
body
.
appendChild
(
i
)
Wechaty
.
vars
.
iframe
=
i
clog
(
'
initClog done
'
)
return
true
}
function
clog
(
s
)
{
var
d
=
new
Date
()
s
=
d
.
getHours
()
+
'
:
'
+
d
.
getMinutes
()
+
'
:
'
+
d
.
getSeconds
()
+
'
<Wechaty>
'
+
s
/**
* FIXME: WARN PuppetWebBridge inject() exception: {"errorMessage":"null is not an object (evaluating 'document.body.appendChild')"
* when will document.createElement('iframe') return null?
* this will cause the bridge init fail, and retry.
* should it be ignored? or keep this exception to retry is better?
*
* Help Functions which Proxy to WXAPP AngularJS Scope & Factory
*
*/
// var i = document.createElement('iframe')
// i.style.display = 'none'
// document.body.appendChild(i)
Wechaty
.
vars
.
iframe
.
contentWindow
.
console
.
log
(
s
)
// i.parentNode.removeChild(i)
}
function
getMsgImg
(
id
)
{
var
contentChatScope
=
Wechaty
.
glue
.
contentChatScope
if
(
!
contentChatScope
)
{
throw
new
Error
(
'
getMsgImg() contentChatScope not found
'
)
}
var
location
=
window
.
location
.
href
.
replace
(
/
\/
$/
,
''
)
var
path
=
Wechaty
.
glue
.
contentChatScope
.
getMsgImg
(
id
)
var
path
=
contentChatScope
.
getMsgImg
(
id
)
return
location
+
path
}
function
send
(
ToUserName
,
Content
)
{
var
chatFactory
=
Wechaty
.
glue
.
chatFactory
var
confFactory
=
Wechaty
.
glue
.
confFactory
if
(
!
chatFactory
||
!
confFactory
)
{
log
(
'
send() chatFactory or confFactory not exist.
'
)
return
false
}
var
m
=
chatFactory
.
createMessage
({
ToUserName
:
ToUserName
,
Content
:
Content
,
MsgType
:
confFactory
.
MSGTYPE_TEXT
})
chatFactory
.
appendMessage
(
m
)
return
chatFactory
.
sendMessage
(
m
)
}
function
getContact
(
id
)
{
var
contactFactory
=
Wechaty
.
glue
.
contactFactory
if
(
!
contactFactory
)
{
log
(
'
contactFactory not inited
'
)
return
null
}
var
c
=
contactFactory
.
getContact
(
id
)
if
(
c
&&
c
.
isContact
)
{
c
.
stranger
=
!
(
c
.
isContact
())
}
return
c
}
function
getUserName
()
{
var
accountFactory
=
Wechaty
.
glue
.
accountFactory
return
accountFactory
?
accountFactory
.
getUserName
()
:
null
}
}.
apply
(
window
,
arguments
))
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录