Files
kiri-core/http-server/config.php
T
2020-09-02 11:38:47 +08:00

112 lines
2.7 KiB
PHP

<?php
use HttpServer\Route\Router;
use HttpServer\Server;
use Snowflake\Snowflake;
use Snowflake\Event;
use Swoole\Http\Request;
use Swoole\Http\Response;
use Swoole\WebSocket\Frame;
return [
'servers' => [
[
'id' => '',
'type' => Server::HTTP,
'host' => '127.0.0.1',
'port' => 9527,
'settings' => [
'worker_num' => 10,
'enable_coroutine' => 1
],
'events' => [
Event::SERVER_WORKER_START => function () {
$router = Snowflake::get()->router;
$router->loadRouterSetting();
},
]
],
[
'id' => '',
'type' => Server::PACKAGE,
'host' => '127.0.0.1',
'port' => 9628,
'settings' => [
'worker_num' => 10,
'enable_coroutine' => 1
],
'message' => [
'pack' => function ($data) {
return \Snowflake\Core\JSON::encode($data);
},
'unpack' => function ($data) {
return \Snowflake\Core\JSON::decode($data);
},
],
'events' => [
]
],
[
'id' => '',
'type' => Server::TCP,
'host' => '127.0.0.1',
'port' => 9629,
'settings' => [
'worker_num' => 10,
'enable_coroutine' => 1
],
'message' => [
'pack' => function ($data) {
return \Snowflake\Core\JSON::encode($data);
},
'unpack' => function ($data) {
return \Snowflake\Core\JSON::decode($data);
},
],
'events' => [
Event::RECEIVE_CONNECTION => function ($data) {
return 'hello word~';
}
]
],
[
'id' => '',
'type' => Server::WEBSOCKET,
'host' => '127.0.0.1',
'port' => 9530,
'settings' => [
'worker_num' => 10,
'enable_coroutine' => 1
],
'events' => [
Event::SERVER_WORKER_START => function () {
$path = APP_PATH . 'app/Websocket';
$websocket = Snowflake::get()->annotation->websocket;
$websocket->registration_notes($path, 'App\\Sockets\\');
},
Event::SERVER_HANDSHAKE => function (Request $request, Response $response) {
$this->error($request->fd . ' connect.');
$response->status(101);
$response->end();
},
Event::SERVER_MESSAGE => function (\Swoole\WebSocket\Server $server, Frame $frame) {
$this->error('websocket SERVER_MESSAGE.');
if (is_null($json = json_decode($frame->data, true))) {
return $server->push($frame->fd, 'format error~');
}
$websocket = Snowflake::get()->annotation->websocket;
if ($websocket->has($json['path'])) {
return $websocket->runWith($json['path'], [$frame->fd, $json]);
} else {
return $server->push($frame->fd, 'hello word~');
}
},
Event::SERVER_CLOSE => function (int $fd) {
$this->error($fd . ' disconnect.');
return 'hello word~';
}
]
],
]
];