Files
kiri-core/HttpServer/config.php
T

112 lines
2.7 KiB
PHP
Raw Normal View History

2020-08-31 01:27:08 +08:00
<?php
use HttpServer\Server;
use Snowflake\Snowflake;
use Snowflake\Event;
use Swoole\Http\Request;
use Swoole\Http\Response;
use Swoole\WebSocket\Frame;
return [
'servers' => [
[
2020-08-31 01:39:28 +08:00
'id' => '',
2020-08-31 01:27:08 +08:00
'type' => Server::HTTP,
'host' => '127.0.0.1',
'port' => 9527,
'settings' => [
'worker_num' => 10,
'enable_coroutine' => 1
],
'events' => [
Event::SERVER_WORKER_START => function () {
2020-09-03 11:39:20 +08:00
$router = Snowflake::app()->router;
2020-09-01 03:38:27 +08:00
$router->loadRouterSetting();
2020-08-31 01:27:08 +08:00
},
]
],
2020-12-23 16:58:40 +08:00
2020-08-31 01:27:08 +08:00
[
2020-08-31 01:39:28 +08:00
'id' => '',
2020-08-31 01:27:08 +08:00
'type' => Server::PACKAGE,
'host' => '127.0.0.1',
'port' => 9628,
'settings' => [
'worker_num' => 10,
'enable_coroutine' => 1
],
'message' => [
'pack' => function ($data) {
2020-12-24 11:12:23 +08:00
return \Snowflake\Core\Json::encode($data);
2020-08-31 01:27:08 +08:00
},
'unpack' => function ($data) {
2020-12-24 11:12:23 +08:00
return \Snowflake\Core\Json::decode($data);
2020-08-31 01:27:08 +08:00
},
],
'events' => [
]
],
[
2020-08-31 01:39:28 +08:00
'id' => '',
2020-08-31 01:27:08 +08:00
'type' => Server::TCP,
'host' => '127.0.0.1',
'port' => 9629,
'settings' => [
'worker_num' => 10,
'enable_coroutine' => 1
],
'message' => [
'pack' => function ($data) {
2020-12-24 11:12:23 +08:00
return \Snowflake\Core\Json::encode($data);
2020-08-31 01:27:08 +08:00
},
'unpack' => function ($data) {
2020-12-24 11:12:23 +08:00
return \Snowflake\Core\Json::decode($data);
2020-08-31 01:27:08 +08:00
},
],
'events' => [
Event::RECEIVE_CONNECTION => function ($data) {
return 'hello word~';
}
]
],
[
2020-08-31 01:39:28 +08:00
'id' => '',
2020-08-31 01:27:08 +08:00
'type' => Server::WEBSOCKET,
'host' => '127.0.0.1',
'port' => 9530,
'settings' => [
'worker_num' => 10,
'enable_coroutine' => 1
],
'events' => [
Event::SERVER_WORKER_START => function () {
2020-08-31 22:33:50 +08:00
$path = APP_PATH . 'app/Websocket';
2020-09-03 11:39:20 +08:00
$websocket = Snowflake::app()->annotation->websocket;
2020-08-31 22:33:50 +08:00
$websocket->registration_notes($path, 'App\\Sockets\\');
2020-08-31 01:27:08 +08:00
},
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.');
2020-08-31 22:33:50 +08:00
if (is_null($json = json_decode($frame->data, true))) {
return $server->push($frame->fd, 'format error~');
}
2020-09-03 11:39:20 +08:00
$websocket = Snowflake::app()->annotation->websocket;
2020-08-31 22:33:50 +08:00
if ($websocket->has($json['path'])) {
return $websocket->runWith($json['path'], [$frame->fd, $json]);
} else {
return $server->push($frame->fd, 'hello word~');
}
2020-08-31 01:27:08 +08:00
},
Event::SERVER_CLOSE => function (int $fd) {
$this->error($fd . ' disconnect.');
return 'hello word~';
}
]
],
]
];