puper/laravoole
在Swoole或Workerman上为Laravel提供10倍性能
Requires
- php: >=5.5.16
- ext-posix: *
- laravel/framework: 5.1.* || 5.2.* || 5.3.*
Suggests
- ext-swoole: event-based extension for best performance
- workerman/workerman: event-based library without need for any extension
README
#Laravoole
Swoole或Workerman上的Laravel
比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.on_request和laravoole.swoole.websocket.on_close。您可以通过编辑EventServiceProvider来处理事件
public function boot() { parent::boot(); \Event::listen('laravoole.on_request', function ($request) { \Log::info($request->segments()); }); }
##base_config
本节配置laravoole本身。
###mode
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向客户端发送静态资源。仅当开发时使用此选项。
###host和port
默认host为127.0.0.1,port为9050
##handler_config
本节配置后端,例如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