modify plugin name
This commit is contained in:
+87
-68
@@ -90,80 +90,99 @@ class Http extends Component
|
|||||||
{
|
{
|
||||||
$this->configs = $config;
|
$this->configs = $config;
|
||||||
foreach ($config as $value) {
|
foreach ($config as $value) {
|
||||||
$value = $this->resolveCallback($value);
|
$this->_addListener($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) {
|
public function addListener(string $type, string $host, int $port, int $mode, array $settings = [])
|
||||||
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);
|
/**
|
||||||
|
* @param $value
|
||||||
$sender = $this->getContainer()->get(Sender::class);
|
* @return void
|
||||||
$sender->setServer($server);
|
* @throws ContainerExceptionInterface
|
||||||
|
* @throws Exception
|
||||||
$this->servers[$value['port']] = $server;
|
* @throws NotFoundExceptionInterface
|
||||||
|
*/
|
||||||
$server->handle('/', function (Request $request, Response $response) use ($handshake, $open, $close, $message) {
|
protected function _addListener($value)
|
||||||
if (is_null($handshake)) {
|
{
|
||||||
$response->upgrade();
|
$value = $this->resolveCallback($value);
|
||||||
} else {
|
if ($value['type'] == Constant::SERVER_TYPE_HTTP) {
|
||||||
call_user_func($handshake, $request, $response);
|
$onRequest = $value['events'][Constant::REQUEST] ?? null;
|
||||||
}
|
if (is_null($onRequest)) {
|
||||||
if ($response->isWritable() && is_callable($open)) {
|
throw new Exception('Server callback con\'t null.');
|
||||||
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);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
$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
@@ -99,7 +99,7 @@ class Server extends HttpService
|
|||||||
|
|
||||||
$this->getEventDispatch()->dispatch(new OnServerBeforeStart());
|
$this->getEventDispatch()->dispatch(new OnServerBeforeStart());
|
||||||
|
|
||||||
return $this->manager->getServer()->start();
|
return $this->manager->start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+6
-6
@@ -59,12 +59,12 @@ class ServerManager extends Component
|
|||||||
|
|
||||||
|
|
||||||
const DEFAULT_EVENT = [
|
const DEFAULT_EVENT = [
|
||||||
Constant::WORKER_START => [OnServerWorker::class, 'onWorkerStart'],
|
Constant::WORKER_START => [OnServerWorker::class, 'onWorkerStart'],
|
||||||
Constant::WORKER_EXIT => [OnServerWorker::class, 'onWorkerExit'],
|
Constant::WORKER_EXIT => [OnServerWorker::class, 'onWorkerExit'],
|
||||||
Constant::WORKER_STOP => [OnServerWorker::class, 'onWorkerStop'],
|
Constant::WORKER_STOP => [OnServerWorker::class, 'onWorkerStop'],
|
||||||
Constant::WORKER_ERROR => [OnServerWorker::class, 'onWorkerError'],
|
Constant::WORKER_ERROR => [OnServerWorker::class, 'onWorkerError'],
|
||||||
Constant::START => [OnServer::class, 'onStart'],
|
Constant::START => [OnServer::class, 'onStart'],
|
||||||
Constant::SHUTDOWN => [OnServer::class, 'onShutdown'],
|
Constant::SHUTDOWN => [OnServer::class, 'onShutdown'],
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user