Commit e36fdd5d by HanSon

增加正常退出和异常退出的handler

1 parent 1ffe804b
...@@ -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();
...@@ -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] 程序结束');
} }
/** /**
......
...@@ -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;
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!