modify plugin name

This commit is contained in:
2022-02-14 10:45:39 +08:00
parent 567ceb69c0
commit 2507c30b27
3 changed files with 94 additions and 75 deletions
+87 -68
View File
@@ -90,80 +90,99 @@ class Http extends Component
{
$this->configs = $config;
foreach ($config as $value) {
$value = $this->resolveCallback($value);
if ($value['type'] == Constant::SERVER_TYPE_HTTP) {
$onRequest = $value['events'][Constant::REQUEST] ?? null;
if (is_null($onRequest)) {
throw new \Exception('Server callback con\'t null.');
}
$server = new Coroutine\Http\Server($value['host'], $value['port'], null, true);
$this->_addListener($value);
}
}
$this->servers[$value['port']] = $server;
$server->handle('/', function (Request $request, Response $response) use ($onRequest) {
call_user_func($onRequest, $request, $response);
});
} else if ($value['type'] == Constant::SERVER_TYPE_WEBSOCKET) {
$handshake = $value['events'][Constant::HANDSHAKE] ?? null;
public function addListener(string $type, string $host, int $port, int $mode, array $settings = [])
{
$open = $value['events'][Constant::OPEN] ?? null;
}
$close = $value['events'][Constant::CLOSE] ?? null;
$message = $value['events'][Constant::MESSAGE] ?? null;
if (is_null($message)) {
throw new \Exception('Server callback con\'t null.');
}
$server = new Coroutine\Http\Server($value['host'], $value['port'], null, true);
$sender = $this->getContainer()->get(Sender::class);
$sender->setServer($server);
$this->servers[$value['port']] = $server;
$server->handle('/', function (Request $request, Response $response) use ($handshake, $open, $close, $message) {
if (is_null($handshake)) {
$response->upgrade();
} else {
call_user_func($handshake, $request, $response);
}
if ($response->isWritable() && is_callable($open)) {
call_user_func($open, $response);
}
while (($data = $response->recv()) instanceof Frame) {
call_user_func($message, $data);
}
call_user_func($close, $response->fd);
});
} else {
$message = $value['events'][Constant::RECEIVE] ?? null;
if (is_null($message)) {
throw new \Exception('Server callback con\'t null.');
}
$conn = $value['events'][Constant::CONNECT] ?? null;
$close = $value['events'][Constant::CLOSE] ?? null;
$server = new Coroutine\Server($value['host'], $value['port'], null, true);
$this->servers[$value['port']] = $server;
$server->handle(function (Coroutine\Server\Connection $connection) use ($message, $conn, $close) {
if (!is_null($conn)) {
call_user_func($conn, $connection);
}
while (true) {
$data = $connection->recv(1024);
if ($data === '' || $data === false) {
defer(function () use ($close, $connection) {
call_user_func($close, $connection);
});
$connection->close();
break;
}
call_user_func($message, $data);
}
});
/**
* @param $value
* @return void
* @throws ContainerExceptionInterface
* @throws Exception
* @throws NotFoundExceptionInterface
*/
protected function _addListener($value)
{
$value = $this->resolveCallback($value);
if ($value['type'] == Constant::SERVER_TYPE_HTTP) {
$onRequest = $value['events'][Constant::REQUEST] ?? null;
if (is_null($onRequest)) {
throw new Exception('Server callback con\'t null.');
}
$server = new Coroutine\Http\Server($value['host'], $value['port'], null, true);
$this->servers[$value['port']] = $server;
$server->handle('/', function (Request $request, Response $response) use ($onRequest) {
call_user_func($onRequest, $request, $response);
});
} else if ($value['type'] == Constant::SERVER_TYPE_WEBSOCKET) {
$handshake = $value['events'][Constant::HANDSHAKE] ?? null;
$open = $value['events'][Constant::OPEN] ?? null;
$close = $value['events'][Constant::CLOSE] ?? null;
$message = $value['events'][Constant::MESSAGE] ?? null;
if (is_null($message)) {
throw new Exception('Server callback con\'t null.');
}
$server = new Coroutine\Http\Server($value['host'], $value['port'], null, true);
$sender = $this->getContainer()->get(Sender::class);
$sender->setServer($server);
$this->servers[$value['port']] = $server;
$server->handle('/', function (Request $request, Response $response) use ($handshake, $open, $close, $message) {
if (is_null($handshake)) {
$response->upgrade();
} else {
call_user_func($handshake, $request, $response);
}
if ($response->isWritable() && is_callable($open)) {
call_user_func($open, $response);
}
while (($data = $response->recv()) instanceof Frame) {
call_user_func($message, $data);
}
call_user_func($close, $response->fd);
});
} else {
$message = $value['events'][Constant::RECEIVE] ?? null;
if (is_null($message)) {
throw new Exception('Server callback con\'t null.');
}
$conn = $value['events'][Constant::CONNECT] ?? null;
$close = $value['events'][Constant::CLOSE] ?? null;
$server = new Coroutine\Server($value['host'], $value['port'], null, true);
$this->servers[$value['port']] = $server;
$server->handle(function (Coroutine\Server\Connection $connection) use ($message, $conn, $close) {
if (!is_null($conn)) {
call_user_func($conn, $connection);
}
while (true) {
$data = $connection->recv(1024);
if ($data === '' || $data === false) {
defer(function () use ($close, $connection) {
call_user_func($close, $connection);
});
$connection->close();
break;
}
call_user_func($message, $data);
}
});
}
}
+1 -1
View File
@@ -99,7 +99,7 @@ class Server extends HttpService
$this->getEventDispatch()->dispatch(new OnServerBeforeStart());
return $this->manager->getServer()->start();
return $this->manager->start();
}
+6 -6
View File
@@ -59,12 +59,12 @@ class ServerManager extends Component
const DEFAULT_EVENT = [
Constant::WORKER_START => [OnServerWorker::class, 'onWorkerStart'],
Constant::WORKER_EXIT => [OnServerWorker::class, 'onWorkerExit'],
Constant::WORKER_STOP => [OnServerWorker::class, 'onWorkerStop'],
Constant::WORKER_ERROR => [OnServerWorker::class, 'onWorkerError'],
Constant::START => [OnServer::class, 'onStart'],
Constant::SHUTDOWN => [OnServer::class, 'onShutdown'],
Constant::WORKER_START => [OnServerWorker::class, 'onWorkerStart'],
Constant::WORKER_EXIT => [OnServerWorker::class, 'onWorkerExit'],
Constant::WORKER_STOP => [OnServerWorker::class, 'onWorkerStop'],
Constant::WORKER_ERROR => [OnServerWorker::class, 'onWorkerError'],
Constant::START => [OnServer::class, 'onStart'],
Constant::SHUTDOWN => [OnServer::class, 'onShutdown'],
];