Fixed: http server will be affected of wesocket server

This commit is contained in:
李铭昕 2019-06-25 10:06:55 +08:00
parent 1bfb8d1d07
commit 204b1ac3da
3 changed files with 14 additions and 12 deletions

View File

@ -24,7 +24,7 @@ class FdCollector
static::$fds[$id] = new Fd($id, $class);
}
public static function get(int $id, $default = null): Fd
public static function get(int $id, $default = null): ?Fd
{
return static::$fds[$id] ?? $default;
}

View File

@ -30,12 +30,9 @@ class CoreMiddleware extends HttpCoreMiddleware
{
/** @var ResponseInterface $response */
$uri = $request->getUri();
/**
* @var array
* Returns array with one of the following formats:
* [self::NOT_FOUND]
* [self::METHOD_NOT_ALLOWED, ['GET', 'OTHER_ALLOWED_METHODS']]
* [self::FOUND, $handler, ['varName' => 'value', ...]]
*/
$routes = $this->dispatcher->dispatch($request->getMethod(), $uri->getPath());

View File

@ -154,15 +154,20 @@ class Server implements MiddlewareInitializerInterface, OnHandShakeInterface, On
public function onClose(\Swoole\Server $server, int $fd, int $reactorId): void
{
$this->logger->debug("WebSocket: fd[{$fd}] close a active connection.");
if ($obj = FdCollector::get($fd)) {
$this->logger->debug("WebSocket: fd[{$fd}] close a active connection.");
if (! $this->container->has($obj)) {
$this->logger->error("WebSocket: class[{$obj}] is not defined.");
return;
}
$obj = FdCollector::get($fd);
$class = $this->container->get($obj->class);
if ($class instanceof OnCloseInterface) {
$class->onClose($server, $fd, $reactorId);
$class = $this->container->get($obj->class);
if ($class instanceof OnCloseInterface) {
$class->onClose($server, $fd, $reactorId);
}
FdCollector::del($fd);
}
FdCollector::del($fd);
}
/**