update to 1.3.0 version.

This commit is contained in:
whale
2020-05-06 17:26:50 +08:00
parent 086c65af26
commit a1b013ee53
13 changed files with 321 additions and 74 deletions

View File

@@ -35,19 +35,20 @@ class MessageEvent
foreach (ZMBuf::$events[CQBefore::class]["message"] ?? [] as $v) {
$c = $v->class;
$class = new $c([
"data" => $this->data,
"data" => context()->getData(),
"connection" => $this->connection
], ModHandleType::CQ_MESSAGE);
Console::debug("Calling CQBefore: " . $c . " -> " . $v->method);
$r = call_user_func_array([$class, $v->method], []);
if (!$r || $class->block_continue) return false;
}
foreach (ZMBuf::get("wait_api", []) as $k => $v) {
if($this->data["user_id"] == $v["user_id"] &&
$this->data["self_id"] == $v["self_id"] &&
$this->data["message_type"] == $v["message_type"] &&
($this->data[$this->data["message_type"]."_id"] ?? $this->data["user_id"]) ==
($v[$v["message_type"]."_id"] ?? $v["user_id"])){
$v["result"] = $this->data["message"];
if (context()->getData()["user_id"] == $v["user_id"] &&
context()->getData()["self_id"] == $v["self_id"] &&
context()->getData()["message_type"] == $v["message_type"] &&
(context()->getData()[context()->getData()["message_type"] . "_id"] ?? context()->getData()["user_id"]) ==
($v[$v["message_type"] . "_id"] ?? $v["user_id"])) {
$v["result"] = context()->getData()["message"];
ZMBuf::appendKey("wait_api", $k, $v);
Co::resume($v["coroutine"]);
return false;
@@ -59,9 +60,9 @@ class MessageEvent
/** @noinspection PhpRedundantCatchClauseInspection */
public function onActivate() {
try {
$word = split_explode(" ", str_replace("\r", "", $this->data["message"]));
$word = split_explode(" ", str_replace("\r", "", context()->getMessage()));
if (count(explode("\n", $word[0])) >= 2) {
$enter = explode("\n", $this->data["message"]);
$enter = explode("\n", context()->getMessage());
$first = split_explode(" ", array_shift($enter));
$word = array_merge($first, $enter);
foreach ($word as $k => $v) {
@@ -77,15 +78,16 @@ class MessageEvent
$c = $v->class;
if (!isset($obj[$c]))
$obj[$c] = new $c([
"data" => $this->data,
"connection" => $this->connection
"data" => context()->getData(),
"connection" => context()->getConnection()
], ModHandleType::CQ_MESSAGE);
if ($word[0] != "" && $v->match == $word[0]) {
Console::debug("Calling $c -> {$v->method}");
$r = call_user_func([$obj[$c], $v->method], $word);
if (is_string($r)) $obj[$c]->reply($r);
$this->function_call = true;
return;
} elseif (($args = matchArgs($v->regexMatch, $this->data["message"])) !== false) {
} elseif ($v->regexMatch != "" && ($args = matchArgs($v->regexMatch, context()->getMessage())) !== false) {
$r = call_user_func([$obj[$c], $v->method], $args);
if (is_string($r)) $obj[$c]->reply($r);
$this->function_call = true;
@@ -96,21 +98,21 @@ class MessageEvent
foreach (ZMBuf::$events[CQMessage::class] ?? [] as $v) {
/** @var CQMessage $v */
if (
($v->message == '' || ($v->message != '' && $v->message == $this->data["message"])) &&
($v->user_id == 0 || ($v->user_id != 0 && $v->user_id == $this->data["user_id"])) &&
($v->group_id == 0 || ($v->group_id != 0 && $v->group_id == ($this->data["group_id"] ?? 0))) &&
($v->discuss_id == 0 || ($v->discuss_id != 0 && $v->discuss_id == ($this->data["discuss_id"] ?? 0))) &&
($v->message_type == '' || ($v->message_type != '' && $v->message_type == $this->data["message_type"])) &&
($v->raw_message == '' || ($v->raw_message != '' && $v->raw_message == $this->data["raw_message"]))) {
($v->message == '' || ($v->message != '' && $v->message == context()->getData()["message"])) &&
($v->user_id == 0 || ($v->user_id != 0 && $v->user_id == context()->getData()["user_id"])) &&
($v->group_id == 0 || ($v->group_id != 0 && $v->group_id == (context()->getData()["group_id"] ?? 0))) &&
($v->discuss_id == 0 || ($v->discuss_id != 0 && $v->discuss_id == (context()->getData()["discuss_id"] ?? 0))) &&
($v->message_type == '' || ($v->message_type != '' && $v->message_type == context()->getData()["message_type"])) &&
($v->raw_message == '' || ($v->raw_message != '' && $v->raw_message == context()->getData()["raw_message"]))) {
$c = $v->class;
if (!isset($obj[$c]))
$obj[$c] = new $c([
"data" => $this->data,
"data" => context()->getData(),
"connection" => $this->connection
], ModHandleType::CQ_MESSAGE);
$r = call_user_func([$obj[$c], $v->method], $this->data["message"]);
$r = call_user_func([$obj[$c], $v->method], context()->getData()["message"]);
if (is_string($r)) $obj[$c]->reply($r);
if ($obj[$c]->block_continue) return;
if (context()->getCache("block_continue") === true) return;
}
}
} catch (WaitTimeoutException $e) {
@@ -126,7 +128,7 @@ class MessageEvent
foreach (ZMBuf::$events[CQAfter::class]["message"] ?? [] as $v) {
$c = $v->class;
$class = new $c([
"data" => $this->data,
"data" => context()->getData(),
"connection" => $this->connection
], ModHandleType::CQ_MESSAGE);
$r = call_user_func_array([$class, $v->method], []);
@@ -138,4 +140,4 @@ class MessageEvent
public function hasReply() {
return $this->function_call;
}
}
}

View File

@@ -58,11 +58,11 @@ class MetaEvent
], ModHandleType::CQ_META_EVENT);
$r = call_user_func([$obj[$c], $v->method]);
if (is_string($r)) $obj[$c]->reply($r);
if ($obj[$c]->block_continue) return;
if (context()->getCache("block_continue") === true) return;
}
}
} catch (WaitTimeoutException $e) {
$e->module->finalReply($e->getMessage());
}
}
}
}

