@@ -34,25 +34,37 @@ public function onWorkerStart(Worker $worker)
34
34
{
35
35
ProcessManager::initProcess (ONEBOT_PROCESS_WORKER , $ worker ->id );
36
36
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
+ }
38
42
}
39
43
40
44
/**
41
45
* Workerman 的顶层 workerStop 事件回调
42
46
*/
43
47
public function onWorkerStop ()
44
48
{
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
+ }
46
54
}
47
55
48
56
/**
49
57
* Workerman 的顶层 onWebSocketConnect 事件回调
50
58
*
51
59
* @param TcpConnection $connection 连接本身
52
- * @param mixed $data 数据
53
60
*/
54
- public function onWebSocketOpen (array $ config , TcpConnection $ connection, $ data )
61
+ public function onWebSocketOpen (array $ config , TcpConnection $ connection )
55
62
{
63
+ // 协程套娃
64
+ if (($ co = Adaptive::getCoroutine ()) !== null && $ co ->getCid () === -1 ) {
65
+ $ co ->create ([$ this , 'onWebSocketOpen ' ], $ config , $ connection );
66
+ return ;
67
+ }
56
68
// WebSocket 隐藏特性: _SERVER 全局变量会在 onWebSocketConnect 中被替换为当前连接的 Header 相关信息
57
69
try {
58
70
global $ _SERVER ;
@@ -91,6 +103,11 @@ public function onWebSocketOpen(array $config, TcpConnection $connection, $data)
91
103
*/
92
104
public function onWebSocketClose (array $ config , TcpConnection $ connection )
93
105
{
106
+ // 协程套娃
107
+ if (($ co = Adaptive::getCoroutine ()) !== null && $ co ->getCid () === -1 ) {
108
+ $ co ->create ([$ this , 'onWebSocketClose ' ], $ config , $ connection );
109
+ return ;
110
+ }
94
111
if (($ connection ->worker instanceof Worker) && ($ socket = WorkermanDriver::getInstance ()->getWSServerSocketByWorker ($ connection ->worker )) !== null ) {
95
112
unset($ socket ->connections [$ connection ->id ]);
96
113
} else {
@@ -110,6 +127,11 @@ public function onWebSocketClose(array $config, TcpConnection $connection)
110
127
*/
111
128
public function onWebSocketMessage (array $ config , TcpConnection $ connection , $ data )
112
129
{
130
+ // 协程套娃
131
+ if (($ co = Adaptive::getCoroutine ()) !== null && $ co ->getCid () === -1 ) {
132
+ $ co ->create ([$ this , 'onWebSocketMessage ' ], $ config , $ connection , $ data );
133
+ return ;
134
+ }
113
135
try {
114
136
ob_logger ()->debug ('WebSocket message from: ' . $ connection ->id );
115
137
$ frame = FrameFactory::createTextFrame ($ data );
@@ -132,6 +154,11 @@ public function onWebSocketMessage(array $config, TcpConnection $connection, $da
132
154
133
155
public function onHttpRequest (array $ config , TcpConnection $ connection , Request $ request )
134
156
{
157
+ // 协程套娃
158
+ if (($ co = Adaptive::getCoroutine ()) !== null && $ co ->getCid () === -1 ) {
159
+ $ co ->create ([$ this , 'onHttpRequest ' ], $ config , $ connection , $ request );
160
+ return ;
161
+ }
135
162
$ port = $ connection ->getLocalPort ();
136
163
ob_logger ()->debug ('Http request from ' . $ port . ': ' . $ request ->uri ());
137
164
$ event = new HttpRequestEvent (HttpFactory::createServerRequest (
0 commit comments