jyj1993126 / laravoole
在Swoole或Workerman上为Laravel提供10倍性能
Requires
- php: >=5.5.16
- ext-posix: *
- laravel/framework: 5.1.* || 5.2.*
Suggests
- ext-swoole: event-based extension for best performance
- workerman/workerman: event-based library without need for any extension
This package is not auto-updated.
Last update: 2024-09-23 13:54:46 UTC
README
#Laravoole
Laravel在Swoole或Workerman上运行
比php-fpm快10倍
##依赖项
##建议
##安装
要开始使用,请将laravoole添加到您的composer.json文件中,并运行composer update
"garveen/laravoole": "~0.2"
或者直接运行shell命令
composer require garveen/laravoole
一旦composer完成其任务,您需要在config/app.php中注册Laravel服务提供者
'providers' => [
...
Laravoole\LaravooleServiceProvider::class,
],
注意:您不应该使用文件会话处理程序,因为在这个环境中它不稳定。请使用redis或其他处理程序。
##使用
php artisan laravoole [start | stop | reload | reload_task | restart | quit]
##配置
要生成config/laravoole.php
php artisan vendor:publish --provider="Laravoole\LaravooleServiceProvider"
大多数配置都可以通过.env
文件完成,并且您应该使用LARAVOOLE_{UPPER_CASE}
格式,例如:
[ 'base_config' => [ 'host' => '0.0.0.0', ] ]
等同于
LARAVOOLE_HOST=0.0.0.0
##基本配置
本节配置laravoole本身。
###模式
SwooleHttp
使用swoole响应HTTP请求
SwooleFastCGI
使用swoole响应fastcgi请求(类似于php-fpm)
SwooleWebSocket
使用swoole响应websocket请求和HTTP请求
WorkermanFastCGI
使用workerman响应fastcgi请求(类似于php-fpm)
###pid_file
定义一个存储主进程进程ID的文件。
###deal_with_public
在HTTP模式下使用时,您可以启用此选项,让laravoole向客户端发送静态资源。仅当开发时使用此功能。
###主机和端口
默认主机为127.0.0.1
,端口为9050
##后端配置
本节配置后端,例如swoole
或workerman
。
###Swoole
例如,如果要将worker_num设置为8,您可以在.env
中设置
LARAVOOLE_WORKER_NUM=8
或者在config/laravoole.php
中设置
[ 'handler_config' => [ 'worker_num' => 8, ] ]
请参阅Swoole的文档
###Workerman
例如,如果要将worker_num设置为8,您可以在.env
中设置
LARAVOOLE_COUNT=8
或者在config/laravoole.php
中设置
[ 'handler_config' => [ 'count' => 8, ] ]
请参阅Workerman的文档
##WebSocket使用
###子协议
请参阅Mozilla的文档:编写WebSocket服务器
默认子协议是jsonrpc,但有一些不同:params
是一个对象,并且还有两个额外的属性
status
作为HTTP状态码
method
与请求的方法相同
您可以通过实现Laravoole\WebsocketCodec\CodecInterface
并将其添加到config/laravoole.php
中来定义自己的子协议。
###客户端示例
<!DOCTYPE html> <meta charset="utf-8" /> <title>WebSocket Test</title> <style> p{word-wrap: break-word;} tr:nth-child(odd){background-color: #ccc} tr:nth-child(even){background-color: #eee} </style> <h2>WebSocket Test</h2> <table><tbody id="output"></tbody></table> <script> var wsUri = "ws://:9050/websocket"; var protocols = ['jsonrpc']; var output = document.getElementById("output"); function send(message) { websocket.send(message); log('Sent', message); } function log(type, str) { str = str.replace(/&/g, '&').replace(/"/g, '"').replace(/'/g, ''').replace(/</g, '<').replace(/>/g, '>'); output.insertAdjacentHTML('beforeend', '<tr><td>' + type + '</td><td><p>' + htmlEscape(str) + '</p></td></tr>'); } websocket = new WebSocket(wsUri, protocols); websocket.onopen = function(evt) { log('Status', "Connection opened"); send(JSON.stringify({method: '/', params: {hello: 'world'}, id: 1})); setTimeout(function(){ websocket.close() },1000) }; websocket.onclose = function(evt) { log('Status', "Connection closed") }; websocket.onmessage = function(evt) { log('<span style="color: blue;">Received</span>', evt.data) }; websocket.onerror = function(evt) { log('<span style="color: red;">Error</span>', evt.data) }; </script> </html>
##与nginx协同工作
server { listen 80; server_name localhost; root /path/to/laravel/public; location / { try_files $uri $uri/ @laravoole; index index.html index.htm index.php; } # http location @laravoole { proxy_set_header Host $host:$server_port; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_http_version 1.1; proxy_pass http://127.0.0.1:9050; } # fastcgi location @laravoole { include fastcgi_params; fastcgi_pass 127.0.0.1:9050; } # websocket # send close if there has not an upgrade header map $http_upgrade $connection_upgrade { default upgrade; '' close; } location /websocket { proxy_connect_timeout 7d; proxy_send_timeout 7d; proxy_read_timeout 7d; proxy_pass http://127.0.0.1:9050; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $connection_upgrade; } }
#许可证 MIT