Files
zhamao-framework/src/ZM/Event/Swoole/WSCloseEvent.php

70 lines
2.1 KiB
PHP
Raw Normal View History

2020-03-02 16:14:20 +08:00
<?php
namespace ZM\Event\Swoole;
2020-06-13 16:19:11 +08:00
use Closure;
2020-05-08 16:37:38 +08:00
use Doctrine\Common\Annotations\AnnotationException;
2020-03-02 16:14:20 +08:00
use Framework\ZMBuf;
use Swoole\Server;
use ZM\Annotation\Swoole\SwooleEventAfter;
use ZM\Annotation\Swoole\SwooleEventAt;
2020-03-20 14:50:12 +08:00
use ZM\Connection\ConnectionManager;
2020-05-08 16:37:38 +08:00
use ZM\Event\EventHandler;
2020-03-02 16:14:20 +08:00
use ZM\Utils\ZMUtil;
class WSCloseEvent implements SwooleEvent
{
public $server;
public $fd;
public function __construct(Server $server, int $fd) {
$this->server = $server;
$this->fd = $fd;
}
/**
* @inheritDoc
2020-05-08 16:37:38 +08:00
* @throws AnnotationException
2020-03-02 16:14:20 +08:00
*/
public function onActivate() {
ZMUtil::checkWait();
2020-06-13 16:19:11 +08:00
set_coroutine_params(["server" => $this->server, "fd" => $this->fd, "connection" => ConnectionManager::get($this->fd)]);
2020-03-02 16:14:20 +08:00
foreach(ZMBuf::$events[SwooleEventAt::class] ?? [] as $v) {
if(strtolower($v->type) == "close" && $this->parseSwooleRule($v)) {
$c = $v->class;
2020-05-08 16:37:38 +08:00
EventHandler::callWithMiddleware($c, $v->method, ["server" => $this->server, "fd" => $this->fd], []);
if(context()->getCache("block_continue") === true) break;
2020-03-02 16:14:20 +08:00
}
}
ConnectionManager::close($this->fd);
2020-03-02 16:14:20 +08:00
return $this;
}
/**
* @inheritDoc
2020-05-08 16:37:38 +08:00
* @throws AnnotationException
2020-03-02 16:14:20 +08:00
*/
public function onAfter() {
foreach (ZMBuf::$events[SwooleEventAfter::class] ?? [] as $v) {
if (strtolower($v->type) == "close" && $this->parseSwooleRule($v) === true) {
$c = $v->class;
2020-05-08 16:37:38 +08:00
EventHandler::callWithMiddleware($c, $v->method, ["server" => $this->server, "fd" => $this->fd], []);
2020-05-06 17:26:50 +08:00
if(context()->getCache("block_continue") === true) break;
2020-03-02 16:14:20 +08:00
}
}
return $this;
}
private function parseSwooleRule($v) {
2020-06-13 16:19:11 +08:00
switch (explode(":", $v->rule)[0]) {
case "connectType": //websocket连接类型
if ($v->callback instanceof Closure) return call_user_func($v->callback, ConnectionManager::get($this->fd));
break;
}
2020-03-02 16:14:20 +08:00
return true;
}
}