hansen1416 / laravel-ratchet
Ratchet 示例
Requires
- php: >=5.5.9
- ext-zmq: *
- cboden/ratchet: ^0.3
- graham-campbell/throttle: ^5.2
- laravel/framework: 5.*
- react/zmq: 0.2.*|0.3.*
This package is not auto-updated.
Last update: 2024-09-18 22:00:50 UTC
README

此包提供了 artisan 命令 ratchet:serve,它将启动一个基于您创建的类的 Ratchet Io 服务器、Web Socket 或 Wamp 服务器。它包含了一些函数,如 abort()、send() 和 sendAll(),以简化一些常见任务。
支持
- WaServer, WampServer & IoServer
- IP 黑名单
- 连接限速
- 消息限速
安装
使用 composer 安装
composer require askedio/laravel-ratchet
在 config/app.php 中注册 provider。
Askedio\LaravelRatchet\Providers\LaravelRatchetServiceProvider::class,
安装 ZMQ 以使用 WampServer 驱动程序。
apt-get install php7.0-zmq
推送服务器
默认驱动程序将创建一个基于 Ratchets 示例 的简单推送服务器。
php artisan ratchet:serve
Starting WampServer server on: 0.0.0.0:8080
Starting ZMQ server on: 127.0.0.1:5555
基于 RatchetServerExample.php 创建您自己的类。
使用 Ratchet 提供的 示例 将数据插入 ZMQ。
Socket 服务器
使用 IoServer driver 和 RatchetServerExample.php class 创建一个简单的 socket 服务器。
这里是一个您可以在 App 中使用的示例。
<?php
namespace App;
use Ratchet\ConnectionInterface;
use Askedio\LaravelRatchet\RatchetServer;
class RatchetServer extends RatchetServer
{
public function onMessage(ConnectionInterface $conn, $input)
{
parent::onMessage($conn, $input);
if (!$this->throttled) {
$this->send($conn, 'Hello you.');
$this->sendAll('Hello everyone.');
$this->send($conn, 'Wait, I don\'t know you! Bye bye!');
$this->abort($conn);
}
}
}
您需要将类更改到命令行或配置中。
php artisan ratchet:serve --driver=IoServer --class="App\RatchetServer::class"
命令行
要使用配置中的默认值运行命令,请按照以下方式操作
php artisan ratchet:serve
您也可以在命令行上定义配置项
php artisan ratchet:serve --help
Usage:
ratchet:serve [options]
Options:
--host[=HOST] Ratchet server host [default: "0.0.0.0"]
-p, --port[=PORT] Ratchet server port [default: "9090"]
--class[=CLASS] Class that implements MessageComponentInterface. [default: "Askedio\LaravelRatchet\RatchetServerExample"]
--driver[=DRIVER] Ratchet connection driver [IoServer|WsServer|WampServer] [default: "WampServer"]
...
配置
有一些配置值您可能需要更改。发布配置后,您可以编辑 config/ratchet.php。
php artisan vendor:publish
配置选项
- class:您的 MessageComponentInterface 或 WampServerInterface 类。
- host:要监听的宿主。
- port:要监听的端口。
- connectionLimit:允许的总连接数(仅限 RatchetServer)。
- throttle:限制连接和消息。
- onOpen:连接的 limit:delay。
- onMessage:消息的 limit:delay。
- abortOnMessageThrottle:当消息限速触发时断开客户端连接。
- blackList:使用 IpBlackList 禁止的宿主集合或模型。
选项
向当前连接发送消息。
$this->send($conn, $message);
向所有连接发送消息。
$this->sendAll($message);
关闭当前连接。
$this->abort($conn);
Supervisor 配置
Supervisor 是一个客户端/服务器系统,允许用户在 UNIX 类操作系统上控制多个进程。
事物崩溃和长时间运行的进程需要监控。我们可以使用 Supervisor 来帮助处理。
安装 supervisor。
sudo apt-get install supervisor
创建配置。
将 /home/forge/app.com/ 替换为您的应用程序路径。
sudo cat <<EOF > /etc/supervisor/conf.d/laravel-ratchet.conf
[program:laravel-ratchet]
process_name=%(program_name)s_%(process_num)02d
command=php /home/forge/app.com/artisan ratchet:serve -q
autostart=true
autorestart=true
user=vagrant
numprocs=1
redirect_stderr=true
stdout_logfile=/home/forge/app.com/ratchet.log
EOF
启用并启动。
sudo supervisorctl reread
sudo supervisorctl update
supervisorctl start laravel-ratchet:*
测试
参见贡献。
贡献
编写一些测试,那就很棒了。