diff --git a/src/Router/Dispatcher.php b/src/Router/Dispatcher.php index 61e68565..b54ba70a 100644 --- a/src/Router/Dispatcher.php +++ b/src/Router/Dispatcher.php @@ -41,7 +41,7 @@ public function handshake(Request $request, Response $response): array if ($e instanceof WsRouteException) { return [ HandlerInterface::HANDSHAKE_FAIL, - $response->withStatus(404)->withAddedHeader('Failed-Reason', 'Route not found') + $response->withStatus(404)->withAddedHeader('Failure-Reason', 'Route not found') ]; } @@ -105,10 +105,8 @@ public function message(Server $server, Frame $frame) $handler = \bean($className); $handler->onMessage($server, $frame); } catch (\Throwable $e) { - /** @var \Swoft\Event\EventManager $em */ - $em = App::getBean('eventManager'); - - if ($em->hasListenerQueue(WsEvent::ON_ERROR)) { + /** @see \Swoft\Event\EventManager::hasListenerQueue() */ + if (App::hasBean('eventManager') && \bean('eventManager')->hasListenerQueue(WsEvent::ON_ERROR)) { App::trigger(WsEvent::ON_ERROR, $frame, $e); } else { App::error($e->getMessage(), ['fd' => $fd, 'data' => $frame->data]); diff --git a/src/WebSocketContext.php b/src/WebSocketContext.php index 28a79c96..de7f5e6c 100644 --- a/src/WebSocketContext.php +++ b/src/WebSocketContext.php @@ -262,7 +262,7 @@ public static function getFdByCoId() * @param int|null $cid * @return bool */ - public static function delFdToCoId(int $cid = null): bool + public static function delFdByCoId(int $cid = null): bool { $cid = $cid > -1 ? $cid : self::getCoroutineId(); diff --git a/src/WebSocketEventTrait.php b/src/WebSocketEventTrait.php index 9d2ca8b3..35fe8be7 100644 --- a/src/WebSocketEventTrait.php +++ b/src/WebSocketEventTrait.php @@ -101,7 +101,7 @@ public function onHandShake(Request $request, Response $response): bool }); // delete coId to fd mapping - WebSocketContext::delFdToCoId(); + WebSocketContext::delFdByCoId(); return true; } @@ -168,7 +168,7 @@ public function onMessage(Server $server, Frame $frame) \bean('wsDispatcher')->message($server, $frame); // delete coId to fd mapping - WebSocketContext::delFdToCoId(); + WebSocketContext::delFdByCoId(); } /** diff --git a/src/WebSocketServer.php b/src/WebSocketServer.php index 96e2fbc2..d9ef4548 100644 --- a/src/WebSocketServer.php +++ b/src/WebSocketServer.php @@ -176,7 +176,7 @@ public function sendTo(int $receiver, string $data, int $sender = 0): int } /** - * broadcast message, will exclude self. + * broadcast message, will exclude sender. * @param string $data 消息数据 * @param int $sender 发送者 * @param int[] $receivers 指定接收者们 @@ -194,15 +194,16 @@ public function broadcast(string $data, array $receivers = [], array $excluded = return $this->sendTo((int)\array_shift($receivers), $data, $sender); } + // excepted itself + if ($sender) { + $excluded[] = $sender; + } + // to all if (!$excluded && !$receivers) { return $this->sendToAll($data, $sender); } - if ($sender) { - $excluded[] = $sender; - } - // to some return $this->sendToSome($data, $receivers, $excluded, $sender); }