View File

@@ -59,7 +59,7 @@ class NoticeEvent
], ModHandleType::CQ_NOTICE);
$r = call_user_func([$obj[$c], $v->method]);
if (is_string($r)) $obj[$c]->reply($r);
if ($obj[$c]->block_continue) return;
if (context()->getCache("block_continue") === true) return;
}
}
} /** @noinspection PhpRedundantCatchClauseInspection */ catch (WaitTimeoutException $e) {
@@ -79,4 +79,4 @@ class NoticeEvent
}
return true;
}
}
}

View File

@@ -60,7 +60,7 @@ class RequestEvent
], ModHandleType::CQ_REQUEST);
$r = call_user_func([$obj[$c], $v->method]);
if (is_string($r)) $obj[$c]->reply($r);
if ($obj[$c]->block_continue) return;
if (context()->getCache("block_continue") === true) return;
}
}
} catch (WaitTimeoutException $e) {
@@ -80,4 +80,4 @@ class RequestEvent
}
return true;
}
}
}

View File

@@ -10,6 +10,9 @@ use Exception;
use Framework\Console;
use Framework\ZMBuf;
use ZM\Event\Swoole\{MessageEvent, RequestEvent, WorkerStartEvent, WSCloseEvent, WSOpenEvent};
use Swoole\Server;
use Swoole\WebSocket\Frame;
use ZM\Connection\ConnectionManager;
use ZM\Http\Response;
use Framework\DataProvider;
use ZM\Utils\ZMUtil;
@@ -18,6 +21,7 @@ class EventHandler
{
public static function callSwooleEvent($event_name, $param0, $param1 = null) {
//$starttime = microtime(true);
unset(ZMBuf::$context[Co::getCid()]);
$event_name = strtolower($event_name);
switch ($event_name) {
case "workerstart":
@@ -43,10 +47,15 @@ class EventHandler
}
break;
case "message":
/** @var Frame $param1 */
/** @var Server $param0 */
$conn = ConnectionManager::get($param1->fd);
set_coroutine_params(["server" => $param0, "frame" => $param1, "connection" => $conn]);
(new MessageEvent($param0, $param1))->onActivate()->onAfter();
break;
case "request":
try {
set_coroutine_params(["request" => $param0, "response" => $param1]);
(new RequestEvent($param0, $param1))->onActivate()->onAfter();
} catch (Exception $e) {
/** @var Response $param1 */
@@ -60,9 +69,11 @@ class EventHandler
}
break;
case "open":
set_coroutine_params(["server" => $param0, "request" => $param1]);
(new WSOpenEvent($param0, $param1))->onActivate()->onAfter();
break;
case "close":
set_coroutine_params(["server" => $param0, "fd" => $param1]);
(new WSCloseEvent($param0, $param1))->onActivate()->onAfter();
break;
}

View File

@@ -39,14 +39,14 @@ class MessageEvent implements SwooleEvent
*/
public function onActivate() {
ZMUtil::checkWait();
$conn = ConnectionManager::get($this->frame->fd);
set_coroutine_params(["server" => $this->server, "frame" => $this->frame, "connection" => $conn]);
$conn = ConnectionManager::get(context()->getFrame()->fd);
try {
if ($conn->getType() == "qq") {
$data = json_decode($this->frame->data, true);
$data = json_decode(context()->getFrame()->data, true);
if (isset($data["post_type"])) {
set_coroutine_params(["data" => $data, "connection" => $conn]);
EventHandler::callCQEvent($data, ConnectionManager::get($this->frame->fd), 0);
Console::debug("Calling CQ Event from fd=" . $conn->fd);
EventHandler::callCQEvent($data, ConnectionManager::get(context()->getFrame()->fd), 0);
} else
EventHandler::callCQResponse($data);
}
@@ -56,7 +56,7 @@ class MessageEvent implements SwooleEvent
/** @var ModBase $class */
$class = new $c(["server" => $this->server, "frame" => $this->frame, "connection" => $conn], ModHandleType::SWOOLE_MESSAGE);
call_user_func_array([$class, $v->method], [$conn]);
if ($class->block_continue) break;
if (context()->getCache("block_continue") === true) break;
}
}
} catch (Exception $e) {
@@ -76,7 +76,7 @@ class MessageEvent implements SwooleEvent
/** @var ModBase $class */
$class = new $c(["server" => $this->server, "frame" => $this->frame, "connection" => $conn], ModHandleType::SWOOLE_MESSAGE);
call_user_func_array([$class, $v->method], []);
if ($class->block_continue) break;
if (context()->getCache("block_continue") === true) break;
}
}
return $this;

View File

@@ -120,7 +120,7 @@ class RequestEvent implements SwooleEvent
return $this;
}
set_coroutine_params(["request" => $this->request, "response" => $this->response, "params" => $params]);
context()->setCache("params", $params);
if (in_array(strtoupper($this->request->server["request_method"]), $node["request_method"] ?? [])) { //判断目标方法在不在里面
$c_name = $node["class"];
@@ -171,7 +171,7 @@ class RequestEvent implements SwooleEvent
$c = $v->class;
$class = new $c(["request" => $this->request, "response" => $this->response]);
$r = call_user_func_array([$class, $v->method], []);
if ($class->block_continue) break;
if (context()->getCache("block_continue") === true) break;
}
}

View File

@@ -52,7 +52,7 @@ class WSCloseEvent implements SwooleEvent
/** @var ModBase $class */
$class = new $c(["server" => $this->server, "fd" => $this->fd], ModHandleType::SWOOLE_CLOSE);
call_user_func_array([$class, $v->method], []);
if($class->block_continue) break;
if(context()->getCache("block_continue") === true) break;
}
}
return $this;

View File

@@ -61,7 +61,7 @@ class WSOpenEvent implements SwooleEvent
$c = $v->class;
$class = new $c(["server" => $this->server, "request" => $this->request, "connection" => $this->conn], ModHandleType::SWOOLE_OPEN);
call_user_func_array([$class, $v->method], [$this->conn]);
if ($class->block_continue) break;
if (context()->getCache("block_continue") === true) break;
}
}
return $this;
@@ -77,7 +77,7 @@ class WSOpenEvent implements SwooleEvent
/** @var ModBase $class */
$class = new $v["class"](["server" => $this->server, "request" => $this->request, "connection" => $this->conn], ModHandleType::SWOOLE_OPEN);
call_user_func_array([$class, $v["method"]], [$this->conn]);
if ($class->block_continue) break;
if (context()->getCache("block_continue") === true) break;
}
}
return $this;

View File

@@ -109,7 +109,7 @@ class WorkerStartEvent implements SwooleEvent
/** @var ModBase $class */
$class = new $class_name(["server" => $this->server, "worker_id" => $this->worker_id], ModHandleType::SWOOLE_WORKER_START);
call_user_func_array([$class, $v->method], []);
if ($class->block_continue) break;
if (context()->getCache("block_continue") === true) break;
}
}
return $this;