Skip to content

Commit 22e4a71

Browse files
committed
change workerman coroutine runtime
1 parent 6df72ec commit 22e4a71

File tree

3 files changed

+33
-11
lines changed

3 files changed

+33
-11
lines changed

src/OneBot/Driver/Event/EventDispatcher.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace OneBot\Driver\Event;
66

7-
use OneBot\Driver\Coroutine\Adaptive;
87
use OneBot\Driver\Interfaces\HandledDispatcherInterface;
98
use OneBot\Exception\ExceptionHandler;
109

@@ -14,12 +13,8 @@ class EventDispatcher implements HandledDispatcherInterface
1413
/**
1514
* 分发事件
1615
*/
17-
public function dispatch(object $event, bool $inside = false): object
16+
public function dispatch(object $event): object
1817
{
19-
if (($co = Adaptive::getCoroutine()) !== null && !$inside) {
20-
$co->create([$this, 'dispatch'], $event, true);
21-
return $event;
22-
}
2318
foreach (ob_event_provider()->getEventListeners($event->getName()) as $listener) {
2419
try {
2520
// TODO: 允许 Listener 修改 $event

src/OneBot/Driver/Socket/SocketFlag.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
trait SocketFlag
88
{
99
/** @var int */
10-
protected $flag = 0;
10+
protected $flag = 1;
1111

1212
public function setFlag(int $flag): self
1313
{

src/OneBot/Driver/Workerman/TopEventListener.php

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,25 +34,37 @@ public function onWorkerStart(Worker $worker)
3434
{
3535
ProcessManager::initProcess(ONEBOT_PROCESS_WORKER, $worker->id);
3636
Adaptive::initWithDriver(WorkermanDriver::getInstance());
37-
ob_event_dispatcher()->dispatchWithHandler(new WorkerStartEvent());
37+
if (($co = Adaptive::getCoroutine()) !== null) {
38+
$co->create(fn () => ob_event_dispatcher()->dispatchWithHandler(new WorkerStartEvent()));
39+
} else {
40+
ob_event_dispatcher()->dispatchWithHandler(new WorkerStartEvent());
41+
}
3842
}
3943

4044
/**
4145
* Workerman 的顶层 workerStop 事件回调
4246
*/
4347
public function onWorkerStop()
4448
{
45-
ob_event_dispatcher()->dispatchWithHandler(new WorkerStopEvent());
49+
if (($co = Adaptive::getCoroutine()) !== null) {
50+
$co->create(fn () => ob_event_dispatcher()->dispatchWithHandler(new WorkerStopEvent()));
51+
} else {
52+
ob_event_dispatcher()->dispatchWithHandler(new WorkerStopEvent());
53+
}
4654
}
4755

4856
/**
4957
* Workerman 的顶层 onWebSocketConnect 事件回调
5058
*
5159
* @param TcpConnection $connection 连接本身
52-
* @param mixed $data 数据
5360
*/
54-
public function onWebSocketOpen(array $config, TcpConnection $connection, $data)
61+
public function onWebSocketOpen(array $config, TcpConnection $connection)
5562
{
63+
// 协程套娃
64+
if (($co = Adaptive::getCoroutine()) !== null && $co->getCid() === -1) {
65+
$co->create([$this, 'onWebSocketOpen'], $config, $connection);
66+
return;
67+
}
5668
// WebSocket 隐藏特性: _SERVER 全局变量会在 onWebSocketConnect 中被替换为当前连接的 Header 相关信息
5769
try {
5870
global $_SERVER;
@@ -91,6 +103,11 @@ public function onWebSocketOpen(array $config, TcpConnection $connection, $data)
91103
*/
92104
public function onWebSocketClose(array $config, TcpConnection $connection)
93105
{
106+
// 协程套娃
107+
if (($co = Adaptive::getCoroutine()) !== null && $co->getCid() === -1) {
108+
$co->create([$this, 'onWebSocketClose'], $config, $connection);
109+
return;
110+
}
94111
if (($connection->worker instanceof Worker) && ($socket = WorkermanDriver::getInstance()->getWSServerSocketByWorker($connection->worker)) !== null) {
95112
unset($socket->connections[$connection->id]);
96113
} else {
@@ -110,6 +127,11 @@ public function onWebSocketClose(array $config, TcpConnection $connection)
110127
*/
111128
public function onWebSocketMessage(array $config, TcpConnection $connection, $data)
112129
{
130+
// 协程套娃
131+
if (($co = Adaptive::getCoroutine()) !== null && $co->getCid() === -1) {
132+
$co->create([$this, 'onWebSocketMessage'], $config, $connection, $data);
133+
return;
134+
}
113135
try {
114136
ob_logger()->debug('WebSocket message from: ' . $connection->id);
115137
$frame = FrameFactory::createTextFrame($data);
@@ -132,6 +154,11 @@ public function onWebSocketMessage(array $config, TcpConnection $connection, $da
132154

133155
public function onHttpRequest(array $config, TcpConnection $connection, Request $request)
134156
{
157+
// 协程套娃
158+
if (($co = Adaptive::getCoroutine()) !== null && $co->getCid() === -1) {
159+
$co->create([$this, 'onHttpRequest'], $config, $connection, $request);
160+
return;
161+
}
135162
$port = $connection->getLocalPort();
136163
ob_logger()->debug('Http request from ' . $port . ': ' . $request->uri());
137164
$event = new HttpRequestEvent(HttpFactory::createServerRequest(

0 commit comments

Comments
 (0)