mirror of
https://github.com/zhamao-robot/zhamao-framework.git
synced 2026-07-21 15:45:36 +08:00
update to 1.3.0 version.
This commit is contained in:
@@ -11,6 +11,7 @@ use ZM\Annotation\Http\RequestMapping;
|
||||
use ZM\Annotation\Swoole\SwooleEventAt;
|
||||
use ZM\Connection\CQConnection;
|
||||
use ZM\ModBase;
|
||||
use ZM\Utils\ZMUtil;
|
||||
|
||||
/**
|
||||
* Class Hello
|
||||
@@ -36,6 +37,30 @@ class Hello extends ModBase
|
||||
return "你好啊,我是由炸毛框架构建的机器人!";
|
||||
}
|
||||
|
||||
/**
|
||||
* @CQCommand(".reload")
|
||||
*/
|
||||
public function reload() {
|
||||
context()->reply("reloading...");
|
||||
ZMUtil::reload();
|
||||
}
|
||||
|
||||
/**
|
||||
* @CQCommand("随机数")
|
||||
* @CQCommand(regexMatch="*从*到*的随机数")
|
||||
* @param $arg
|
||||
*/
|
||||
public function randNum($arg) {
|
||||
// 获取第一个数字类型的参数
|
||||
$num1 = context()->getArgs($arg, ZM_MATCH_NUMBER, "请输入第一个数字");
|
||||
// 获取第二个数字类型的参数
|
||||
$num2 = context()->getArgs($arg, ZM_MATCH_NUMBER, "请输入第二个数字");
|
||||
$a = min(intval($num1), intval($num2));
|
||||
$b = max(intval($num1), intval($num2));
|
||||
// 回复用户结果
|
||||
context()->reply("随机数是:".mt_rand($a, $b));
|
||||
}
|
||||
|
||||
/**
|
||||
* 中间件测试的一个示例函数
|
||||
* @RequestMapping("/httpTimer")
|
||||
@@ -45,12 +70,21 @@ class Hello extends ModBase
|
||||
return "This page is used as testing TimerMiddleware! Do not use it in production.";
|
||||
}
|
||||
|
||||
/**
|
||||
* 默认示例页面
|
||||
* @RequestMapping("/index")
|
||||
*/
|
||||
public function index() {
|
||||
return "Hello Zhamao!";
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 框架会默认关闭未知的WebSocket链接,因为这个绑定的事件,你可以根据你自己的需求进行修改
|
||||
* @SwooleEventAt(type="open",rule="connectType:unknown")
|
||||
*/
|
||||
public function closeUnknownConn() {
|
||||
Console::info("Unknown connection , I will close it.");
|
||||
$this->connection->close();
|
||||
context()->getConnection()->close();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,73 +4,62 @@
|
||||
namespace ZM\Context;
|
||||
|
||||
|
||||
use Co;
|
||||
use Framework\ZMBuf;
|
||||
use Swoole\Http\Request;
|
||||
use Swoole\WebSocket\Frame;
|
||||
use swoole_server;
|
||||
use ZM\API\CQAPI;
|
||||
use ZM\Connection\ConnectionManager;
|
||||
use ZM\Connection\CQConnection;
|
||||
use ZM\Connection\WSConnection;
|
||||
use ZM\Exception\InvalidArgumentException;
|
||||
use ZM\Exception\WaitTimeoutException;
|
||||
use ZM\Http\Response;
|
||||
use ZM\Utils\ZMRobot;
|
||||
|
||||
class Context implements ContextInterface
|
||||
{
|
||||
private $server = null;
|
||||
private $frame = null;
|
||||
private $data = null;
|
||||
private $request = null;
|
||||
private $response = null;
|
||||
private $cid;
|
||||
|
||||
public function __construct($param, $cid) {
|
||||
if (isset($param["server"])) $this->server = $param["server"];
|
||||
if (isset($param["frame"])) $this->frame = $param["frame"];
|
||||
if (isset($param["data"])) $this->data = $param["data"];
|
||||
if (isset($param["request"])) $this->request = $param["request"];
|
||||
if (isset($param["response"])) $this->response = $param["response"];
|
||||
$this->cid = $cid;
|
||||
}
|
||||
public function __construct($cid) { $this->cid = $cid; }
|
||||
|
||||
/**
|
||||
* @return swoole_server|null
|
||||
*/
|
||||
public function getServer() {
|
||||
return $this->server;
|
||||
}
|
||||
public function getServer() { return ZMBuf::$context[$this->cid]["server"] ?? null; }
|
||||
|
||||
/**
|
||||
* @return Frame|null
|
||||
*/
|
||||
public function getFrame() {
|
||||
return $this->frame;
|
||||
}
|
||||
public function getFrame() { return ZMBuf::$context[$this->cid]["frame"] ?? null; }
|
||||
|
||||
public function getFd() { return ZMBuf::$context[$this->cid]["fd"] ?? $this->getFrame()->fd ?? null; }
|
||||
|
||||
/**
|
||||
* @return array|null
|
||||
*/
|
||||
public function getData() {
|
||||
return $this->data;
|
||||
}
|
||||
public function getData() { return ZMBuf::$context[$this->cid]["data"] ?? null; }
|
||||
|
||||
public function setData($data) { ZMBuf::$context[$this->cid]["data"] = $data; }
|
||||
|
||||
/**
|
||||
* @return Request|null
|
||||
*/
|
||||
public function getRequest() {
|
||||
return $this->request;
|
||||
}
|
||||
public function getRequest() { return ZMBuf::$context[$this->cid]["request"] ?? null; }
|
||||
|
||||
/**
|
||||
* @return Response|null
|
||||
*/
|
||||
public function getResponse() {
|
||||
return $this->response;
|
||||
}
|
||||
public function getResponse() { return ZMBuf::$context[$this->cid]["response"] ?? null; }
|
||||
|
||||
/** @return WSConnection */
|
||||
public function getConnection() { return ConnectionManager::get($this->getFrame()->fd); }
|
||||
|
||||
/**
|
||||
* @return int|null
|
||||
*/
|
||||
public function getCid() {
|
||||
return $this->cid;
|
||||
}
|
||||
public function getCid() { return $this->cid; }
|
||||
|
||||
/**
|
||||
* @return ZMRobot|null
|
||||
@@ -79,4 +68,136 @@ class Context implements ContextInterface
|
||||
$conn = ConnectionManager::get($this->getFrame()->fd);
|
||||
return $conn instanceof CQConnection ? new ZMRobot($conn) : null;
|
||||
}
|
||||
|
||||
public function getMessage() { return ZMBuf::$context[$this->cid]["data"]["message"] ?? null; }
|
||||
|
||||
public function setMessage($msg) { ZMBuf::$context[$this->cid]["data"]["message"] = $msg; }
|
||||
|
||||
public function getUserId() { return $this->getData()["user_id"] ?? null; }
|
||||
|
||||
public function setUserId($id) { ZMBuf::$context[$this->cid]["data"]["user_id"] = $id; }
|
||||
|
||||
public function getGroupId() { return $this->getData()["group_id"] ?? null; }
|
||||
|
||||
public function setGroupId($id) { ZMBuf::$context[$this->cid]["data"]["group_id"] = $id; }
|
||||
|
||||
public function getDiscussId() { return $this->getData()["discuss_id"] ?? null; }
|
||||
|
||||
public function setDiscussId($id) { ZMBuf::$context[$this->cid]["data"]["discuss_id"] = $id; }
|
||||
|
||||
public function getMessageType() { return $this->getData()["message_type"] ?? null; }
|
||||
|
||||
public function setMessageType($type) { ZMBuf::$context[$this->cid]["data"]["message_type"] = $type; }
|
||||
|
||||
public function getRobotId() { return $this->getData()["self_id"] ?? null; }
|
||||
|
||||
public function getCache($key) { return ZMBuf::$context[$this->cid]["cache"][$key] ?? null; }
|
||||
|
||||
public function setCache($key, $value) { ZMBuf::$context[$this->cid]["cache"][$key] = $value; }
|
||||
|
||||
/**
|
||||
* only can used by cq->message event function
|
||||
* @param $msg
|
||||
* @param bool $yield
|
||||
* @return mixed
|
||||
*/
|
||||
public function reply($msg, $yield = false) {
|
||||
switch ($this->getData()["message_type"]) {
|
||||
case "group":
|
||||
case "private":
|
||||
case "discuss":
|
||||
return CQAPI::quick_reply(ConnectionManager::get($this->getFrame()->fd), $this->getData(), $msg, $yield);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function finalReply($msg, $yield = false) {
|
||||
ZMBuf::$context[$this->cid]["block_continue"] = true;
|
||||
if ($msg == "") return true;
|
||||
return $this->reply($msg, $yield);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $prompt
|
||||
* @param int $timeout
|
||||
* @param string $timeout_prompt
|
||||
* @return string
|
||||
* @throws InvalidArgumentException
|
||||
* @throws WaitTimeoutException
|
||||
*/
|
||||
public function waitMessage($prompt = "", $timeout = 600, $timeout_prompt = "") {
|
||||
if ($prompt != "") $this->reply($prompt);
|
||||
if (!isset($this->getData()["user_id"], $this->getData()["message"], $this->getData()["self_id"]))
|
||||
throw new InvalidArgumentException("协程等待参数缺失");
|
||||
$cid = Co::getuid();
|
||||
$api_id = ZMBuf::$atomics["wait_msg_id"]->get();
|
||||
ZMBuf::$atomics["wait_msg_id"]->add(1);
|
||||
$hang = [
|
||||
"coroutine" => $cid,
|
||||
"user_id" => $this->getData()["user_id"],
|
||||
"message" => $this->getData()["message"],
|
||||
"self_id" => $this->getData()["self_id"],
|
||||
"message_type" => $this->getData()["message_type"],
|
||||
"result" => null
|
||||
];
|
||||
if ($hang["message_type"] == "group" || $hang["message_type"] == "discuss") {
|
||||
$hang[$hang["message_type"] . "_id"] = $this->getData()[$this->getData()["message_type"] . "_id"];
|
||||
}
|
||||
ZMBuf::appendKey("wait_api", $api_id, $hang);
|
||||
$id = swoole_timer_after($timeout * 1000, function () use ($api_id, $timeout_prompt) {
|
||||
$r = ZMBuf::get("wait_api")[$api_id] ?? null;
|
||||
if ($r !== null) {
|
||||
Co::resume($r["coroutine"]);
|
||||
}
|
||||
});
|
||||
|
||||
Co::suspend();
|
||||
$sess = ZMBuf::get("wait_api")[$api_id];
|
||||
ZMBuf::unsetByValue("wait_api", $api_id);
|
||||
$result = $sess["result"];
|
||||
if (isset($id)) swoole_timer_clear($id);
|
||||
if ($result === null) throw new WaitTimeoutException($this, $timeout_prompt);
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $arg
|
||||
* @param $mode
|
||||
* @param $prompt_msg
|
||||
* @return mixed|string
|
||||
* @throws InvalidArgumentException
|
||||
* @throws WaitTimeoutException
|
||||
*/
|
||||
public function getArgs(&$arg, $mode, $prompt_msg) {
|
||||
switch ($mode) {
|
||||
case ZM_MATCH_ALL:
|
||||
$p = $arg;
|
||||
array_shift($p);
|
||||
return trim(implode(" ", $p)) == "" ? $this->waitMessage($prompt_msg) : trim(implode(" ", $p));
|
||||
case ZM_MATCH_NUMBER:
|
||||
foreach ($arg as $k => $v) {
|
||||
if (is_numeric($v)) {
|
||||
array_splice($arg, $k, 1);
|
||||
return $v;
|
||||
}
|
||||
}
|
||||
return $this->waitMessage($prompt_msg);
|
||||
case ZM_MATCH_FIRST:
|
||||
if (isset($arg[1])) {
|
||||
$a = $arg[1];
|
||||
array_splice($arg, 1, 1);
|
||||
return $a;
|
||||
} else {
|
||||
return $this->waitMessage($prompt_msg);
|
||||
}
|
||||
}
|
||||
throw new InvalidArgumentException();
|
||||
}
|
||||
|
||||
public function cloneFromParent() {
|
||||
set_coroutine_params(ZMBuf::$context[Co::getPcid()] ?? ZMBuf::$context[$this->cid]);
|
||||
return context();
|
||||
}
|
||||
|
||||
public function copy() { return ZMBuf::$context[$this->cid]; }
|
||||
}
|
||||
|
||||
@@ -7,12 +7,13 @@ namespace ZM\Context;
|
||||
use Swoole\Http\Request;
|
||||
use Swoole\WebSocket\Frame;
|
||||
use Swoole\WebSocket\Server;
|
||||
use ZM\Connection\WSConnection;
|
||||
use ZM\Http\Response;
|
||||
use ZM\Utils\ZMRobot;
|
||||
|
||||
interface ContextInterface
|
||||
{
|
||||
public function __construct($param, $cid);
|
||||
public function __construct($cid);
|
||||
|
||||
/** @return Server */
|
||||
public function getServer();
|
||||
@@ -23,6 +24,14 @@ interface ContextInterface
|
||||
/** @return mixed */
|
||||
public function getData();
|
||||
|
||||
public function setData($data);
|
||||
|
||||
/** @return WSConnection */
|
||||
public function getConnection();
|
||||
|
||||
/** @return int|null */
|
||||
public function getFd();
|
||||
|
||||
/** @return int */
|
||||
public function getCid();
|
||||
|
||||
@@ -34,4 +43,74 @@ interface ContextInterface
|
||||
|
||||
/** @return ZMRobot */
|
||||
public function getRobot();
|
||||
|
||||
/** @return mixed */
|
||||
public function getUserId();
|
||||
|
||||
/** @return mixed */
|
||||
public function getGroupId();
|
||||
|
||||
/** @return mixed */
|
||||
public function getDiscussId();
|
||||
|
||||
/** @return string */
|
||||
public function getMessageType();
|
||||
|
||||
/** @return mixed */
|
||||
public function getRobotId();
|
||||
|
||||
/** @return mixed */
|
||||
public function getMessage();
|
||||
|
||||
public function setMessage($msg);
|
||||
|
||||
public function setUserId($id);
|
||||
|
||||
public function setGroupId($id);
|
||||
|
||||
public function setDiscussId($id);
|
||||
|
||||
public function setMessageType($type);
|
||||
|
||||
/**
|
||||
* @param $msg
|
||||
* @param bool $yield
|
||||
* @return mixed
|
||||
*/
|
||||
public function reply($msg, $yield = false);
|
||||
|
||||
/**
|
||||
* @param $msg
|
||||
* @param bool $yield
|
||||
* @return mixed
|
||||
*/
|
||||
public function finalReply($msg, $yield = false);
|
||||
|
||||
/**
|
||||
* @param string $prompt
|
||||
* @param int $timeout
|
||||
* @param string $timeout_prompt
|
||||
* @return mixed
|
||||
*/
|
||||
public function waitMessage($prompt = "", $timeout = 600, $timeout_prompt = "");
|
||||
|
||||
/**
|
||||
* @param $arg
|
||||
* @param $mode
|
||||
* @param $prompt_msg
|
||||
* @return mixed
|
||||
*/
|
||||
public function getArgs(&$arg, $mode, $prompt_msg);
|
||||
|
||||
public function setCache($key, $value);
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @return mixed
|
||||
*/
|
||||
public function getCache($key);
|
||||
|
||||
public function cloneFromParent();
|
||||
|
||||
public function copy();
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user