Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
Toggle navigation
This project
Loading...
Sign in
hfpp2012
/
vbot
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit e36fdd5d
authored
Jan 19, 2017
by
HanSon
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
增加正常退出和异常退出的handler
1 parent
1ffe804b
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
63 additions
and
10 deletions
example/index.php
src/Core/MessageHandler.php
src/Core/Server.php
example/index.php
View file @
e36fdd5
...
@@ -159,4 +159,12 @@ $robot->server->setMessageHandler(function ($message) use ($path) {
...
@@ -159,4 +159,12 @@ $robot->server->setMessageHandler(function ($message) use ($path) {
});
});
$robot
->
server
->
setExitHandler
(
function
(){
\Hanson\Vbot\Support\Console
::
log
(
'其他设备登录'
);
});
$robot
->
server
->
setExceptionHandler
(
function
(){
\Hanson\Vbot\Support\Console
::
log
(
'异常退出'
);
});
$robot
->
server
->
run
();
$robot
->
server
->
run
();
src/Core/MessageHandler.php
View file @
e36fdd5
...
@@ -28,6 +28,10 @@ class MessageHandler
...
@@ -28,6 +28,10 @@ class MessageHandler
private
$customHandler
;
private
$customHandler
;
private
$exitHandler
;
private
$exceptionHandler
;
private
$sync
;
private
$sync
;
private
$messageFactory
;
private
$messageFactory
;
...
@@ -76,13 +80,43 @@ class MessageHandler
...
@@ -76,13 +80,43 @@ class MessageHandler
public
function
setCustomHandler
(
Closure
$closure
)
public
function
setCustomHandler
(
Closure
$closure
)
{
{
if
(
!
$closure
instanceof
Closure
){
if
(
!
$closure
instanceof
Closure
){
throw
new
\Exception
(
'
message
handler must be a closure!'
);
throw
new
\Exception
(
'
custom
handler must be a closure!'
);
}
}
$this
->
customHandler
=
$closure
;
$this
->
customHandler
=
$closure
;
}
}
/**
/**
* 退出处理器
*
* @param Closure $closure
* @throws \Exception
*/
public
function
setExitHandler
(
Closure
$closure
)
{
if
(
!
$closure
instanceof
Closure
){
throw
new
\Exception
(
'exit handler must be a closure!'
);
}
$this
->
exitHandler
=
$closure
;
}
/**
* 异常处理器
*
* @param Closure $closure
* @throws \Exception
*/
public
function
setExceptionHandler
(
Closure
$closure
)
{
if
(
!
$closure
instanceof
Closure
){
throw
new
\Exception
(
'exit handler must be a closure!'
);
}
$this
->
exceptionHandler
=
$closure
;
}
/**
* 轮询消息API接口
* 轮询消息API接口
*/
*/
public
function
listen
()
public
function
listen
()
...
@@ -96,15 +130,24 @@ class MessageHandler
...
@@ -96,15 +130,24 @@ class MessageHandler
list
(
$retCode
,
$selector
)
=
$this
->
sync
->
checkSync
();
list
(
$retCode
,
$selector
)
=
$this
->
sync
->
checkSync
();
if
(
in_array
(
$retCode
,
[
'1100'
,
'1101'
])){
# 微信客户端上登出或者其他设备登录
if
(
in_array
(
$retCode
,
[
'1100'
,
'1101'
])){
# 微信客户端上登出或者其他设备登录
if
(
$this
->
exitHandler
){
Console
::
log
(
'[INFO] 微信客户端正常退出'
);
call_user_func_array
(
$this
->
exitHandler
,
[]);
}
break
;
break
;
}
elseif
(
$retCode
==
0
){
}
elseif
(
$retCode
==
0
){
$this
->
handlerMessage
(
$selector
);
$this
->
handlerMessage
(
$selector
);
}
else
{
}
else
{
$this
->
sync
->
debugMessage
(
$retCode
,
$selector
,
10
);
if
(
$this
->
exceptionHandler
){
Console
::
log
(
'[INFO] 微信客户端异常退出'
);
call_user_func_array
(
$this
->
exitHandler
,
[]);
}
break
;
}
}
$this
->
sync
->
checkTime
(
$time
);
$this
->
sync
->
checkTime
(
$time
);
}
}
Console
::
log
(
'[INFO] 程序结束'
);
}
}
/**
/**
...
...
src/Core/Server.php
View file @
e36fdd5
...
@@ -317,22 +317,24 @@ class Server
...
@@ -317,22 +317,24 @@ class Server
public
function
setMessageHandler
(
\Closure
$closure
)
public
function
setMessageHandler
(
\Closure
$closure
)
{
{
if
(
!
is_callable
(
$closure
)){
throw
new
\Exception
(
'[ERROR] message handler must be a closure!'
);
}
MessageHandler
::
getInstance
()
->
setMessageHandler
(
$closure
);
MessageHandler
::
getInstance
()
->
setMessageHandler
(
$closure
);
}
}
public
function
setCustomerHandler
(
\Closure
$closure
)
public
function
setCustomerHandler
(
\Closure
$closure
)
{
{
if
(
!
is_callable
(
$closure
)){
throw
new
\Exception
(
'[ERROR] message handler must be a closure!'
);
}
MessageHandler
::
getInstance
()
->
setCustomHandler
(
$closure
);
MessageHandler
::
getInstance
()
->
setCustomHandler
(
$closure
);
}
}
public
function
setExitHandler
(
\Closure
$closure
)
{
MessageHandler
::
getInstance
()
->
setExitHandler
(
$closure
);
}
public
function
setExceptionHandler
(
\Closure
$closure
)
{
MessageHandler
::
getInstance
()
->
setExceptionHandler
(
$closure
);
}
public
function
debug
(
$debug
=
true
)
public
function
debug
(
$debug
=
true
)
{
{
$this
->
debug
=
$debug
;
$this
->
debug
=
$debug
;
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment