π Service
Overviewβ
ripple provides a memory-resident service mode operation, which can run your program as a service. Compared with the traditional CGI mode workflow, The workflow of the service mode can effectively improve the performance of the program and reduce the unnecessary consumption of loading files. In extensive practice, the performance of the service mode is much higher than the traditional CGI mode.
Currently the project supports ThinkPHP / Laravel / Workerman / Webman
Installation methodβ
Install via Composer
composer require cloudtay/ripple-driver
Deployment referenceβ
Workermanβ
Worker::$eventLoopClass = \Ripple\Drive\Workerman\Driver::class;
Worker::runAll();
Webmanβ
Modify the configuration file config/server.php service configuration file
return [
//...
'event_loop' => \Ripple\Drive\Workerman\Driver::class,
];
Laravelβ
Environment configuration support (ENV)β
Configuration items | Description | Default value |
---|---|---|
PRP_HTTP_LISTEN | HTTP service, listening address format such as http://127.0.0.1:8008 | http://127.0.0.1:8008 |
PRP_HTTP_WORKERS | HTTP service, number of worker processes | 4 |
PRP_ISOLATION | Controller isolation mode, after turning on, the Controller will be re-instantiated for each | |
request, suitable for stateful Controller isolation $this->request | 0 |
php artisan ripple:server {action} {--daemon}
# action: start|stop|reload|status, default is start
# -d | --daemon Whether to run as a daemon process, the default is false
publish profileβ
php artisan vendor:publish --tag=ripple-config
runβ
php artisan ripple:server {action} {--daemon}
# action: start|stop|reload|status, Default is start
# -d | --daemon # Whether to run as a daemon process, the default is false
open
http://127.0.0.1:8008/
ThinkPHPβ
php think ripple:server {action} {--daemon}
# action: start|stop|reload|status, default is start
# -d | --daemon Whether to run as a daemon process, the default is false
open
http://127.0.0.1:8008/
Nginx Referenceβ
location / {
try_files $uri $uri/ @backend;
}
location @backend {
proxy_pass http://127.0.0.1:8008;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Original-Method $request_method;
proxy_set_header X-Original-URI $request_uri;
proxy_set_header X-Original-Query $query_string;
proxy_set_header X-Forwarded-Port $server_port;
proxy_set_header User-Agent $http_user_agent;
proxy_set_header Referer $http_referer;
}
Notesβ
You need to have a certain understanding of the mechanism of the CLI running mode, and know what will happen during the running of the following functions to decide how to use them? For example
dd
var_dump
echo
exit
die
Custom serviceβ
Laravel/ThinkPHP's Http service is also implemented based on Worker, built into Drive and injected with Laravel's dependency injection. You can implement custom services by inheriting the Worker class and use HttpWorker to call each other, such as
Ws.phpβ
<?php declare(strict_types=1);
namespace ApCo\Server;
use Co\Net;
use Ripple\WebSocket\Server\Connection;
use Ripple\WebSocket\Server\Server;
use Ripple\Worker\Command;
use Ripple\Worker\Manager;
use Ripple\Worker\Worker;
class WsWorker extends Worker
{
private Server $wsServer;
private array $connections = [];
public function register(Manager $manager): void
{
$this->wsServer = Net::WebSocket()->server('ws://127.0.0.1:8001', []);
}
public function boot(): void
{
$this->wsServer->onMessage(function (string $content, Connection $connection) {
$connection->send("response > {$content}");
});
$this->wsServer->onConnect(function (Connection $connection) {
$this->connections[$connection->getId()] = $connection;
});
$this->wsServer->onClose(function (Connection $connection) {
unset($this->connections[$connection->getId()]);
});
$this->wsServer->listen();
}
public function onCommand(Command $workerCommand): void
{
if ($workerCommand->name === 'sendMessageToAll') {
foreach ($this->connections as $connection) {
$connection->send($workerCommand->arguments['message']);
}
}
}
public function getName(): string
{
return 'ws-server';
}
public function getCount(): int
{
return 1;
}
public function onReload(): void
{
// TODO: Implement onReload() method.
}
}
AppServiceProvider.phpβ
<?php declare(strict_types=1);
namespace ApCo\Providers;
use ApCo\Server\WsWorker;
use Illuminate\Support\ServiceProvider;
use Ripple\Worker\Manager;
class AppServiceProvider extends ServiceProvider
{
public function boot(Manager $manager): void
{
$manager->addWorker(new WsWorker());
}
}
Access Servicesβ
class IndexController extends Controller
{
public function notice(Request $request,\Ripple\Drive\Laravel\Worker $httpWorker) : JsonResponse
{
$command = Command::make('sendMessageToAll', [
'message' => 'post message ' . $request->post('message')
]);
$httpWorker->commandToWorker($command, 'ws-server');
return Response::json(['message' => 'success']);
}
}